文章转自:android.yaohuiji.com
一、赋值运算符 Assignment Operators
其实Java里有12种赋值运算符,“=”只是最常见的一种,下面我们讲解的四个常用复合赋值运算符(+=、-=、*=、/=)
public class Lesson04_1 {
public static void main(String[] args){
int x=10;
int y=10;
y=y-6;
x=x+2*5;
System.out.println("x="+x);
System.out.println("y="+y);
x=10;
y=10;
y-=6;
x+=2*5;
System.out.println("x="+x);
System.out.println("y="+y);
x=10;
x*=2+5;
System.out.println("x="+x);
}
}
我们可以看到上面两段代码的效果是等同的,下面的代码的等同的代码是x=x*(2+5),为什么不是乘法优先呢?是因为*=还是赋值运算符,赋值运算符的优先级是最低的。关于运算符优先级我们最后会有个总结。
最后把11种复合赋值运算符都列出来:
运算符 | 举例 | 等效表达式 |
+= | a += b | a = a+b |
-= | a -= b | a = a-b |
*= | a *= b | a = a*b |
/= | a /= b | a = a/b |
%= | a %= b | a = a%b |
&= | a &= b | a = a&b |
|= | a |= b | a = a|b |
^= | a ^= b | a = a^b |
<<= | a <<= b | a = a<<b |
>>= | a >>= b | a = a>>b |
>>>= | a >>>= b | a = a>>>b |
二、关系运算符 Relational Operators
关系运算符用于比较操作数之间的关系,关系运算符总是产生一个布尔值(true 或 false)。
运算符 | 功能 | 举例 | 运算结果 | 可运算类型 |
> | 大于 | ‘a’>’b’ | false | 整数、浮点数、字符 |
< | 小于 | 2<3.0 | true | 整数、浮点数、字符 |
== | 等于 | ‘x’==88 | true | 任意 |
!= | 不等于 | true!=true | flase | 任意 |
>= | 大于或等于 | 6.6>=8.9 | flase | 整数、浮点数、字符 |
<= | 小于或者等于 | ‘M’<=88 | true | 整数、浮点数、字符 |
当在两个字符类型数据之间比较或者把字符类型数据和整数浮点数类型比较时,Java将字符的Unicode值当做数值与其它数值相比较。
相等性运算符 “Equality” Operators
== 和 != 这两个关系运算符比较的是两个相似事物的是否相等。注意他们无法比较两个不兼容的类型,什么叫无法比较?就是编译器会直接报错。
对于整数和浮点数之间的相等性比较如果他们的值相等,那么就返回true。对于引用类型的变量之间的比较,是看他们是否引用了同一个对象,如果变量的位相等那么他们就是相等的。
import java.util.Date;
public class Lesson04_2 {
public static void main(String[] args){
System.out.println("'a'=='a' ? " + ('a'=='a'));
System.out.println("'a'=='b' ? " + ('a'=='b'));
System.out.println("5.0==5L ? " + (5.0==5L));
System.out.println("true==true ? " + (true==true));
Date date1 = new Date();
Date date2 = new Date();
Date date3=date1;
boolean b1=date1==date2;
boolean b2=date1==date3;
System.out.println("date1="+date1.getTime());
System.out.println("date2="+date2.getTime());
System.out.println("b1="+b1);
System.out.println("b2="+b2);
}
}
看一下运行结果:
三、instanceof 运算符 instanceof Comparison
因为你们还没有正式学过对象,所以这一节,等学过对象之后请再回来看一遍。
instanceof 运算符只值能用于比较对象引用变量,可以检查对象是否是某种类型。这里的类型是指类、接口类型数组。或者说 instanceof 检查的是运算符左边的变量引用的对象是否能通过右边类型或者接口的IS-A 测试。
import java.util.Date;
public class Lesson04_3 {
public interface Foo{}
public class A implements Foo{}
public class B extends A{};
public void instance(){
A a = new A();
B b= new B();
Foo f= new A();
System.out.println(a instanceof Foo);
System.out.println(b instanceof A);
System.out.println(b instanceof Foo);
System.out.println(f instanceof A);
System.out.println(f instanceof B);
}
public static void main(String[] args){
Lesson04_3 lesson = new Lesson04_3();
lesson.instance();
}
}
运行结果如下:
四、算术运算符 Arithmetic Operators
基本算术运算符: +加 –减 *乘 /除
求余运算符: %
递增和递减运算符: ++ – ,可以作为缀或后缀
下面举例说明:
public class Lesson04_4 {
public static void main(String[] args){
//求余运算
int a=10;
int b=7;
System.out.println(a%b);
//自增运算
double x = 2;
double y= 1.2;
double z= x++ + ++y;
System.out.println(z);
}
}
运行的结果是:
五、条件运算符 Conditional Operator
条件运算符根据条件来返回一个值。计算问号左边表达式的值,值为真是提供冒号左边的操作数为返回值,值为假时提供冒号右边的操作数为返回值。这是Java里唯一的一个三元运算符,为了让你记忆深刻,我做了一张图:
其中括号可以省略。
public class Lesson04_5 {
public static void main(String[] args){
int numOfPets =3;
String status = (numOfPets < 4)?"宠物数量刚刚好":"宠物数量太多了";
System.out.println(status);
}
}
编译和运行结果是:
六、逻辑运算符 Logical Operator
逻辑运算符只对布尔型操作数进行运算并返回一个布尔型数据。一共有6个逻辑运算符:&& , || ,& , | ,!和 ^
短路逻辑运算符 Short-Circuit Logical Operators:
public class Lesson04_6 {
public static void main(String[] args) {
int i = 5;
// 短路与运算符&&,要求左右两个表达式都为true时才返回true,如果左边第一个表达式为false时,它立刻就返回false,就好像短路了一样立刻返回,省去了一些无谓的计算时间。
boolean flag = (i < 3) && (i < 4);
System.out.println(flag);
// 短路或运算符||,要求左右两个表达式有一个为true时就返回true,如果左边第一个表达式为true时,它立刻就返回true,就好像短路了一样立刻返回,省去了一些无谓的计算时间。
flag = (i > 4) || (i > 3);
System.out.println(flag);
}
}
编译和运行结果是:
非短路逻辑运算符 Not Short-Circuit Operators:
public class Lesson04_6 {
public static void main(String[] args) {
int i = 5;
// 非短路与运算符&,要求左右两个表达式都为true时才返回true,两个表达式他都会计算
boolean flag = (i < 3) & (i < 4);
System.out.println(flag);
// 非短路或运算符|,要求左右两个表达式有一个为true时就返回true,两个表达式他都会计算
flag = (i > 4) | (i > 3);
System.out.println(flag);
}
}
编译和运行结果是:
逻辑运算符 Logical Operators ^ and ! :
^ 异或运算符 XOR : 要使一个^表达式为真,必须有且只有一个操作数为真。
public class Lesson04_6 {
public static void main(String[] args) {
System.out.println("xor ((2<3)^(3<4)) :" + ((2<3)^(3<4)));
System.out.println("xor ((2<3)^(3>4)) :" + ((2<3)^(3>4)));
System.out.println("xor ((2>3)^(3>4)) :" + ((2>3)^(3>4)));
}
}
编译和运行结果是:
七、位运算符 Bitwise Operators
位运算符是对整数操作数以二进制的每一位进行操作,返回结果也是一个整数。
逻辑位运算符分别是 按位取反 ~ 、按位与 & 、按位或 | 和 按位异或 ^
移位运算符有 左移<< 、 右移>> 、
从SCJP考试的5.0版本以后不再对此项知识点有任何涉及,果断决定不放在讲座里讲,而是放在番外篇里提一提。
八、运算符优先级
优先级
| 运算符 |
1
|
() [] .
|
2
|
! +(正) -(负) ~ ++ –
|
3
|
* / %
|
4
|
+(加) -(减)
|
5
|
<< >> >>>
|
6
|
< <= > >= instanceof
|
7
|
== !=
|
8
|
&(按位与)
|
9
|
^
|
10
|
|
|
11
|
&&
|
12
|
||
|
13
|
?:
|
14
|
= += -= *= /= %= &= |= ^= ~= <<= >>= >>>=
|
优先级是上面的高下面的低,用括号来解决优先级不清晰的部分是个良好的编程习惯。