Binary Operators in Java

OTUS is launching a new professional program soon preparation for Oracle Java Programmer (OCAJP) certification… We invite you to a free Demo lesson Java Data Types: Identifiers and Primitives and publish an article by Vladislav Rodin – head of the development group, teacher at MIPT and foxminded.

Introduction

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.

Operator priority

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:

  1. Post-unary operators: exception ++ and exception–
  2. Pre-unary operators: ++ exception and –exception
  3. The rest of the unary operators: +, -,!
  4. Multiplication, division, taking the remainder: *, /,%
  5. Addition and Subtraction: +, –
  6. Bit shift operators: <>, >>>
  7. Comparison operators: , <=,> =, instanceof
  8. Equality-inequality operators: ==,! =
  9. Logical operators: &, |, ^
  10. Short-circuit logical operators: &&, ||
  11. Ternary operator: boolean expression? expression1: expres-
    sion2
  12. 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

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

Converting numbers

If the arguments of a binary operator are numeric and different, Java applies type conversions according to the following rules:

  1. 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
  2. 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
  3. Regardless of the types of arguments, Java first converts less capacious integer types (byte, short, char) to int
  4. After casting the arguments to the same type, Java will return the same type as the arguments after the casting.

Example 1

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.

Example 2

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.

Example 3

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.

Example 4

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!

Example 5

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)!

Similar Posts

Leave a Reply Cancel reply