1.算数运算符
算数运算符分为:+,-,*,/,%,++,--
System.out.println(5/2);//2,因为都是int类型,所以结果也是int类型,因此舍弃小数 System.out.println(5/2.0);//2.5,int类型除double类型结果是double类型 System.out.println(5%2);//1,商2余1 System.out.println((3%7);//3,小的对大的取余,余数是小的 System.out.println(8/2);//0,整除 int a=5,b=5; a++; ++b; System.out.println(a);//6 System.out.println(b);//6 int c=5,d=5; int e=c++;//c++值为5,加完后为6 int f=++d;//++值为6 System.out.println(c);//6 System.out.println(e);//5 System.out.println(d);//6 System.out.println(f);//6
2.关系运算符
关系运算符分为:>,>=,<,<=,==,!==
int a=5,b=10,c=5; boolean b1 = a>b; System.out.println(b1); //false System.out.println(c<b); //true System.out.println(a>=c); //true System.out.println(a<=b); //true System.out.println(a==c); //true System.out.println(a!=c); //false System.out.println(a%2==0); //false System.out.println(a+c>b); //false System.out.println(a++>5); //false------a自增1变为6 System.out.println(a++>5); //true-------a自增1变为7 System.out.println(a); //7
3.逻辑运算符
逻辑运算符分为:&& , || , !
int a=5,b=10,c=5; boolean b1 = b>=a && b<c; System.out.println(b1); //true&&false=false System.out.println(b<=c && b>a); //false&&true=false System.out.println(a==b && c>b); //false&&false=false System.out.println(b!=c && a<b); //true&&true=true int age = 99; System.out.println(age>=18 && age<=50); //年龄在18到50之间 int score = 86; System.out.println(score>=0 && score<=100); //成绩在0到100之间 //演示短路&&: boolean b3 = a>b && c++>2; System.out.println(b3); //false System.out.println(c); //5,发生短路了 //演示短路||: boolean b3 = a<b || c++>2; System.out.println(b3); //true System.out.println(c); //5,发生短路了 int a=5,b=10,c=5; boolean b2 = !(a<b); System.out.println(b2); //!true=false System.out.println(!(a>b)); //!false=true
4.赋值运算符
简单赋值运算符:=
复杂赋值运算符:+=,-=,*=,/=,%= 自带强转功能
int a = 5; a += 10; //相当于a=(int)(a+10) System.out.println(a); //15 a *= 2; //相当于a=(int)(a*2) System.out.println(a); //30 a /= 6; //相当于a=(int)(a/6) System.out.println(a); //5 //小面试题: short s = 5; //s = s+10; //编译错误,需强转,改为:s=(short)(s+10); s += 10; //相当于s=(short)(s+10);
5.条件运算符(三目运算符)
boolean?表达式1:表达式2;
int a=5,b=10; int max=a>b?a:b; System.out.println(max);//选最大值
6.字符串连接
字符串连接用:+
int age=20; System.out.println("我的年龄是:"+age);//我的年龄是:23
标签:false,int,System,运算符,println,out From: https://www.cnblogs.com/tian0000000000/p/17601717.html