2、自增自减运算符
a++:先运算,后自增
a–:先运算,后自减
++a:先自增,后运算
–b:先自减,后运算
public class OperatorTest02 {
public static void main(String[] args) {
int a = 1;
int b;
System.out.println("a = " + a); // a = 1
b = ++a * 2;
System.out.println("a = " + a + ", b = " + b); // a = 2, b = 4
b = a++ * 2;
System.out.println("a = " + a + ", b = " + b); // a = 3, b = 4
int c = a++ * (++a); // 3 * (3 + 1 + 1) = 15 a = 3 --> a = 3 + 1 --> a = 4 + 1
System.out.println("a = " + a + ", c = " + c); // a = 5, c = 15
a = 5;
b = 1;
int sum = a + (b + 1); // 优先级,先是b + 1 = 1 + 1 = 2, 再是 a + (b + 1) = 5 + 2 = 7, 最后sum = 7
System.out.println("a = " + a + ", b = " + b + ", sum = " + sum); // a = 5, b = 1, sum = 7
sum = a + (++b); // b = 1 + 1 = 2 --> 5 + 2 = 7 --> sum = 7
System.out.println("a = " + a + ", b = " + b + ", sum = " + sum); // a = 5, b = 2, sum = 7
}
}
javac -encoding utf-8 -g OperatorTest02.java
java OperatorTest02
3、比较运算符
“>” 、>= 、<、<=、==、!=
import java.util.Scanner;
/**
*身高比较器,演示比较运算符案例
*@author zhang.fuqing
*@version 1.0
*@date 2018-09-16 07:27:35
*
**/
public class ComparisonHeight {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入张三的身高(以CM为单位,整型值):");
int zhangSanHeight = scanner.nextInt();
System.out.println("请输入李四的身高(以CM为单位,整型值):");
int liSiHeight = scanner.nextInt();
System.out.println("张三的身高是否等于李四的身高?" + (zhangSanHeight == liSiHeight));
System.out.println("张三的身高是否大于李四的身高?" + (zhangSanHeight > liSiHeight));
System.out.println("张三的身高是否小于李四的身高?" + (zhangSanHeight < liSiHeight));
}
}
javac -encoding utf-8 -g ComparisonHeight.java
java ComparisonHeight
4、逻辑运算符
&:逻辑与 true&true=true true&false=false false&true=false false&false=false
&&:短路与 A&&B,先判断A,如果A为false,则输出结果false,如果A为true,则再判断B……
|:逻辑或 true|true=true true|false=true false|true=true false|false=false
||:短路或 A||B,先判断A,如果A为true,则输出结果true,如果A为false,则再判断B……
!:逻辑非
^:异或
public class LogicOperator {
public static void main(String[] args) {
int a = 1;
int b = 2;
boolean flag = a > b & b++ >= 2; // &
System.out.println("flag = " + flag + ", a = " + a + ", b = " + b); // flag = false, a = 1, b = 3
flag = a > b && b++ >= 2; // &&
System.out.println("flag = " + flag + ", a = " + a + ", b = " + b); // flag = false, a = 1, b = 3
a = 1;
b = 2;
flag = a < b | b++ >= 2; // |
System.out.println("flag = " + flag + ", a = " + a + ", b = " + b); // flag = true, a = 1, b = 3
a = 1;
b = 2;
flag = a < b || b++ >= 2; // ||
System.out.println("flag = " + flag + ", a = " + a + ", b = " + b); // flag = true, a = 1, b = 2
}
}
javac -encoding utf-8 -g LogicOperator.java
java LogicOperator
5、三元运算符
布尔表达式 ? 结果1 : 结果2;
如果布尔表达式为true,则输出结果1,否则输出结果2。
public class TernaryOperator {
public static void main(String[] args) {
int a = 1;
int b = 2;
int returnValue = a < b ? a + 1 : a + 100;
System.out.println("returnValue = " + returnValue + ", a = " + a + ", b = " + b);
returnValue = a > b ? a + 1 : a + 100;
System.out.println("returnValue = " + returnValue + ", a = " + a + ", b = " + b);
returnValue = a < b ? (a += 1) : (a += 100); // 符合优先级:小括号>三元运算符 (a += 1) --> 2
// returnValue = a < b ? a += 1 : a += 100; 返回语句a += 1,报错
System.out.println("returnValue = " + returnValue + ", a = " + a + ", b = " + b);
returnValue = a > b ? (a += 1) : (a += 100); // 符合优先级:小括号>三元运算符
System.out.println("returnValue = " + returnValue + ", a = " + a + ", b = " + b);
}
}
javac -encoding utf-8 -g TernaryOperator.java
java TernaryOperator