Java基础语法目录
Java自学笔记——基础语法(一)注释、标识符、关键字Java自学笔记——基础语法(二)数据类型、类型转换
Java自学笔记——基础语法(三)变量、常量
Java自学笔记——基础语法(四)运算符
Java自学笔记——基础语法(五)包机制、JavaDoc
目录
- Java语言支持如下运算符:
- 一、运算符优先级排列
- 二、运算符的种类
- 1)算数运算符
- 2)关系运算符
- 3)逻辑运算符
- 4)位运算符
- 5)条件运算符
- 6)扩展运算符
Java语言支持如下运算符:
- 算术运算符:+, -, * ,/,%,++,–
- 赋值运算符 =
- 关系运算符: >,<, >=, <= , ==, != instanceof
- 逻辑运算符: &&,‖,!
- 位运算符: &,|,^,~,>>,<<,>>>(了解!!!)
- 条件运算符 ? :
- 扩展赋值运算符:+=,-=,*=,/=
一、运算符优先级排列
优先级 | 运算符 | 简介 |
1 | [ ] | 方法调用,属性获取 |
2 | !、~、 ++、 – | 一元运算符 |
3 | * 、/ 、% | 乘、除、取模(余数) |
4 | + 、 - | 加减法 |
5 | <<、 >>、 >>> | 左位移、右位移、无符号右移 |
6 | < 、<= 、>、 >=、 instanceof | 小于、小于等于、大于、大于等于, 对象类型判断是否属于同类型 |
7 | == 、!= | 2个值是否相等,2个值是否不等于。下面有详细的解释 |
8 | & | 按位与 |
9 | ^ | 按位异或 |
10 | | | 按位或 |
11 | && | 与 |
12 | || | 或 |
13 | ? : | 条件运算符 |
14 | =、 += 、-= 、*= 、/=、 %=、 &=、 |=、 ^=、 <、<= 、>、>= 、>>= | 混合赋值运算符 |
二、运算符的种类
1)算数运算符
举例1
package operator; // 新创建的包
public class Demo01 {
public static void main(String[] args) {
// 二元运算符
// ctrl +D
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b); // 0 0.5取0 加上强转 0.5
}
}
举例2
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 12312323123232L;
int b = 123;
short c = 10;
byte d = 8;
//有 long 的类型,相加以后变为long
//有 double 的类型,相加以后变为long
//也就是说会变成最高类型的
System.out.println(a+b+c+d); //1231232321213364 Long
System.out.println(b+c+d); //141 Int
System.out.println(c+d); //18 Int
System.out.println((double) c+d); //18 Int
}
}
举例3
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增 , 自减
//一元运算符
int a = 3;
int b = a++; // b=3 a=4 先赋值,后自增
int c = ++a; // a=5 c=5 先自增,后赋值
System.out.println(a); // 5
System.out.println(b); // 3
System.out.println(c); // 5
//幂运算 2^3 2*2*2 = 8
//变 Math.pow(2,3) + alt + enter
double pow = Math.pow(2, 3);
System.out.println(pow); // 9.0
}
}
2)关系运算符
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果 : 正确,错误 布尔值
//if
int a = 10;
int b = 20;
int c = 21;
System.out.println(c%a); //1 21/10=2````1
System.out.println(a>b); //false
System.out.println(a<b); //true
System.out.println(a==b); //false
}
}
3)逻辑运算符
package operator;
//逻辑运算符
public class Demo05 {
public static void main(String[] args) {
// 与 或 非
boolean a = true;
boolean b = false;
System.out.println("a && b"+(a && b)); //false
System.out.println("a || b:"+(a||b)); //true
System.out.println("!(a && b):"+!(a&&b)); //true
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4); //这里等于错,不执行c++
System.out.println(d); //false
System.out.println(c); // 5
}
}
4)位运算符
package operator;
public class Demo06 {
public static void main(String[] args) {
/*
A= 0011 1100
B= 0000 1101
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~B = 1111 0010
2*8 = 16 2*2*2*2
最快 位运算 效率极其高
<< 左移
>> 右移
*/
System.out.println(2<<3);
}
}
5)条件运算符
package operator;
//三元运算符 条件
public class Demo08 {
public static void main(String[] args) {
// x ? y : z
// 如果 x == true,则结果为 y, 否则结果为 z
int score = 80;
String type = score < 60?"不及格":"及格"; // 必须掌握
// if
System.out.println(type); //不及格
}
}
6)扩展运算符
package operator;
//扩展运算符
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b; // a = a+b
a-=b;
System.out.println(a);
//字符串连接符 + , String 字符串类型
//+ 两边只要有一边有 字符串(String)类型,就会把他们连起来
System.out.println(a+b); //30
System.out.println(""+a+b); //1020
System.out.println(a+b+""); // 30 先加,然后运算
}