标签:
操作符 | 描述 |
= | 取右边的值 |
1 class Demo{ 2 public static void main(String[] args){ 3 int a = 7;//将右边的值赋给左边的变量 4 System.out.println(a);//打印输出变量a 5 } 6 }
打印结果如下:
7
操作符 |
描述 |
+ | 加 |
- | 减 |
* | 乘 |
/ | 除 |
% | 取余 |
1 class Demo{ 2 public static void main(String[] args){ 3 int a = 8; 4 int b = 6; 5 int c = a + b;//将变量a+b的和赋值给变量c 6 System.out.println("a + b = " + c); 7 c = a - b;//将变量a-b的差赋值给变量c 8 System.out.println("a - b = " + c); 9 c = a * b;//将变量a*b的积赋值给变量c 10 System.out.println("a * b = " + c); 11 c = a / b;//将变量a/b的商赋值给变量c 12 System.out.println("a / b = " + c); 13 c = a % b;//取变量a除以变量b的余数 14 System.out.println("a / b = " + c); 15 } 16 }
打印结果如下:
a + b = 14
a - b = 2
a * b = 48
a / b = 1
a / b = 2
注意:
操作符 | 描述 |
++ | 递增 |
-- | 递减 |
1 class Demo{ 2 public static void main(String[] args){ 3 int a = 5; 4 int b = 7; 5 int c = a++;//先将变量a的值赋给变量c,然后变量a自增1,此时a=6 6 System.out.println("a = " + a + " c = " + c); 7 c = ++a;//先将变量a的值自增1,然后将值赋给变量c,此时a=7 8 System.out.println("a = " + a + " c = " + c); 9 int d = b--;//先将变量b的值赋给变量d,然后变量b自减1,此时b=6 10 System.out.println("b = " + b + " d = " + d); 11 d = --b;//先将变量b的值自减1,然后将值赋给变量d,此时b=5 12 System.out.println("b = " + b + " d = " + d); 13 } 14 }
打印结果如下:
a = 6 c = 5
a = 7 c = 7
b = 6 d = 7
b = 5 d = 5
操作符 | 描述 |
< | 小于 |
> | 大于 |
<= | 小于等于 |
>= | 大于等于 |
== | 等于 |
!= | 不等于 |
1 class Demo{ 2 public static void main(String[] args){ 3 int x = 47; 4 int y = 49; 5 System.out.println("x < y = " + (x < y)); 6 System.out.println("x > y = " + (x > y)); 7 System.out.println("x <= y = " + (x <= y)); 8 System.out.println("x >= y = " + (x >= y)); 9 System.out.println("x == y = " + (x == y)); 10 System.out.println("x != y = " + (x != y)); 11 } 12 }
打印结果为:
x < y = true
x > y = false
x <= y = true
x >= y = false
x == y = false
x != y = true
注意:
操作符 | 描述 |
&& | 与 |
|| | 或 |
! | 非 |
1 class Demo{ 2 public static void main(String[] args){ 3 boolean t = true; 4 boolean f = false; 5 System.out.println(t&&t);//当两边值都为true时则返回true 6 System.out.println(t&&f);//否则为flase 7 System.out.println(f&&f); 8 System.out.println(f||f);//当两边边都为false时则返回false 9 System.out.println(t||t);//否则为true 10 System.out.println(t||f); 11 System.out.println(!t);//true则返回flase 12 System.out.println(!f);//false则返回true 13 } 14 }
注意:
操作符 | 描述 |
& | 按位与 |
| | 按位或 |
^ | 按位异或 |
~ | 按位非 |
按位与运算:当两边二进制都为1时结果为1
值1(十进制) | 00000000 00000000 00000000 00000111(7) | 00000000 00000000 00000000 00000110(6) |
值2(十进制) | 00000000 00000000 00000000 00000011(3) | 00000000 00000000 00000000 00000101(5) |
结果(十进制) | 00000000 00000000 00000000 00000011(3) | 00000000 00000000 00000000 00000100(4) |
按位或运算:当两边二进制都为0时结果为0
值1(十进制) | 00000000 00000000 00000000 00001001(9) | 00000000 00000000 00000000 00010001(17) |
值2(十进制) | 00000000 00000000 00000000 00000110(6) | 00000000 00000000 00000000 00000011(3) |
结果(十进制) | 00000000 00000000 00000000 00001111(15) | 00000000 00000000 00000000 00010011(19) |
按位异或运算:当两边二进制都为1时结果为0
值1(十进制) | 00000000 00000000 00000000 00001111(15) | 00000000 00000000 00000000 00000111(7) |
值2(十进制) | 00000000 00000000 00000000 00001001(9) | 00000000 00000000 00000000 00000101(5) |
结果(十进制) | 00000000 00000000 00000000 00000110(6) | 00000000 00000000 00000000 00000010(2) |
按位非运算:二进制值1为0,0为1
值(十进制) | 00000000 00000000 00000000 00001010(10) |
结果(十进制) | 11111111 11111111 11111111 11110101(-11) |
1 class Demo{ 2 public static void main(String[] args){ 3 System.out.println("7 & 3 = " + (7&3)); 4 System.out.println("6 & 5 = " + (6&5)); 5 System.out.println("9 | 6 = " + (9|6)); 6 System.out.println("17 | 3 = " + (17|3)); 7 System.out.println("15 ^ 9 = " + (15^9)); 8 System.out.println("7 ^ 5 = " + (7^5)); 9 System.out.println("~10 = " + (~10)); 10 } 11 }
打印结果如下:
7 & 3 = 3
6 & 5 = 4
9 | 6 = 15
17 | 3 = 19
15 ^ 9 = 6
7 ^ 5 = 2
~10 = -11
操作符 | 描述 |
<< | 左移 |
>> | 右移 |
>>> | 无符号右移 |
左移:将操作数的二进制向左移动指定位数(空位补0)
操作数(十进制) | 00000000 00000000 00000000 00000111(7) | 00000000 00000000 00000000 00000101(5) |
左移位数 | 3 | 4 |
结果(十进制) | 00000000 00000000 00000000 00111000(56) | 00000000 00000000 00000000 01010000(80) |
右移:将操作数的二进制向右移动指定位数(按最高位补位)
操作数(十进制) | 00000000 00000000 00000000 00001111(15) | 11111111 11111111 11111111 11110001(-15) |
右移位数 | 3 | 4 |
结果(十进制) | 00000000 00000000 00000000 00000001(1) | 11111111 11111111 11111111 11111111(-1) |
无符号又移动:将操作数的二进制向右移动指定位数(空位补0)
操作数(十进制) | 11111111 11111111 11111111 11111110(-2) |
右移位数 | 3 |
结果(十进制) | 00011111 11111111 11111111 11111111(536870911) |
1 class Demo{ 2 public static void main(String[] args){ 3 System.out.println("7 << 3 = " + (7<<3)); 4 System.out.println("5 << 4 = " + (5<<4)); 5 System.out.println("15 >> 3 = " + (15>>3)); 6 System.out.println("-15 >> 3 = " + (-15>>4)); 7 System.out.println("-2 >>> 3 = " + (-2>>>3)); 8 } 9 }
打印结果如下:
7 << 3 = 56
5 << 4 = 80
15 >> 3 = 1
-15 >> 3 = -1
-2 >>> 3 = 536870911
格式: 条件表达式 ? 表达式1 : 表达式2
相当于if...else语句,当布尔表达式为true时,则执行表达式1,否则执行表达式2。
注意:因为是一个运算符,所以运算完必须要有一个结果。
1 class Demo{ 2 public static void main(String[] args){ 3 int a = 7; 4 int b = 6; 5 System.out.println(a>b ? a+1 : a-1); 6 } 7 }
打印结果如下:
8
标签:
原文地址:http://www.cnblogs.com/joker0day/p/4460060.html