package operator;
public class Demo04 {
public static void main(String[] args) {
// ++ -- 自增,自减 一元运算符
int a =3;
int b =a++; //执行完这行代码前,先给b赋值=3,再自增a = a+1
System.out.println(a); // 4
// a++ a=a+1;
int c =++a; //++a a=a+1; 执行完这行代码前,先自增,再给b赋值
System.out.println(a);
System.out.println(a);
System.out.println(b);
System.out.println(b);
System.out.println(c);
System.out.println(c);
// 幂运算 2^3 2*2*2 = 8 很多运算, 我们会使用一些工具类来操作!
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
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
左移,右移 效率极高,和底层打交道
<< 左移 *2
>>右移 /2
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 8
*/
System.out.println(2<<3); // 2*2*2*2
}
}
package operator;
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
//偷懒操作
a+=b; //a = a+b
a-=b; //a = a-b
System.out.println(a);
//字符串连接符 + , String
System.out.println(a+b);
System.out.println(""+a+b); //如果在后面是拼接 1020
System.out.println(a+b+""); //如果在前面是正常相加减 30
}
}
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 ?"不及格":"及格"; // 必须掌握
System.out.println(type);
// 优先级 ()
}
}
评论一下吧
取消回复