运算符:运算的符号
算术:+,-,*,/,%,++,–
%:取模/取余,余数为0即为整除
System.out.println(5%2);//1,商2余1
System.out.println(8%2);//0,商4余1----整除
System.out.println(2%8);//2,商0余2
++/–:自增/自减1,可在变量前也可在变量后
单独使用时,在前在后都一样:
int a = 5,b = 5;
a++;//相当于a=a+1
++b;//相当于b=b+1
System.out.println(a);//6
System.out.println(b);//6
被使用时,在前在后不一样:
- a++的值为a
- ++a的值为a+1
int a = 5,b = 5;
int c = a++;//将a++的值5赋给c,a自增1变为6
int d = ++b;//将++b的值6赋给d,b自增1变为6
System.out.println(a);//6
System.out.println(b);//6
System.out.println(c);//5
System.out.println(d);//6
注:–与++同理
int a = 5,b = 5;
a--;//相当于a=a-1
--b;//相当于b=b-1
System.out.println(a);//4
System.out.println(b);//4
int a = 5,b = 5;
int c = a--;//将a--的值5赋给c,a自减1变为4
int d = ++b;//将--b的值4赋给d,b自减1变为4
System.out.println(a);//4
System.out.println(b);//4
System.out.println(c);//5
System.out.println(d);//4
关系:>,<,>=,<=,==,!=
关系运算的结果为boolean型,关系成立为true,不成立为false
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+c>b); //false
System.out.println(a%2==0); //false
System.out.println(c++>5); //false-------c自增1变为6
System.out.println(c++>5); //true--------c自增1变为7
逻辑:&&,||,!
&&:短路与(并且),两边都为真则为真,见false则false
当第1个条件为false时,发生短路(后面的不执行了)
||:短路或(或者),有真则为真,见true则true
当第1个条件为true时,发生短路(后面的不执行了)
!:逻辑非(取反),非真则假,非假则真
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 = 25;
System.out.println(age>=18 && age<=50); //看age是否在18到50之间
//||的演示:
System.out.println(b>=a || b<c); //true||false=true
System.out.println(b<=c || b>a); //false||true=true
System.out.println(b!=c || a<b); //true||true=true
System.out.println(a==b || c>b); //false||false=false
int score = 89;
System.out.println(score<0 || score>100); //看score是否不合法
//!的演示
boolean b2 = !(a<b);
System.out.println(b2); //!true=false
System.out.println(!(a>b)); //!false=true
//短路的演示
int a=5,b=10,c=5;
boolean b3 = a>b && c++>2;
System.out.println(b3); //false
System.out.println(c); //5,发生短路了
boolean b4 = a<b || c++>2;
System.out.println(b4); //true
System.out.println(c); //5,发生短路了
赋值:=,+=,-=,*=,/=,%=
- 简单赋值运算符:=
- 扩展赋值运算符:+=,-=,*=,/=,%=
注:扩展赋值自带强转功能
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)
字符串连接:+
+:
- 若两边为数字,则做加法运算
- 若两边出现了字符串,则做字符串连接
- 任何类型与字符串相连,结果都会变为字符串类型----同化作用
//字符串拼接演示
int age = 20;
System.out.println("age="); //age=
System.out.println(age); //20
System.out.println("age="+age); //age=20
System.out.println("我的年龄是"+age); //我的年龄是20
System.out.println("我今年"+age+"岁了"); //我今年20岁了
String name = "ZQ";
System.out.println("name="+name); //name=ZQ
System.out.println("大家好,我叫"+name); //大家好,我叫ZQ
System.out.println("大家好,我叫"+name+",今年"+age+"岁了"); //大家好,我叫ZQ,今年20岁了
//同化作用演示
System.out.println(10+20+30+""); //60---------String
System.out.println(10+20+""+30); //3030-------String
System.out.println(""+10+20+30); //102030-----String
条件/三目:?:
语法:boolean?数1:数2
执行过程:
- 注:整个表达式是有值的,它的值要么是?号后的数1,要么是:号后的数2
计算boolean的值: - 若为true,则整个表达式的值为?号后的数1
- 若为false,则整个表达式的值为:号后的数2
int num = 5;
int flag = num>0?1:-1;
System.out.println(flag); //1
int a=8,b=55;
int max = a>b?a:b;
System.out.println("max="+max);