运算符
运算符分类:
1.算术运算符:
+ 加- 减* 乘/ 除% 取余++ 自增-- 自减
前缀自增自减法(++a,--a): 先进行自增或者自减运算,再进行表达式运算。
后缀自增自减法(a++,a--): 先进行表达式运算,再进行自增或者自减运算
public class Demo {
public static void main(String
[] args
) {
int a
= 10;
int b
= 20;
int c
= 25;
int d
= 25;
System
.out
.println("a + b = " + (a
+ b
) );
System
.out
.println("a - b = " + (a
- b
) );
System
.out
.println("a * b = " + (a
* b
) );
System
.out
.println("b / a = " + (b
/ a
) );
System
.out
.println("b % a = " + (b
% a
) );
System
.out
.println("c % a = " + (c
% a
) );
System
.out
.println("a++ = " + (a
++) );
System
.out
.println("a-- = " + (a
--) );
System
.out
.println("d++ = " + (d
++) );
System
.out
.println("++d = " + (++d
) );
}
}
a + b
= 30
a - b
= -10
a * b
= 200
b / a
= 2
b % a
= 0
c % a
= 5
a++
= 10
a--
= 11
d++
= 25
++d
= 27
public class Demo{
public static void main(String
[] args
){
int a
= 3;
int b
= ++a
;
int c
= 3;
int d
= --c
;
System
.out
.println("进行自增运算后的值等于"+b
);
System
.out
.println("进行自减运算后的值等于"+d
);
}
}
进行自增运算后的值等于4
进行自减运算后的值等于2
int b = ++a; 拆分运算过程为: a=a+1=4; b=a=4, 最后结果为b=4,a=4
int d = --c; 拆分运算过程为: c=c-1=2; d=c=2, 最后结果为d=2,c=2
public class Demo{
public static void main(String
[] args
){
int a
= 5;
int b
= 5;
int x
= 2*++a
;
int y
= 2*b
++;
System
.out
.println("自增运算符前缀运算后a="+a
+",x="+x
);
System
.out
.println("自增运算符后缀运算后b="+b
+",y="+y
);
}
}
自增运算符前缀运算后a
=6,x
=12
自增运算符后缀运算后b
=6,y
=10
2.赋值运算符:
= 赋值+= 加并赋值-= 减并赋值*= 乘并赋值/= 除并赋值%= 取余并赋值<<= 左位移并赋值>>= 右位移并赋值&= 按位与并赋值^= 按位异或并赋值!= 按位或并赋值
public class Demo{
public static void main(String
[] args
) {
int a
= 10;
int b
= 20;
int c
= 0;
c
= a
+ b
;
System
.out
.println("c = a + b = " + c
);
c
+= a
;
System
.out
.println("c += a = " + c
);
c
-= a
;
System
.out
.println("c -= a = " + c
);
c
*= a
;
System
.out
.println("c *= a = " + c
);
a
= 10;
c
= 15;
c
/= a
;
System
.out
.println("c /= a = " + c
);
a
= 10;
c
= 15;
c
%= a
;
System
.out
.println("c %= a = " + c
);
c
<<= 2 ;
System
.out
.println("c <<= 2 = " + c
);
c
>>= 2 ;
System
.out
.println("c >>= 2 = " + c
);
c
>>= 2 ;
System
.out
.println("c >>= 2 = " + c
);
c
&= a
;
System
.out
.println("c &= a = " + c
);
c
^= a
;
System
.out
.println("c ^= a = " + c
);
c
|= a
;
System
.out
.println("c |= a = " + c
);
}
}
c
= a + b
= 30
c +
= a
= 40
c -
= a
= 30
c *
= a
= 300
c /
= a
= 1
c %
= a
= 5
c
<<= 2
= 20
c
>>= 2
= 5
c
>>= 2
= 1
c
&= a
= 0
c ^
= a
= 10
c
|= a
= 10
3.关系运算符(比较运算符):
> 大于< 小于>= 大于等于<= 小于等于== 等于!= 不等instance of (用来在运行时指出对象是否是特定类的一个实例 返回boolean)
( Object reference variable ) instanceof (class/interface type)
最终结果都是boolean (true 或 false)
public class Demo {
public static void main(String
[] args
) {
int a
= 10;
int b
= 20;
System
.out
.println("a == b = " + (a
== b
));
System
.out
.println("a != b = " + (a
!= b
));
System
.out
.println("a > b = " + (a
> b
));
System
.out
.println("a < b = " + (a
< b
));
System
.out
.println("b >= a = " + (b
>= a
));
System
.out
.println("b <= a = " + (b
<= a
));
}
}
a
== b
= false
a
!= b
= true
a
> b
= false
a
< b
= true
b
>= a
= true
b
<= a
= false
String name
= "James";
boolean result
= name
instanceof String;
4.逻辑运算符:
& 与| 或! 非&& 短路与|| 短路或
public class Demo{
public static void main(String
[] args
) {
boolean a
= true;
boolean b
= false;
System
.out
.println("a && b = " + (a
&&b
));
System
.out
.println("a || b = " + (a
||b
) );
System
.out
.println("!(a && b) = " + !(a
&& b
));
}
}
a
&& b
= false
a
|| b
= true
!(a
&& b
) = true
public class Demo{
public static void main(String
[] args
){
int a
= 5;
boolean b
= (a
<4)&&(a
++<10);
System
.out
.println("使用短路逻辑运算符的结果为"+b
);
System
.out
.println("a的结果为"+a
);
}
}
使用短路逻辑运算符的结果为false
a的结果为5
5.条件运算符(三目运算符):
条件表达式 ? 值1 : 值2;
条件表达式是值为boolean类型的表达式
执行流程: 1.计算条件表达式 2.如果值为true,最终结果取值1 3.如果值为false,最终结果取值2
public class Demo{
public static void main(String
[] args
){
int a
, b
;
a
= 10;
b
= (a
== 1) ? 20 : 30;
System
.out
.println( "Value of b is : " + b
);
b
= (a
== 10) ? 20 : 30;
System
.out
.println( "Value of b is : " + b
);
}
}
Value of b is
: 30
Value of b is
: 20
6.位运算符:
& 与 (如果相对应位都是1,则结果为1,否则为0)| 或 (如果相对应位都是 0,则结果为 0,否则为 1)^ 异或 (如果相对应位值相同,则结果为0,否则为1)~ 取反 (按位取反运算符翻转操作数的每一位,即0变成1,1变成0。)<< 按位左移 (按位左移运算符。左操作数按位左移右操作数指定的位数。)>> 按位右移 ( 按位右移运算符。左操作数按位右移右操作数指定的位数。)>>> 按位右移并补0 (按位右移补零操作符。左操作数的值按右操作数指定的位数右移,移动得到的空位以零填充。)
A为60 B为13
A
= 0011 1100
B
= 0000 1101
-----------------
A
&B
= 0000 1100
A
| B
= 0011 1101
A ^ B
= 0011 0001
~A
= 1100 0011
public class Demo{
public static void main(String
[] args
) {
int a
= 60;
int b
= 13;
int c
= 0;
c
= a
& b
;
System
.out
.println("a & b = " + c
);
c
= a
| b
;
System
.out
.println("a | b = " + c
);
c
= a
^ b
;
System
.out
.println("a ^ b = " + c
);
c
= ~a
;
System
.out
.println("~a = " + c
);
c
= a
<< 2;
System
.out
.println("a << 2 = " + c
);
c
= a
>> 2;
System
.out
.println("a >> 2 = " + c
);
c
= a
>>> 2;
System
.out
.println("a >>> 2 = " + c
);
}
}
a
& b
= 12
a
| b
= 61
a ^ b
= 49
~a
= -61
a
<< 2
= 240
a
>> 2
= 15
a
>>> 2
= 15
运算符优先级