运算符
算术运算符
//二元运算符
//ctrl + d ;复制当前行到下一行
int a = 10;
int b = 20;
int c = 25;
int d = 25;
System.out.println(a+b);//30
System.out.println(a-b);//-10
System.out.println(a*b);//200
System.out.println(a/(double)b);//0.5
long a = 123123123123123L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);//123123123123264 long类型
System.out.println(b+c+d);//141 int类型
System.out.println(c+d);//18 int类型
%(模运算)
int a = 10;
int c = 21;
//取余 ,模运算
System.out.println(c%a);//(1) c/a 21/10 = 2 ...1
++ ——
//++ -- 自增 自减 一元运算符
int a = 3;
int b = a++;//执行完这行代码后,先给b赋值,在自增
// a = a + 1
System.out.println(a);
// a = a + 1
int c = ++a;// 执行完这行代码前,先自增,再给c值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算2^3 2*2*2 = 8 很多运算,我们会使用一些工具类来操作
double pow = Math.pow(3,2);
Stystem.out.println(pow);//8.0
逻辑运算符
//与(and)或(or)非(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b:"+*(a&&b));//逻辑与运算:两个变量都为真,结果才为ture
System.out.println("a || b:"+*(a||b));//逻辑或运算:两个变量有一个为真,结果才为ture
System.out.println("!(a && b):"+*!(a&&b));//逻辑与运算:如果是真,则变假,如果是假则为真
短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);//c<4已经错了,所以后面不计算。
System.out.println(d);//false
System.out.println(c);//5
位运算
/*A = 0011 1100;
B = 0000 1101;
A&B = 0000 1100;//如果两个都为1则为1
A|B = 0011 1101;//如果一个是1则为1
A^B = 0011 0001;//如果相同则为1
~B = 1111 0010;//取相反值
2*8 = 16 2*2*2*2 = 16
<<(左移) >>(右移)
0000 0000 0
0000 0001 1
0000 0010 2
0000 0100 4
0000 1000 8
*/
System.out.println(2<<3);//8
偷懒运算符
int a = 10;
int b = 20;
a+=b;//a = a+b
a-=b;//a = a-b
System.out.println(a);
三元运算符
//x ? y : z//如果x==true,则结果为y,否则结果为zint score = 80;String type = score < 60 ? "不及格" : "及格";//必须掌握//ifSystem.out.println(type);//及格
***字符串连接符 ***
int a = 10;int b = 20;//字符串连接符 + ,StingSystem.out.println(""+a+b);//1020 运算在后面就直接输出System.out.println(a+b+"");//30 运算在前面就运算