1、Java语言支持如下运算符
-
算数运算符:+、 -、 * 、/、 % 、++ 、--
-
赋值运算符:=
-
关系运算符:>、<、>=、<=、==、!=、instanceof
-
逻辑运算符:&&、||、!
-
以下了解即可
-
位运算符:&、|、^、~、>>、<<、>>>
-
条件运算符:? 、:
-
扩展赋值运算符:+=、-=、*=、/=
public class Demo6 {
public static void main(String[] args) {
//二元运算符(2个变量运算称为2元)
int a =10;
int b =20;
int c =25;
int d =30;
System.out.println(a+b);//30
System.out.println(a-b);//-
System.out.println(a*b);//200
System.out.println(a/b);//0 注意,值有可能为小数,这里需要强转
System.out.println(a/(double) b);//0.5
System.out.println("===========================");
long aa = 2132423423L;
short bb = 111;
byte cc = 22;
int dd = 343;
//关于运算后结果类型的注意
//如果运算类型中包含Long,那就是以Long为输出类型
System.out.println(aa+bb+cc+dd);
System.out.println(bb+cc+dd);
//如果为int 那就是以int为输出类型,因byte范围127之内
System.out.println(cc+dd);
System.out.println("===========================");
//关系运算符
int aaa = 10;
int bbb = 20;
int ccc = 21;
System.out.println(aaa == bbb);
System.out.println(aaa >= bbb);
System.out.println(aaa <= bbb);
System.out.println(aaa != bbb);
//取余 取2留1
System.out.println(ccc%aaa);//1 21/10=2...1
}
}
public class Demo7 {
public static void main(String[] args) {
//一元运算符 (1个变量运算)
int a = 11;
int b = a++;//这段代码先给b赋值a,再自增
int c = ++a;//a先自增为12,这里是先自增13,所有c是13
System.out.println(a);//13
System.out.println(b);//11
System.out.println(c);//13
//幂运算 2*2*2=8 a^3 很多运算借助工具
//2的3次方
double pow = Math.pow(2, 3);
System.out.println(pow);//8.0
}
}
public class Demo8 {
public static void main(String[] args) {
//与(and) 或(or) 非(取反)
boolean flag1 = true;
boolean flag2 = false;
System.out.println((flag1&&flag2));//false 俩个结果都为真,结果true.
System.out.println((flag1||flag2));//true 俩个结果有一个为真,结果true.
System.out.println(!(flag1&&flag2));//true 如果为真,变为假,如果为假,变为真。
//短路运算
int c = 4;
boolean flag3 = (c > 5) && (c++<5);
System.out.println(flag3);//flase
System.out.println(c);//4
}
}
public class Demo9 {
public static void main(String[] args) {
int a =10;
int b =20;
a+=b;
System.out.println(a);//30
a-=b;
System.out.println(a);//10
//字符串连接符 “”
System.out.println(a+b+"");//字符串在后面没有效果
System.out.println(""+a+b);//把a的值和b的值在字符串中拼接在了一起
}
}
public class Demo10 {
public static void main(String[] args) {
// x ? y : z
//如果x为true,输出y,如果为false,输出z
int a = 55;
String type = a < 60 ? "不及格" : "及格";
System.out.println(type);//不及格
int aaa = a < 60 ? 1:0;
System.out.println(aaa);// 1
}
}


