You are currently viewing Java Operators Decoded: Essential Tools for Crafting Robust Code

Java Operators Decoded: Essential Tools for Crafting Robust Code

1. Introduction

Operators are like tools in a toolbox for Java programmers. They help us perform different tasks, from basic math to making decisions in our code. Whether you’re adding numbers together, checking if something is true or false, or even working with binary data, operators are the key to getting things done in Java.
In this article, we’ll take a closer look at the various types of operators in Java and how they work.

2. What is an Operator?

An operator is a special symbol or keyword that performs operations on one, two, or three operands to produce a result. Depending on the number of operands, an operator is either unary (one), binary (two), or ternary (three).

3. Assignment Operator

3.1. Simple Assignment =

The simple assignment operator works as follows:

    variable = expression;

You should interpret the above code as follows: Take the content of expression and put it in place of variable. The previous value of variable is then overwritten by the new value expression. For this to be possible, expression and variable must be of compatible types.

    int value = 5;
    value = 7;

In the first line, the code assigns the value 5 to the int variable value. In the next line, this value is overwritten by 7.
The code below, however, fails to compile due to a type incompatibility.

    int value = 5;
    value = "This is not an int";

Assignment works for both primitive types and objects. When you assign an object to another, it is a reference assignment, not a content assignment. We’ll come back to objects later.

    Student student1 = new Student();
    Student student2 = new Student();
    student2 = student1;

In the above assignment, the student2 variable is assigned the value of student1. Following this assignment, these two variables refer to the same object in memory, namely student1.

3.2. Multiple Assignments

An assignment is an operator like any other, which means that applying it to a variable returns a result. Most of the time the returned value is discarded.
The code below will print 7 to the console:

    int value = 5;
    System.out.println(value = 7);//value gets the value 7, which is returned

The affectation value = 5; is equivalent to the following statements:

    value = 5;
    return value;

You might chain multiple assignments, as in the code below:

    i = j = k = 8;

In this case, evaluation is carried out according to the operator’s associativity rules, i.e. from right to left.

    (i = (j = (k = 8));

4. Arithmetic Operators

Read about arithmetic operators here.

5. Boolean Operators

Read about boolean operators here.

6. Bit Manipulation Operators

Read about bit manipulation operators here.

7. Precedence and Associativity Rules for Operators

7.1. Precedence Rules

These rules are used to determine which operator is applied first when two operators with different precedence follow each other in an expression. In this case, the operator with the highest precedence applies first.

    int result = 1 + 2 * 4;//Equivalent to 1 + (2 * 4)
    System.out.println(result);//Prints 9

The table below summarizes the precedence rules for all Java operators. Operators appear in descending order of precedence. You can see, for example, that assignment operators have the lowest precedence.

OperatorsPrecedence
Array element access, member access, method invocationarr[index], (args)
Unary postfix operatorsexpression++ expression–
Unary prefix operators~! ++expression –expression +expression -expression
Unary prefix creation and castnew (type)
Multiplicative* / %
Additive+ –
Shift<< >> >>>
Relational< <= > >= instanceof
Equality== !=
Bitwise / Logical AND&
Bitwise / Logical XOR^
Bitwise / Logical OR|
Conditional AND&&
Conditional OR||
Conditional?:
Arrow operator->
Assignment= += -= *= /= %= <<= >>= >>>= &= |= ^=

7.2. Associativity Rules

Associativity rules are used to determine which operator to apply first when two operators of equal precedence follow each other in an expression.
There are two types of associativity: left associativity and right associativity.

7.3. Left Associativity

Left Associativity means grouping expressions from left to right. All binary operators, except for the relational and assignment operators, associate from left to right.

int result = 3 + 4 - 5;// -> (3 + 4) - 5

7.4. Right Associativity

Right Associativity means grouping expressions from right to left. All unary operators, all assignment operators, and the ternary conditional operator associate from right to left.
Example 1

int result = - - 10;// -> -(- 10)

Example 2

    int i = 0, j = 1, k = 2;
    i = j = k = 5; // -> (i = (j = (k=5)))

8. Conclusion

This article was a quick introduction to operators in Java. You’ve discovered the different types of operators: unary, binary, and ternary. You’ve also learned about the precedence and associativity rules that govern all these operators.

9. References

1) OCP Oracle Certified Professional Java SE 17 by Khalil A. Mughal and Vasily A. Strelnikov
2) Oracle Java Documentation

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.

This Post Has 4 Comments

Leave a Reply