Java五种运算符
程序员文章站
2023-12-25 23:56:33
...
五种运算符
算数运算符
- +;-;*;/;%
进行除法运算时应注意口诀(自己瞎想的,不喜勿喷)
整数相除取商数(/)
整数相除取余数(%)
整数相除只得整
要得小数必浮点
package Test2;
public class OperatorDemo {
public static void main(String[] args) {
int a = 3;
int b = 4;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
//整数相除取商数
System.out.println(a/b);
//整数相除取余数
System.out.println(a%b);
//整数相除只得整,要得小数必浮点
System.out.println(3.0/b);
System.out.println(a/4.0);
System.out.println(3.0/4.0);
}
}
- ++;–
++在前,先自加一再用
++在后,先用再自加一
–在前,先自减一再用
–在后,先用再自减一
package Test2;
public class OperatorDome2 {
public static void main(String[] args) {
int a = 8;
int b = ++a;//b=a+1
System.out.println("b: "+b);
int d = 8;
int c = d++;//c=d;d=d+1
System.out.println("c: "+c);
}
}
得到b=9;c=8
赋值运算符
- =;+=;-=;*=;/=
package Test2;
public class OperatorDemo3 {
public static void main(String[] args) {
int a = 10;
a += 5;//a=a+5
System.out.println("a:"+a);
a -= 5;//a=a-5
System.out.println("a:"+a);
a *= 5;//a=a*5
System.out.println("a:"+a);
a /=5;//a=a/5
System.out.println("a:"+a);
//隐含了强制转换,“=”右边默认是int类型
//相当于 b = (b的类型)(b+1)
byte b = 1;
b += 1;
System.out.println("b:"+b);
}
}
注意:赋值运算符实际上隐含了强制转换
关系运算符
- ==;!=;>;<;>=;<=(其结果都是boolean类型)
package Test2;
public class OperatorDemo4 {
public static void main(String[] args) {
int a = 3;
int b = 4;
int c = 3;
System.out.println(a==b);
System.out.println(a==c);
System.out.println("............");
System.out.println(a!=b);
System.out.println(a!=c);
System.out.println("............");
System.out.println(a>b);
System.out.println(a>c);
System.out.println("............");
System.out.println(a>=b);
System.out.println(a>=c);
System.out.println("............");
System.out.println(a<b);
System.out.println(a<c);
System.out.println("............");
System.out.println(a<=b);
System.out.println(a<=c);
System.out.println("............");
}
}
逻辑运算符
- &;|;^;!
有错(flase)则错(flase)【&】
有对(true)则对(true)【|】
相同则错(flase)【^】
不同则对(true)【^】
对就是错(flase)【!】
错(flase)就是对(true)【!】
package Test2;
public class OperatorDemo5 {
public static void main(String[] args) {
int a = 3;
int b = 4;
int c = 5;
System.out.println((a>b)&(a>c));//flase & flase
System.out.println((a<b)&(a>c));//true & flase
System.out.println((a>b)&(a<c));//flase & true
System.out.println((a<b)&(a<c));//true & true
System.out.println("............");
System.out.println((a>b)|(a>c));//flase | flase
System.out.println((a<b)|(a>c));//true | flase
System.out.println((a>b)|(a<c));//flase | true
System.out.println((a<b)|(a<c));//true | true
System.out.println("............");
System.out.println((a>b)^(a>c));//flase ^ flase
System.out.println((a<b)^(a>c));//true ^ flase
System.out.println((a>b)^(a<c));//flase ^ true
System.out.println((a<b)^(a<c));//true ^ true
System.out.println("............");
System.out.println(!(a>b));//!flase
System.out.println(!!(a>b));//!!flase
System.out.println("............");
}
}
-
&&;||
&&和&
相同点:结果一样
不同点:&&有短路效果,若左边为flase,则右边不执行
&无论左边是true或flase,右边都会执行||和|
相同点:结果一样
不同点:||有短路效果,若左边为true,则右边不执行
|无论左边是true或flase,右边都会执行
package Test2;
public class OperatorDemo6 {
public static void main(String[] args) {
int a = 3;
int b = 4;
int c = 5;
System.out.println((a>b)&&(a>c));//flase && flase
System.out.println((a<b)&&(a>c));//true && flase
System.out.println((a>b)&&(a<c));//flase && true
System.out.println((a<b)&&(a<c));//true && true
System.out.println("............");
System.out.println((a>b)||(a>c));//flase || flase
System.out.println((a<b)||(a>c));//true || flase
System.out.println((a>b)||(a<c));//flase || true
System.out.println((a<b)||(a<c));//true || true
System.out.println("............");
//&&与&的区别
int x = 6;
int y = 7;
System.out.println((x++>6)&(y++>7));
System.out.println("x:"+x);
System.out.println("y:"+y);
int x1 = 6;
int y1 = 7;
System.out.println((x1++>6)&&(y1++>7));
System.out.println("x1:"+x1);
System.out.println("y1:"+y1);
System.out.println("............");
//||与|的区别
int x2 = 6;
int y2 = 7;
System.out.println((x2++>=6)|(y2++>=7));
System.out.println("x2:"+x2);
System.out.println("y2:"+y2);
int x3 = 6;
int y3 = 7;
System.out.println((x3++>=6)||(y3++>=7));
System.out.println("x3:"+x3);
System.out.println("y3:"+y3);
System.out.println("............");
}
}
三元运算符
- 关系表达式?表达式1:表达式2
计算关系表达式的值看是true还是flase;
若是true则表达式1就是结果
若是flasez则表达式2就是结果
package Test2;
public class OperatorDemo7 {
public static void main(String[] args) {
int a = 3;
int b = 4;
int c = (a>b)?a:b;
System.out.println("c:"+c);
}
}