Chapter 2: Java Basics - Operators

No Operator Overloading in JavaJava does not support operator overloading. So Java objects can not use operators such as ==, !=. There is only one special case: "+" and "+=" are used by String class to join strings together. "s1 += s2" is converted to method call s1 = s1.append(s2)".

Trying to use "==" or "!=" to compare strings like

   
   if(string == "No")...

is a big source of tricky run-time errors.

2.1: Aliasing - Object Assignment with "="

For primitive objects, assignment directly assigns the rvalue to the lvalue. But for user-defined objects, an assignment between object a1 and b1 like

   
   a1 = b1;

will only assign the handle of B to A. Therefore, handle a1 and b1 are now pointing to the same object. This is called aliasing.

When an object is passed to a method as an argument, its handle is passed and therefore the method can directly access the original object. This is equal to pass-by-reference in C++.

If you want to assign the value of an object to another, you have to design your own assignment method:

   
   a1.copyFrom(b1);

If you want to clone an object, you can make its class Cloneable and call its clone( ) method.

2.2: Compare Objects with "=="

If you use " = = " to compare two objects, you are actually comparing the handles of the two objects. Object's method equals( ) are also comparing the handles of the two objects. So if you want your object to be comparable, you must override equals( ) properly in your class.

Many standard Java classes such as String, Integer, etc. have already properly implemented their equals method.

Because String is immutable, if two strings have the same content, most probably their handles are pointing to the same memory location. That's why in most cases you can use "==" to check if two strings are equal - if they are equal their handles will be pointing to the same address. But it is not always like this. Sometimes JVM will have two equal strings in different locations. So always use method equals to compare strings.

2.3: Short Circuit in Logical Calculation

The logical operators && (and), || (or) and ! (not) is exactly the same as in C++.

In a expression " (A= =B) && (C= =D) && (E= =F) ", if "(A= =B)" is evaluated to be false, the rest of the expressions are no longer evaluated, because the final result has been definite (false) and thus there is no need to do it. This is called a short circuit and it may improve the performance.

2.4: Bitwise Operator

Bitwise operator & (and), | (or) and ^ (xor) is exactly the same as in C++.

Bitwise operators can be combined with "=": &=, |= and ^=.

Because boolean type is only one-bit: 00000001 and 00000000, so & and | do the same job on boolean type as && and ||.

2.5: Shift Operator

If you say

   
   int a = 1, b = 7;
   int i = a << b;

a is shifted to the left by the number of b (7). So i becomes 10000000 (128).

When you say

   int i = 128 >> 3;

b is shifted to the right by 3. So i becomes 00010000 (16).

The "<<" and ">>" shift operator use sign extension: if the operand is positive, 0 will be used to supplement the shifts, and if the operand is negative, 1 will be used. You can force right shift to supplement with 0 regardless of the sign of the operand with ">>>".