Logical operator: XOR^
boolean a,b;
boolean c = a ^ b;
Bitwise operators &, |, ^, ~, <<, >>, >>>.
&, |, ^ Perform operator judgment based on the data type of the operation. Is it a logical operator or a bitwise operator?
&: bitwise and
|: bitwise or
~: bitwise not
^: bitwise exclusive or
//位运算符实现数值类型值的交换
int a = 10, b = 20;
a = a ^ b;
b = a ^ b;
a = a ^ b;
<<: shift left
>>: shift right
>>>: shift right without sign
Conditional operator (ternary operator)
- Format
** Conditional expression? Expression 1: expression 2; - Description
- The result of the conditional expression is boolean type
- If the result of the conditional expression is true, expression 1 is executed, otherwise, expression 2 is executed.
- The types of the results returned by expressions 1 and 2 must be unified into one type.
- Ternary operators can be nested.
- Anything that uses ternary operators can be rewritten into if-else form, but not vice versa, because 3.
//输出三个值的最大值
//输出三个值的最大值
int a = 10, b = 20, c = 30;
int max = (a > b) ? a : ((b > c) ? b : c);
System.out.println(max);