算数运算符:+,-,*,/,%(取余,模运算),++,–
package operator; public class YSHG1 { public static void main(String[] args) { //二元运算符 int a = 10; int b = 20; int c = 25; int d = 25; System.out.println(a+b);//输出30 System.out.println(a-b);//输出-10 System.out.println(a*b);//输出200 System.out.println(a/b);//输出0,因为是int型,所以需要进行强制转换 System.out.println(a/(float)b);//输出0.5 } } package operator; public class YSHG4 { public static void main(String[] args) { //取余运算(模运算) int a = 10; int b = 3; System.out.println(a%b);//输出1,10/3 = 3.....1 } } package operator; public class YSHG5 { public static void main(String[] args) { //++(自增)--(自减) (一元运算符) int a = 3; int b = a++;//执行完这行代码后,先给b赋值,再自增(从左到右) //此处应有a = a + 1; System.out.println(a);//输出4 System.out.println(b);//输出3 } } package operator; public class YSHG6 { public static void main(String[] args) { int a = 3; //此处应有a = a + 1; int c = ++a;//执行完这行代码前,先自增,再给c赋值(从左到右) System.out.println(a);//输出4 System.out.println(c);//输出4 } } package operator; public class YSHG2 { public static void main(String[] args) { long a = 123123123123L; int b = 123; short c = 10; byte d = 8; System.out.println(a+b+c+d);//输出123123123264(long)有一个数是long型,结果也是long型 System.out.println(b+c+d);//输出141(int)没有long型,结果都是int型 System.out.println(c+d);//输出18(int)没有long型,结果都是int型 //如果有一个数是double型,结果是double型 } } package operator; public class YSHG7 { public static void main(String[] args) { //幂运算 double pow = Math.pow(2, 3);//工具类,很多运算需要用工具类来操作 System.out.println(pow);//输出8.0 } }赋值运算符:=
关系运算符:>,<,>=,<=,==(等于),!=instanceof(不等于)
package operator; public class YSHG3 { public static void main(String[] args) { //关系运算符返回的结果:正确,错误(布尔值表示) int a = 10; int b = 20; System.out.println(a>b);//输出false System.out.println(a<b);//输出ture System.out.println(a==b);//输出false System.out.println(a!=b);//输出ture } }逻辑运算符:&&(与) ,||(或),!(非,取反)
package operator; public class YSHG8 { public static void main(String[] args) { //逻辑运算符 boolean a = true; boolean b = false; System.out.println("a && b:"+(a&&b));//逻辑与运算:两个变量都为真,结果才为true System.out.println("a || b:"+(a||b));//逻辑或运算:两个变量有一个为真,结果就为true System.out.println("! (a && b):"+!(a&&b));//逻辑非运算:变量是真,结果为假;变量是假,结果为真 //短路运算:前一个变量是错的就停止执行运算 int c = 5; boolean d = (c<4)&&(c++<4); System.out.println(d);//输出false System.out.println(c);//输出5,证明&&后面没有运算 } }位运算符:&(与),|(或),^(异或),~(非),>>(左移),<<(右移),>>>(右移且把高位补0)
package operator; public class YSHG9 { public static void main(String[] args) { //位运算 /* A = 0011 1100 B = 0000 1101 A&B = 0000 1100(两个都是1,输出1) A|B = 0011 1101(有一个是1,输出1) A^B = 0011 0001(两个相同,输出0) ~B = 1111 0010(输出相反的) */ /* 2*8 = 16用位运算效率高 << *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);//输出16 } }条件运算符:? :
package operator; public class YSHG11 { public static void main(String[] args) { //三元运算符(必须掌握) //x ? y : z(如果x==true,结果为y,否则结果为z) int score = 80; String type = score < 60 ? "不及格" : "及格"; System.out.println(type);//输出及格 } }扩展赋值运算符:+=(a = a + b),-=(a = a - b),*=,/=
package operator; public class YSHG10 { public static void main(String[] args) { int a = 10; int b = 20; a+=b;//a = a + b /* a-=b;//a = a - b*/ System.out.println(a); //字符串连接符:+,转为String System.out.println(""+a+b);//输出1020 System.out.println(a+b+"");//输出30(从左到右) } }