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;
int integer = 100; int result = integer / 0; // throws ArithmeticExceptionAlso, 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 ArithmeticExceptionAn 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: InfinityIf 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
String message = "Hello"; int a = 5; int b = 8; System.out.println(message + a + b); // Prints: Hello58If 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
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
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 -> falseAnd 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
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;
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