运算符:
计算机最重要的东西就是计算,计算最重要的就是运算符。
Java语言支持的运算符:
算术运算符:+(加),-(减),*(乘),/(除),%(模运算,取余),++,–
二元运算符:加,减,乘,除,模运算
//二元运算符
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/b);//a和b都是int型,所以结果也是int型,四舍五入就是0
System.out.println(a/(double)b);//结果出现小数时要注意作用范围
System.out.println(a%b);
}
}
一元运算符:自增,自减
//一元运算符
public class Demo04 {
public static void main(String[] args) {
//++自增,--自减
int a = 3;
int b = a++; //执行完这行代码后先给b赋值,再自增
//a = a+1;
System.out.println(a); //4
//a = a+1
int c = ++a;//执行这行代码前先自增,再给c赋值
System.out.println(a); //5
System.out.println(b); //3
System.out.println(c); //5
//幂运算 2^3 2*2*2 = 8 Java没有^运算符 很多运算我们都会使用一些工具类进行操作。
double pow = Math.pow(2, 3);
System.out.println(pow);
}
}
返回类型:
//返回类型
public class Demo02 {
public static void main(String[] args) {
long a = 413413413309309309L;
int b = 4;
short c = 3;
byte d = 9;
float e = 0.39F;
double f = 4.13;
char g = 'g';
boolean h = true;
//有一个为long结果为long,没有long时则返回int类型
System.out.println(a+b+c+d); //long
System.out.println(b+c+d); //Int
System.out.println(c+d); //Int
System.out.println(e+f);
System.out.println(e+g);
System.out.println(a+b+c+d+e+f);
//System.out.println(a+b+h); //boolean不能参与运算
//cast转换
}
}
赋值运算符:=(赋值运算符)
int a = 10 //把10赋值给a
关系运算符:>(大于),<(小于),>=(大于等于),<=(小于等于),==(等于),!=(不等于),instanceof(二元运算符)
关系运算符返回的结果:
//关系运算符返回的结果:正确(true)、错误(false) 布尔值
public class Demo03 {
public static void main(String[] args) {
//后面会和if一起使用
int a = 10;
int b = 20;
System.out.println(a>b); //false
System.out.println(a==b); //false
System.out.println(a<b); //true
System.out.println(a!=b); //true
}
}
逻辑运算符:&&(与),||(或),!(非)
//逻辑运算符
public class Demo05 {
public static void main(String[] args) {
//与&&(and),或||(or),非!( && ) (取反)
boolean a = true;
boolean b = false;
System.out.println("a && b:"+(a&&b)); //逻辑与运算:两个变量为真结果为真
System.out.println("a || b:"+(a||b)); //逻辑或运算:两个变量有一个为真结果为真
System.out.println("!(a && b):"+!(a&&b)); //逻辑非运算:如果是真,则为假,如果是假则为真
//短路运算
System.out.println("b&&a:"+(b&&a));
//一上来就发现b是假的,所以不用管后面了,就知道这个语句是假的,后面就不需要执行了
//下面我们实验检测一下后面的语句没执行
int c = 5; //定义变量c
boolean d = (c<4)&&(c++<4); //定义变量d,前半部分是false,后面的加号没走
System.out.println(c); //如果上面一条语句的后半部分执行了,那c的值就是6了
System.out.println(d);
}
}
位运算符:&(与),|(或),^(异或),~(非-取反),>>,<<,>>>(了解一下!!!)
//位运算
public class Demo06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
A&B = 0000 1100 11则1,否则得0
A|B = 0011 1101 如果对应位都是0,结果位0,否则为1
A^B = 0011 0001 如果两个位置相同则为0,否则为1
~B = 1111 0010 和该数完全相反
2*8=16 2*2*2*2
<< 左移-相当于乘以二
>> 右移-相当于除以二
位运算的好处:效率极高
二进制的思路:
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
*/
System.out.println(2<<3);
}
}
条件运算符: ? :
//三元运算符
public class Demo08 {
public static void main(String[] args) {
//x ? y : z
//如果x==true,则结果为y,否则结果为z
int score = 80;
String type = score < 60 ?"不及格":"及格" ; //用String接受它返回的类型
System.out.println(type);
}
}
扩展赋值运算符:+=,-=,*=,/=
//扩展赋值运算符
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b; //a=a+b
System.out.println(a);
a-=b; //a=a-b
System.out.println(a);
//字符串连接 +
//只要在加号运算符一侧出现了“ ”(字符串类型),它就会把其他值都转换为字符串进行连接
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+"");//字符串在后面,前面依旧会先运算
}
}
运算符优先级
由左到右一次结合运算
赋值运算符最低
一元算术运算符>二元算术运算符(先乘除后加减)>位运算符
有括号先算括号里面的(),我们在日常应用时,可以多使用括号来增强代码的可读性。
//运算符优先级
public class Demo09 {
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = 3;
int d = ++a*b;
System.out.println(d);
int e = a*(b+c)+d;
System.out.println(e);
}
}