package operator;
public class Demo04 {
public static void main(String[] args) {
//++ --自增,自减 一元运算符
int a = 3;
int b = a++;//执行完这行代码后,先给b赋值,再自增
//a=a+1;
System.out.println(a);
//a++ a = a+1;
int c = ++a;//执行完这行代码前,先自增,宣布给b赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算 2^3 2*2*2 = 8 很多运算,我们会使用一些工具类来操作!
double pow = Math.pow(3,2);
System.out.println(pow);
}
}
运行结果:
4
5
3
5
9.0
package operator;
public class Demo05 {
public static void main(String[] args) {
//或(or) 与 (and) 非(取反)
boolean a = true;
boolean b = false;
System.out.println("a && b:"+(b&&a));//逻辑与运算:两个变量都为真,结果才为true
System.out.println("a || b:"+(b||a));//逻辑与运算:两个变量有一个为真,则结果就为true
System.out.println("!(a && b):"+!(b&&a));//如果是真,则变为假,如果为假,则变为真
//短路运算
int c = 5;
Boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
}
}
运算结果:
a && b:false
a || b:true
!(a && b):true
false
5
package operator;
public class Demo06 {
public static void main(String[] args) {
//&运算 11为1,其他为0; |运算 00为0,其他为1;^运算 10为1,其他为0;~运算 相反即可
System.out.println(2<<3);
}
}
运算结果:
16
扩展运算符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 eg:如果字符串在前面会进行拼接,如果字符串在后面前面依旧会进行运算
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
运算结果:
10
1020
30
package operator;
//三元运算符
public class Demo08 {
public static void main(String[] args) {
//x ? y : z
//如果x==true,则结果为y,否则结果为z
int score = 50;
String type = score < 60?"不及格":"及格";//必须掌握
//if
System.out.println(type);
}
}
运算结果:
不及格



