This article emphasizes some important points with regard to operators in Java:

 

1. Be careful with the division operator (/):

Consider the following expression:

float a = 3 / 4;
What the result do you expect? 0.75 right? But the variable a has a value of 0.0. Why?

It’s because the / operator used with integer values will result in integer value, hence 0.75 become 0.0. If you want to have the precise result, use the suffix f or F for the operands like the following:

float a = 3f / 4f;
The result will be 0.75.

Note that if either an operand is a float, then the result will be float. Hence:

float a = 3f / 4;
This also gives the exact result.

If the division involves in two integer variables, use type casting like this:

int x = 3;
int y = 4;

float z = (float) x / y;


The similar rule applies for expressions result in double value.

Be aware of this behavior will save you time in debugging problems related to arithmetic operation.

 

2. Divided by zero

If an integer is divided by zero, then an ArithmeticException will be thrown at runtime. For example:

int integer = 100;

int result = integer / 0;	// throws ArithmeticException
Also, using the remainder operator (%) on an integer number and if the right operand is zero, the ArithmeticException will be thrown. For example:

int integer = 100;
int remain = integer % 0; // throws ArithmeticException
An interesting point is that if a floating-point number is divided by zero, no exception is thrown. Instead, the result is Infinity. For example:

float pi = 3.14F;
float result = pi / 0;

System.out.println(result);	// Prints: Infinity
If the remainder operator is used on a floating-point number and if the right operand is zero, no exception is thrown. Instead, the result is NaN. For example:

float pi = 3.14F;
float result = pi % 0;

System.out.println(result);	// Prints: NaN
 

3. String concatenation operator (+)

In an expression, if either operand is a String, the + operator becomes a String concatenation operator. For example:

String message = "Hello";
int a = 5;
int b = 8;

System.out.println(message + a + b);	// Prints: Hello58
If both operands are number, the + operator is the addition operator. For example:

String message = "Hello";
int a = 5;
int b = 8;

System.out.println(a + b + message);	// Prints: 13Hello
  

4. Pay attention to position of the increment (++) and decrement (--) operators:

The ++ and -- operators can be placed on the left side or the right side of an operand. The result of an expression depends on the position of these operators. Let’s see the following example:

int a = 5;
int b = 3;

int x = ++a + ++b;
The result is x = 10; 

int a = 5;
int b = 3;

int x = a++ + b++;
The result is x = 8; 

int a = 5;
int b = 3;

int x = ++a + b++;
The result is x = 9;

 

Note that the ++ and -- operators can be also applied for float and double variables, as they adds or subtracts 1.0 from the variables. For example:

float x = 0.75f;
float y = ++x;
The result is y = 1.75;

 

These operators cannot be applied for final variables, so look out for code like this:

final int f = 10;

int g = f++;	// compile error: cannot assign a value to final: variable f
 

5. Understand the ‘short-circuit’ behavior of the conditional operators AND and OR:

Consider the following code that uses the AND operator (&&):

if (expression1 && expression2) { …}

If the expression1 evaluates to false, then the expression2 won’t evaluate, hence the result is false, because:

false AND whatever -> false
And let’s see the following statement that uses the OR operator (||):

if (expression1 || expression2) { …}

If the expression1 evaluates to true, then the expression2 won’t evaluate, hence the result is true, because:

true OR whatever -> true
 

6. Use ternary expression

Use the ternary expression (A ? B : C) whenever possible to make the code more compact. For example, the following code:

if (x > 500) {
	result = 5 * x;
} else {
	result = 2 * x;
}

This can be re-written using the ternary expression like the following statement:

result = x > 500 ? 5 * x : 2 * x;

 

7. Use bit shift operators if performance matters:

Generally, these kinds of shift operators are rarely used. But remember this rule:

                Right shift: x >> n = x / 2^n

                Left shift: x << n = x * 2^n

That means if you want to divide a number itself by 2^n, then using the right shift operator is much faster than the normal division. For example:

int x = 2_147_483_648;
x = x / 8;

Then:

x = x >> 3

produces the same result but much faster.

The similar applies for multiplication. For example:

int x = 100;
x = x * 8;

Then

x = x << 3

produces the same result but much faster. 

References: Operators in the Java Tutorials

 

Related Topics:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment