The OCA (Oracle Certified Associate Java SE8 Programmer) certification offers a series of quirky assignments that test a deep understanding of the Java programming language. The whole block is devoted to expressions, loops and operators. We will talk about the latter today.
The operator accepts arguments as input and returns some values. There are unary, binary, and ternary operators. For instance, ! false – unary, a + b – binary, and? : – is the only operator that takes three arguments as input.
The first thing to remember is the precedence of the operators:
- Post-unary operators: exception ++ and exception–
- Pre-unary operators: ++ exception and –exception
- The rest of the unary operators: +, -,!
- Multiplication, division, taking the remainder: *, /,%
- Addition and Subtraction: +, –
- Bit shift operators: <<, >>, >>>
- Comparison operators: <,>, <=,> =, instanceof
- Equality-inequality operators: ==,! =
- Logical operators: &, |, ^
- Short-circuit logical operators: &&, ||
- Ternary operator: boolean expression? expression1: expres-
- Assignment operators: =, + =, – =, * =, / =,% =, & =, ^ =,! =, << =, >> =, >>> =
Working with Binary Arithmetic Operators
Let’s start with the binary operators, which are the most common in Java. On the exam, they may offer quite complex expressions for the calculation of which you must remember the order of their execution.
Arithmetic operators include addition, subtraction, multiplication, and remainder operators. As I said, when calculating, you must remember the precedence of the operators:
int x = 2 * 5 + 3 * 4 – 8 = 10 + 12 – 8 = 14;
You should also remember that the execution priority can be changed by parentheses (the expression in parentheses is calculated first):
int x = 2 * ((5 + 3) * 4 – 8) = 2 * (8 * 4 – 8) = 2 * (32 – 8) = 2 * 24 = 48;
Newbies may confuse the integer division (/) and modulus (%) operators:
System.out.print(9 / 3); // Outputs 3 System.out.print(9 % 3); // Outputs 0 System.out.print(10 / 3); // Outputs 3 System.out.print(10 % 3); // Outputs 1 System.out.print(11 / 3); // Outputs 3 System.out.print(11 % 3); // Outputs 2 System.out.print(12 / 3); // Outputs 4 System.out.print(12 % 3); // Outputs 0
If the arguments of a binary operator are numeric and different, Java applies type conversions according to the following rules:
- If two arguments are of different data types, then Java converts the argument of the less capacious type to the argument of the more capacious type
- If one argument is of an integer type and the other is of a real type, then Java converts the integer value to a real type
- Regardless of the types of arguments, Java first converts less capacious integer types (byte, short, char) to int
- After casting the arguments to the same type, Java will return the same type as the arguments after the casting.
What type of result will the multiplication x * y be?
int x = 1; long y = 33;
According to the first rule, x will be converted to type long, and then a result of the same type will be returned.
What type of addition will be x + y?
double x = 39.21; float y = 2.1;
Here, in the best traditions of certification, a trap lies in wait for us: 2.1 is a double, and 2.1f is a float. An attempt to write a double value to a float variable without explicit conversion results in a compilation error in Java.
What type of addition will be x + y?
double x = 39.21; float y = 2.1f;
Similar to example 1, float will be converted to double, and then a double result will be returned.
What type will the result of doing division x / y be?
short x = 10; short y = 3;
According to rule 3, both arguments will be cast to int before division, and then according to rule 4 int will be returned. The answer is not short at all!
What type will the result of the operation x * y / z be?
short x = 14; float y = 13; double z = 30;
According to all the rules above, x will be converted to int first. Then the operation of multiplying x (int) and y (float) is performed: x will be converted to float, and the result will be of type float. Then float / double is executed, so float will be converted to double and double will be returned.
Is it interesting to develop in this direction? See the course program Oracle Java Programmer Certification Preparation (OCAJP)!