OPERATORS IN JAVA

java-logo

An operator is a specific symbol in a programming language that allows a specific operation to be performed. An operator always acts on a set of elements called operands.

Operand

In the example you can see the operator + which acts on two operands. As we shall see, it is possible to join several operators together; in these cases, precedence must be established among the operators. Precedence is a rule that some operators have priority over others. Then when we are in compound operations in which the operators have the same precedence associativity comes into action. It determines in what order operators will be considered. There are actually two types:

Associativity

In the first case the leftmost operator is executed then the rightmost operator. In the second case it is the reverse. Most operators are binary, that is, they work on a pair of operands such as the + operator. Other operators work on a single operand, such as the operator
which is used to reverse the sign of an operand. In compound operations it is possible to alter precedence and associativity with round brackets as we shall see. Let’s give an example right away.

5 + 3 * 2 = 11

In this case, the multiplication operator takes precedence over the sum operator; therefore, first the 3 * 2 calculation is done and then the sum. If we want to change this behavior just put the parentheses in round brackets and perform the sum first then the multiplication.

(5 + 3) * 2 = 16

THE SIMPLE ASSIGNMENT OPERATOR

We met him several times and he was represented by the equal symbol. This is the operator we must use to assign a value to a variable or constant.

myVar = 150;

To be more precise, we must say that the simple assignment operator assigns a value or expression that is to its right to the variable or constant that is placed to the left of the equal symbol.

An expression is a simple or compound operation evaluated during program execution (Runtime) and always producing a value. An expression can be a literal value or a variable that returns the associated value at that time, or consist of multiple operators acting on operands or even the invocation of a method.

Operatore di uguaglianza

lvalue is the memory that is associated with a variable in which the return value of an expression is placed.

lvalue

rvalue is an expression that produces a value, which can be assigned to an lvalue.

rvalue

In this case, if we reverse the order of the assignment, the variable myVar can also be an rvalue since it is an expression that returns its own value and therefore can be to the right of the equal symbol. However, literal 250 clearly cannot be an lvalue since it does not represent an area of memory into which we can go and write something, that is, it is not an area of memory pointed to by a variable. It follows that the assignment is invalid.

Assegnamento

ARITHMETIC OPERATORS

Aritmetic operators

The first five operators are binary, meaning they act on two operands while the last two are unary, increasing or decreasing by one unit the operand to which they are applied. The addition and subtraction operators we know them well have left-to-right associativity. You can, of course, use variables instead of numeric literals.

Somma e sottrazione

The multiplication operator follows the same rules as addition and subtraction. However, it has higher precedence and has the same associativity from left to right.

Moltiplicazione

Turning to division, if there is a decimal part and we use a variable of type integer the fractional part will be discarded. Use float and double to keep the decimal part.

Divisione
Double

The modulo operator returns the remainder of a division.

Modulo

INCREMENT AND DECREMENT OPERATORS

If we apply the unary increment operator ++ by placing it before the variable we are using the prefixed notation of the operator which is called preincrement. In this case what happens is that first the variable y is incremented and then only then added to x.

Preincrement

If we place the operator ++ after the operand things change, first the sum of x and y is performed then only then y is incremented by one unit.

Postincrement

Same thing happens for the unary decrement operator .

Predecrement

The last figure represents a postdecrement.

Postdecrement

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.

LINKS TO PREVIOUS POSTS

THE JAVA LANGUAGE

LINK TO CODE ON GITHUB

GITHUB