# Operators in Java

Operators in Java are used to perform operations with values, usually saved on variables. These values used in the operation are called operands and the operation performed with the operands is defined by the operator.

Java has different types of operators:

• Arithmetic.
• Comparison.
• Logical.
• Assignment.

## Arithmetic operators

Java provides the following operators for performing arithmetic operations

Let’s see in practice how these operators work.

### Activity 1: Practicing with arithmetic operators

1. Open the Java online compiler
2. Introduce the following code:

Line 3 & 4: Declaration and initialization of the two integer variables that we will use as operands. And the declaration of the double type variable that we will use to save the results of our operations.

Line 6 & 7: Addition operation between the variables num1 and num2. The result is saved in the result variable and showed on the screen

Line 10 & 11: Division of two operands of integer type. Since the division is not exact, the result loses precision.

Line 12 & 13: Division of two integer type operands that have previously been cast to double type.

Line 14 & 15: Modulus operator with operands num1 and num2. Since num1 <num2, the remainder of the division of both numbers is num1.

Line 18 & 19: increment operator, in its suffix mode. First, the data in num1 is stored in the variable inc and then it is incremented by 1.

And it showed on the screen the value of inc that is equal to the value of num1 before it is incremented.

Line 20 & 21: increment operator, in its prefix mode. The data in num1, which has a value of 8, is first incremented and then stored in the variable inc.

Line 22 & 23: decrement operator, in its prefix mode, is applied to the variable num2 and the result is stored in the variable dec

1. Run the program. You should get the following result:

## Comparison Operators

They are used to compare two values:

Let’s practice with these types of operators

### Activity 2: Practicise with comparison operators

1. Open the Java online compiler
2. Introduce the following code:

Line 1: This time, I have changed the name of the class, from the classic “HelloWorld” to “Operators”.

Line 3: Declaration of the two integer variables that I will use with the comparison operators.

Line 4: Initialization of the two variables. Note: The “=” operator is an assignment operator, not a comparison operator.

Line 5: We compare both variables within an “if” statement, using the comparison operator “==”. Note that it is a comparison operator (two equal signs in a row). The result of the comparison operation will be true if both values ​​are equal and false if they are not.

Line 6: It would be executed if the result of the comparison within the “if” statement is true. Showing the message on the screen

Line 8: It would be executed if the result of the comparison within the “if” statement is false. Showing the corresponding message on the screen.

Line 10: The value of the variable num1 is updated, it will now be worth 3. Both variables have the same value.

Line 11 to line 15: “if” statement similar to the one starting on line 5

Line 16: Change the value of both variables

Line 17 to line 21: A new “if” statement compares both variables, this time using the greater than operator: “>”.

Although we do not check all the comparison operators in this practice, it is enough to get an idea of ​​how they work. You can check the rest of the operators and practice their operations.

1. Run the program and you will get the following result:

## Logical operators

They are used to perform logical operations with variables

Let’s practice with these types of operators

### Activity 3: Practicing with logical operators

1. Open the Java online compiler
2. Introduce the following code:

Line 3: Declaration of two Boolean variables that will be the ones I will use to show how the different logical operators work.

The rest of the lines all do practically the same thing: They draw an operation and its result on the screen.

1. Run the program. You should get the following result:

## Assignment operators

These types of operators are used to assign values to variables:

The first one in the table above is wildly common and we’ve already used it several times. It is the one we use when we initialize a variable to save a certain value in it.

The rest are not that common, but in any case, let’s see how they work.

### Activity 4: Practicing with assignment operators

1. Open the Java online compiler
2. Introduce the following code:

Line 3: Declaration and initialization of a variable of integer type that I will use to perform the assignment operations

Line 4 to line 7: Instructions for painting different texts and values on the screen. This, we already know how it works.
With the last line, we draw the result of the operation “num + = 2” on the screen.

Line 12: And with this other instruction, we draw the result of the operation “num * = 3” on the screen.

This practice provides a general idea of the use of assignment operators. You can check on your own the rest of the operators and practice how they work.

1. Run the program. You should get the following result:

## Exercises with operators

We will be working with operators on all the exercises that I will be proposing in the different posts. In addition, we are familiar with them and they are easy to understand and apply. For this reason, I do not see much sense in dedicating a post specifically to exercises on this topic. My understanding is that raising one here is enough.

### Exercise 1.- Fix program errors

Please correct the errors in the following program to make it works properly.

A possible solution code would be:

### Exercise 2.- Basic operations

Develop a program with the following characteristics:
The program must have two variables of type double. The value of these variables is hardcoded, the first is 5 and the second is 7. And the program must display the following lines on the screen.

A possible solution would be:

If we run this program, we would obtain:

NOTE:

This post is part of the “Java” collection that reproduces the notes of the class that I teach on the subject at ESIC University. You can see the index of this collection here.