Java Operators

Java operator is a symbol that is used to perform specific operations on variables and values. Let’s assume you want to add 20 with 30 and assign it to a variable. You might do something like –

int result = 20 + 30;

Here we have used two operators – 

  1. One is + to add two values.
  2. Other one is = to assign the result to a variable result.

In the above example, we have added two values. But we can use operators to add a value with a variable or even a variable with another variable.

int result1 = 20 + 30;             ⇐ result1 is 50 (20+30)
int result2 = 40 + result1;        ⇐ result2 is 90 (40+50)
int result3 = result2 + result1;   ⇐ result3 is 140 (50+90)

In java, we can divide operators into the below groups. There are a few more groups, but we can ignore them for now.

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
 Arithmetic Operators

These are used to perform common mathematical operations.  Let’s assume we have two integer variables a and b. Based on the operator used, I will show you the result.

int a = 10;
int b = 3;
OperatorNameShort DescriptionExampleResult
+AdditionAdds two valuesa + b13
SubtractionSubtracts one value from anothera – b7
*MultiplicationMultiplies two valuesa*b30
/DivisionDivides one value by anothera/b3
%ModulusReturns the division remaindera%b1
++IncrementIncreases the value by 1++a11
DecrementDecrease the value by 1–b2

You might be wondering, why 10/3 is 3 not 3.33. The reason is, as we are operating only on integers (whole number), we are getting the whole number as output. To get the exact result, we have to define one or both of the variables as double or float.

double a = 10;
double b = 3;
System.out.println(a/b);  ⇐ Will print 3.33

For increment and decrement operators, we have used ++a and --b. It is also allowed to do something like a++ and b--. But there is one major difference. Let’s consider the increment operator. Both a++ and ++a increment the value by 1, but the result returned by a++ is the value of a before incrementing, whereas the result returned by ++a is the value of a after the increment is applied. Let’s understand this with an example.

int a = 10;
int b = 3;
System.out.println(a + ++b);  ⇐ 14 : b will be incremented first before addition
System.out.println(b);        ⇐ It will print print 4
System.out.println(a + b++);  ⇐ 14 : b will be incremented after addition
System.out.println(b);        ⇐ It will print 5
Assignment Operators

Assignment operators are used to assign values to variables. Let’s assume we have one integer variable a whose value is 10 (int a = 10;). Based on the operator used, I will show you the result.

OperatorExampleShort DescriptionResult
=a = 10;Assign 10 to a.Value of a will be 10
+=a += 5;Perform a = a+5;15 (10 + 5)
-=a -= 5;Perform a = a-5;5 (10 – 5)
*=a *= 5;Perform a = a*5;50 (10 * 5)
/=a /= 5;Perform a = a/5;2 (10 / 5)
%=a %= 5;Perform a = a%5;0 (10 % 5)
Comparison Operators

Comparison operators are used to compare two values. The result of the comparison operator is always boolean (true or false). Let’s assume we have two integer variables a and b. Based on the operator used, I will show you the result.

int a = 10;
int b = 3;
Operator Name Example Result
== Equal to a == b false
!= Not Equal a != b true
> Greater than a > b true
< Less than a< b false
>= Greater than or equal a >= b true
<= Less than or equal a <= b false
Logical Operators

Logical operators are used to check whether an expression is true or false. The outcome of a logical operator is always boolean (true or false). Let’s assume we have two integer variables a and b.

int a = 10;
int b = 3;
OperatorNameDescriptionExampleResult
&&Logical ANDTrue only if all the expressions are true.a > 5 && b < 1false
||Logical ORTrue if any one of the expressions are true.a > 5 || b < 1true
!Logical NOTReverse the result from true to false and false to true.!(a > 5 || b < 1)false

Logical expressions are evaluated left to right. Meaning in case of a > 5 && b < 1, first a > 5 will be evaluated and then b < 1. The evaluation of a logical expression might exit in between before checking the entire expression. This is known as short circuit.

Let’s consider the Logical AND operator (&&). It checks whether all the expressions are true. So if it finds one expression is false, is it really necessary to check the remaining right side expressions? No, right. Because irrespective of the remaining expression, the result is always going to be false. This is exactly what short circuit means – ignoring the right-hand side if it isn’t necessary. Let’s check below example –

int a = 10;
int b = 3;
boolean result = a > 5 && b < 1;
  1. First it checks if a is greater than 5. The result is true. So it goes to the next expression.
  2. Then it checks if b is less than 1. This is false. So the result is false.

Here the short circuit didn’t happen as both the expressions are evaluated.

Now let’s reverse the expressions –

int a = 10;
int b = 3;
boolean result = b < 1 && a > 5;

First it checks if b is less than 1. This is false. So the result is always going to be false. So, the remaining expression (a > 5) is ignored. This is a short circuit.

 Similarly for Logical OR operator (||), if one expression is true, there is no need to evaluate the remaining right side expressions. Because the result is always going to be true.

I hope the concept of operators are clear now. We will meet again with a new topic.