Java运算符小记

Java运算符记录本,以此记下容易出错的地方。

来自于 Thinking in Java

Relational Operators

The relational operators == and != also work with all objects. Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Equivalence{
public static void main (String[] args){
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1 == n2);
System.out.println(n1 != n2);
}
}
/* Output:
false
true
*/

The statement System.out.println(n1==n2) will print the result of the boolean comparison within it. While the contents of the objects are the same, the references are not the same. The operators == and != compare object references, so the output is actually “false” and then “true”.

What if we want to compare the actual contents of an object for equivalences? We must use the special method equals() that exits for all objects (not primitives, which work fine with == and !=). Example:

1
2
3
4
5
6
7
8
9
public class EqualsMethod{
public static void main(String[] args){
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.printly(n1.equals(n2));
}
}
/* Output: true */

But default equals() does not compare contents. Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Value{
int i;
}
public class EqualsMethod2{
public static void main(String[] args){
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
}
}
/* Output: false */

The result is false. This is because the default behavior of equals() is to compare references. So unless you override equals() in your new class you won’t get the desired behavior. Most of the Java library classes implement equals() so that it compares the contents of objects instead of their references.

Bitwise Operators

The bitwise operators allow you to manipulate individual bits in an integral primitive data type. Bitwise operators perform Boolean algebra on the corresponding bits in the two arguments to produce the result.

The bitwise operators come from C’s low-level orientation, where you often manipulate hardware directly and must set the bits in hardware registers. Java was originally designed to be embedded in TV set-top boxes, so this low-level orientation still made sense.

The bitwise AND operator (&) produces a one in the output bit if both input bits are one; otherwise, it produces a zero.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Example
class Bitwise{
public static void main(String[] args){
int a = 129;
int b = 128;
System.out.println(a&b);
}
}
/* Output: 128;
Integer.toBinaryString(a) is 10000001;
Integer.toBinaryString(b) is 10000000;
Integer.toBinaryString(a&b) is 10000000; */

The bitwise OR operator (|) produces a one in the output bit if either input is a one and produces a zero only if both input bits are zero.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Example
class Bitwise{
public static void main(String[] args){
int a = 129;
int b = 128;
System.out.println(a|b);
}
}
/* Output: 129;
Integer.toBinaryString(a) is 10000001;
Integer.toBinaryString(b) is 10000000;
Integer.toBinaryString(a|b) is 10000001; */

The bitwise EXCLUSIVE OR, or XOR (^) , produces a one in the output bit if one or the other input bit is a one, but not both, which means if the bits are same, producing 0, if the bits are different, producing 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Example
class Bitwise{
public static void main(String[] args){
int a = 15;
int b = 2;
System.out.println(a^b);
}
}
/* Output: 13;
Integer.toBinaryString(a) is 1111;
Integer.toBinaryString(b) is 0010;
Integer.toBinaryString(a^b) is 1101; */

The bitwise NOT, also called the ones complement operator, (~), is a unary operator; it takes only one argument. (All other bitwise operators are binary operators.) Bitwise NOT produces the opposite of the input bit - a one if the input bit is zero, a zero if the input bit is one.

1
2
3
4
5
6
7
8
9
10
// Example
class Bitwise{
public static void main(String[] args){
int a = 2;
System.out.println(~a);
}
}
/* Output: -3;
Integer.toBinaryString(a) is 10;
Integer.toBinaryString(~a) is 1111 1111 1111 1111 1111 1111 1111 1101; */