第一章06续-运算符续
运算符-续
一元运算符
package Operator;
public class Demo04 {
public static void main(String[] args) {
// ++ -- 自增,自减 一元运算符
int a = 3;
int b = a++;//执行完这行代码后,先给b赋值,在执行自增
// a++ a=a+1
System.out.println(a);
// ++a a=a+1
int c = ++a;// 执行完这行代码前,先给a自增,在给c赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
// 幂运算 2^3 = 8 很多运算,可以使用一些工具类来操作
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
输出:
4
5
3
5
8
位运算符
package com.DaoShl.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 异或:相同为0,不同为1;
~B = 1111 0010 非
-------------------------------------------
位运算 效率极高!!
2*8 = 16 怎么最快 --->2*2*2*2 = 16;
<<左移 *2
<<右移 /2
0000 0000 0
0000 0001 1
0000 0010 2
0000 0011 3
0000 0100 4
0000 1000 8
*/
System.out.println(2<<3);// 输出16
}
}
扩展值运算符(二元运算符)
package com.DaoShl.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;
// 字符串连接符 + String
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
条件运算符(三元运算符)
package com.DaoShl.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);
}
}

浙公网安备 33010602011771号