Java语言支持如下运算符
- 算术运算符:+,-,*,/,%(模,就是余数),++,–
- 赋值运算符:=
- 关系运算符:>,<,>=,<=,==,!=, instanceof(比较继承),equals(比较字符串)
- 逻辑运算符:&&(与),||(或),!(非)
- 位运算符:&,|,^,~,>>,<<,>>>
- 条件运算符:?:
- 扩展赋值运算符:+=,-=,*=,/=
注意事项
public class Demo01 {
public static void main(String[] args) {
//二元运算符
int a =10;
int b =20;
int c =22;
int d =25;
System.out.println(a+b);// 30 true
System.out.println(a-b);// -10 true
System.out.println(a*b);// 200 true
System.out.println(a/b);// 0 false int类型用/运算只取商,小数部分舍去
System.out.println(a/(double)b);// 0.5 true 如果除法运算有小数时,要保留小数应该将其中一个数转换为浮点数double
System.out.println(c%b);// 2 取余 模运算
//幂运算 :Java 中有很多运算,我们会使用一些工具类来操作!
double pow = Math.pow(10, 3); //Math.pow(x,y); 按alt+回车
System.out.println(pow); //10^3=1000
}
}
public class Demo02 {
public static void main(String[] args) {
long a = 12324412312331L;
int b = 2311;
short c = 10 ;
byte d = 4 ;
System.out.println(a+b+c+d);//long
System.out.println(b+c+d);//int
System.out.println(c+d);//int 系统混合运算时自动转化为int,防止溢出
}
}
public class Demo04 {
public static void main(String[] args) {
//++(自增) --(自减) 一元运算符
int a = 3;
int b = a++;// ++在后,先把a赋值给b,再运算a自增1(a=a+1)
int c = ++a;// ++在前,先运算a自增1(a=a+1),再把值赋给c
System.out.println(a);// 5 执行两次自增1,最后值为5
System.out.println(b);// 3 因++在后面,先把a赋值给b,a再自增1为4
System.out.println(c);// 5 因++在前面,a先自增1为5,再赋值给c
// 逻辑运算符
public class Demo05 {
public static void main(String[] args) {
//与(and) 或(or) 非(取反)
boolean a = true;
boolean b = false;
System.out.println("a&&b:"+(a&&b));// a&&b:false》逻辑与运算:两个变量都为真,结果才为true,否则为false
System.out.println("a||b:"+(a||b));// a||b:true》逻辑或运算:只要有一个变量为真,结果就为true
System.out.println("!(a&&b):"+!(a&&b));// !(a&&b):true》如果为真,则变为假;如果为假,则变为真
System.out.println("=========================");
//短路运算(当前面变量判断出整个逻辑的真假时,则中止后面的运算,直接输出真或者假)
int c = 5;
boolean d = (c<4)&&(++c<4);//当前面的变量为false时,中止后面的运算,所以c还是没变
System.out.println(d);// false
System.out.println(c);// 5
boolean e = (c++<4)&&(c--==4);
System.out.println(e);// false
System.out.println(c);// 6
}
}
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>>>16
* */
System.out.println(2<<3);
}
}
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;// a=a+b
System.out.println(a);// 30
a-=b;// a=a-b
System.out.println(a);// 10
public class Demo07{
public static void main(String[] args){
int a = 10;
int b = 20;
//字符串连接符 + 转换为String
System.out.println(a+b);// 30
System.out.println(""+a+b);// 1020 (""在前面,直接拼接字符)
System.out.println(a+b+"");// 30 (""在后面,先运算,再拼接字符)
}
}
public class Demo07{
public static void main(String[] args){
// x ? y : z
//如果x为true,则为y,为false,则为z
int score = 80;
String tybe = score>60 ? "及格" : "不及格";
System.out.println(tybe);// 及格
}
}