OPERATORS IN JAVA

THE COMPOUND ASSIGNMENT OPERATORS

java-logo

Let’s start right away with an example. From the figure below you can see that we took the value of the variable x (10) we added 5 and assigned the result to the variable x.

Assegnamenti composti

We see the contracted form alongside that has the same meaning.

Forma contratta

x+=5 -> x = x + 5

Compound assignment forms exist for all arithmetic operators:

+=;-=;*=;/=;%=

The use of these forms is very common in Java, so it is good to understand them early on.

THE COMPARISON OPERATORS

We use such operators to perform a true comparison between two values.

Operatori relazionali

These operators always result in a boolean value (true or false). The two equality operators == and inequality != can be used to compare values of any type, primitive values and objects. The other operators we can use only to compare primitive data types.

Confronto

LOGICAL OPERATORS

They allow us to form even complex boolean expressions that return true or false.

Operatori logici

Complex expressions can be formed; in fact, such operators can be combined with each other. They apply to operands of type boolean; to understand how these operators work, it is best to examine their truth tables. The AND operator returns true only when combining operands that are true.

and

The OR operator returns false only when combining operands that are both false, in all other cases it returns true.

OR

The XOR operator returns true when the compared operands are different, false when they are the same.

xor

The NOT operator negates (reverses) the operand on which it is applied.

Not

LET’S GIVE AN EXAMPLE, MORE YOU WILL FIND IN THE DOWNLOADABLE CODE FROM GITHUB

Esempio

THE SHORT-CIRCUIT LOGIC OPERATORS

These operators are two AND and OR and are variations on those already reviewed. They serve to make our code more efficient; we’ll see why in a bit. Let’s see how to write these two operators:

&& = AND Logical Conditional

|| = OR Logical Conditional

Let us see what are the special features of these two operators and why we talk about short-circuit.

Operatori Logici condizionali

At the end of the last instruction we will get b = false and y = 11. This is because the logical AND & also evaluates the second condition even if it is superfluous. In fact, since the first expression is false, the remaining part of the code might as well not be executed since the result is already acquired.

Operatori logici condizionali

The reason it is called short-circuit is this: In fact if the first part of the expression fails then the whole expression is false. The AND && produces a short circuit in that the second part of the expression is not evaluated. This also results in the variable y not being incremented, in fact the post increment is not executed. Let us now see what happens with the conditional OR.

Operatori logici condizionali

Analyzing the first part of the expression already shows that the whole expression will be true. The | operator examines both conditions, so y is also incremented.

Operatori logici condizionali

Again to improve performance, the second part of the expression is not evaluated.

LINKS TO PREVIOUS POSTS

THE JAVA LANGUAGE

LINK TO CODE ON GITHUB

GITHUB

EXECUTION OF THE EXAMPLE CODE

  • Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.

java -jar –enable-preview CourseJava.jar

  • Or run the main found in the file CorsoJava.java.