Previously, we have learned how to write a Hello World program, the varied data types and variables, object-oriented programming concepts and string handling. Now keeping the ball rolling, let us learn about distinct kinds of Java operators with examples and code.

First, let us have a look at what is an operator and what are the different types of operators used in Java.

Table of Contents

Operators are special characters that perform certain operations on one or more operands (characters on which operation is performed).

**The different types of operators used in Java are:**

- Arithmetic Operators in Java
- Assignment Operators in Java
- Equality & Relational Operators in Java
- Logical / Conditional Operators in Java
- Bitwise Operators in Java
- Unary Operators in Java
- Ternary Operators in Java

Now, let us know the functionality of all of the above-listed operators and learn how to use them.

Java offers its users the to perform basic arithmetic operations using +,-, /,*. In addition to these basic arithmetic operators, Java identifies % operator which is used to find the remainder of two operands.

Arithmetic Operator | Description |
---|---|

+ | To add two or more numbers. Also used to concatenate strings. |

– | To subtract the second operand from the first. |

* | To multiply two or more numbers. |

/ | To divide the first operand with the second, the remainder value is ignored. |

% | To return the remainder when the first operand is divided by the second operand. |

Let us have a look at how these arithmetic operators can be used.

public class Main { public static void main (String[] args) { int a = 10 + 20; //the value of a is now 30 System.out.println("10 + 20 = " + a); int b; b = a - 5; // the value of b is now 25 System.out.println(a + " - 5 = " + b); int c; c = b * 5; //the value of b is now 125 System.out.println(b + " * 5 = " + c); c= b / 5; // the value of b is now 25 System.out.println(b + " / 5 = " + c); c = b%2; // the value of b is now 1 System.out.println(b + " % 2 = " + c); } }

**Output:**

The above example program outputs the following result.

10 + 20 = 30 30 - 5 = 25 25 * 5 = 125 25 / 5 = 5 25 % 2 = 1

Assignment operators include basic assignment operators and shorthand assignment operators. An equal to (=) is the basic assignment operator used in Java, which is used to assign the value on the right-hand side to the variable on the left-hand side.

**Syntax:**

variable = value/expression.

**Example:**

int x = 10; // the value 10 is assigned to variable x.

Furthermore, apart from the basic assignment operator, Java defines arithmetic shorthand assignment operators.

Assignment Operator | Description |
---|---|

+= | Reads the value of left operand adds it to the operand on the right and stores the result back to the left-hand side variable. It is similar to a = a + 5. |

-= | Reads the value of left operand subtracts it with the operand on the right and stores the result back to the left-hand side variable. It is similar to a = a – 5. |

*= | Reads the value of left operand multiplies it with the operand on the right and stores the result back to the left-hand side variable. It is similar to a = a * 5. |

/= | Reads the value of left operand divides it by the operand on the right and stores the result back to the left-hand side variable. It is similar to a = a / 5. |

%= | Reads the value of left operand divides it by the operand on the right and stores the remainder obtained back to the left-hand side variable. It is similar to a = a % 5. |

Taking a sneak peek into how these assignment operators are actually used

public class Main{ public static void main(String args[]) { int a1 = 10; int a2 = 20; a2 += a1; // a2 = a2+a1 System.out.println("The result for += is: "+a2); a2 -= a1; //a2=a2-a1 System.out.println("The result for -= is: "+a2); a2 *= a1; //a2=a2*a1 System.out.println("The result for *= is: "+a2); a2 /= a1; //a2=a2/a1 System.out.println("The result for /= is: "+a2); a2 %= a1; //a2=a2%a1 System.out.println("The result for %= is: "+a2); } }

The above code snippet gives the following results:

The result for += is: 30 The result for -= is: 20 The result for *= is: 200 The result for /= is: 20 The result for %= is: 0

Java involves two equality operators, namely exactly equals to or equals to (= =) and not equal to (!=), which describes if the two values are equal or not. The relational operators illustrate if one operand is greater than or less than the other operand. The distinct relational operators that Java defines include <, >, <=, >=. The equality and relational operators together are considered as comparison operators.

Equality/Relational Operator | Description |
---|---|

== | It returns true if operands on both sides are equal to each other. |

!= | It returns true if operand on the left side is not equal to the operand on the right. |

< | It returns true if the operand on the left side is less than the operand on the right side. |

> | It returns true if the operand on the left side is greater than the operand on the right side. |

<= | It returns true if the operand on the left side is less than or equal to the operand on the right side. |

>= | It returns true if the operand on the left side is greater than or equal to the operand on the right side. |

Now, let us consider A and B are two integer type variables where A=50 and B=75 respectively.

Operator | Example : A =50, B=75 | Result Description |
---|---|---|

== |
A == B | It returns false because the values of A and B are not equal to each other. |

!= | A != B | It returns true because the values A and B are not equal to each other. |

< |
A< B | It returns true as the value of A is less than the value of B. |

> |
A>B | It returns false as the value of A is not greater than B. |

<= |
A<=B | It returns true as the value of A is less than the value of B |

>= |
A>=B | It returns false as the value of A is not greater than the value of B |

Table: Representing the results of equality and relational operators in Java

Java describes two conditional operators && (logical AND) and | | (logical OR) that evaluate 2 boolean expressions. These operators are also referred to as short circuit operators which imply that the second operand value is evaluated only when needed.

Let us assume that we have two expressions, A = x<y and B = y>z where the values of x, y, z are 10, 20, 30 respectively.

Logical/Conditional Operator | Description | Example : A =x<y, B=y>z | Result Description |
---|---|---|---|

&& | a. The operator returns true iff both operands are true. b. The operator returns false if either or both the operands are false. | A && B | It returns false because the expression A evaluates true and B false. |

| | |
a. The operator returns true if either or both are true. b. The operator returns false if both the operands are false. | A | | B | It returns true because the expression A evaluates true and B false. |

Table: Description and representation of logical/conditional operators in Java

The bitwise operators operate on individual bit level and can be applied only to integral values. Bitwise operators in Java include `~`

(bitwise not or complement), `&`

(bitwise AND), `|`

(bitwise OR), `^`

(bitwise exclusive OR/XOR). We even come across two types of bitwise shift operators `<<`

(left shift), and `>>`

(right shift).

Bitwise Operator | Description |
---|---|

& | A binary operator that compares two analogous values and returns 1 if either of the bits is 1 else returns a 0. |

| |
A binary operator that compares two analogous values and returns 1 if both of the bits are 1.It returns a 0 if either of the values is 1. |

~ |
A binary operator that is used to perform inversion operation, i.e., every 0 becomes 1 and every 1 turns to 0. |

^ |
A binary operator that compares two analogous values and returns 1 if either of the bits is different and it returns 0 if both the values are same. |

<< |
The operator shifts a bit to the left by a specified number of bits. |

>> |
The operator shifts a bit to the right by a specified number of bits. |

Let’s have a look at how these operators can be used:

public class Main{ public static void main(String args[]) { int a1 = 10; /* 10 = 00001010 */ int a2 = 20; /* 20 = 00010100 */ int result = 0; result = a1 & a2; System.out.println("The result of bitwise AND: "+result); result = a1 | a2; System.out.println("The result of bitwise OR: "+result); result = a1 ^ a2; System.out.println("The result of bitwise XOR: "+result); result = ~a2; System.out.println("The result of bitwise NOT: "+result); result = a1 << 2; // left shift 10 by 2 positions System.out.println("The result of left shift operator: "+result); result = a2 >> 2; //right shift 20 by 2 positions System.out.println("The result of right shift operator: "+result); } }

The output for the above program is as follows:

The result of bitwise AND: 0 The result of bitwise OR: 30 The result of bitwise XOR: 30 The result of bitwise NOT: -21 The result of left shift operator: 40 The result of right shift operator: 5

These kinds of operators generally perform the action over a single operand. The operations can be a negation, inversion of a value, incrementing the value or decrementing the value of the operand.

Unary Operator | Description |
---|---|

+ |
A unary operator that depicts that the corresponding value is positive. |

– | A unary operator that negates the corresponding operand. |

++ |
A unary operator that increments the value by 1. |

— | A unary operator that decrements the value by 1. |

! | A unary operator that returns the logical complement/ inversion of an operand. |

The increment and decrement operators come with two variants postfix and prefix. Both variants for auto increment perform the same operation but the prefix form first updates the value and returns it whereas the postfix variant returns the recent value of the operand and later increments it.

Confused !! Let’s try it hands-on to check what the outcome is.

public class Main{ public static void main(String args[]){ int i = 5; System.out.println("Postfix increment: "+ i++); // outputs 5 int j = 2; System.out.println("Prefix increment: "+ ++j); // outputs 3 } }

The output of the above example program is as follows:

Postfix increment: 5 Prefix increment: 3

Let us mull over the syntax of this operator to know how it works.

**Syntax:**

variable <name_of_variable> = expression ? value1 if true : value2 if false

From the above syntax, if the value of the evaluating expression turns out to be true then value 1 is returned (the one before the colon ‘:’) else value2 is returned (the value after the colon).

The following example will clear out the doubts if any,

public class Main { public static void main(String args[]) { int a1, result; a1 = 75; result = (a1 == 25) ? 500: 250; //returns the value after ':' because a1 is not equal to 75 System.out.println( "The result is: "+result); result = (a1 == 75) ? 500: 250; //returns the value before ':' because a1 equals 75 System.out.println( "The result is: "+result); } }

The output is as follows:

The result is: 250 The result is: 500

So far we have explored how each kind of operator works in Java. Now before closing the operators tutorial let’s know what’s the precedence of these operators when used in an expression.

Generally, the highest precedence is evaluated before the lowest precedence one.

Java Operator | Precedence | Associativity |
---|---|---|

Postfix |
++, — |
Right to Left |

Unary |
++, –, +, -, ~, ! | Right to Left |

Multiplicative |
*, /, % | Left to Right |

Additive |
+, – | Left to Right |

Shift |
<<, >> | Left to Right |

Relational |
<, <=, >, >= | Left to Right |

Equality |
==, != | Left to Right |

Bitwise AND |
& | Left to Right |

Bitwise XOR |
^ | Left to Right |

Bitwise OR |
| | Left to Right |

Logical AND |
&& | Left to Right |

Logical OR |
|| | Left to Right |

Ternary |
?: | Right to Left |

Assignment |
=, +=, -=, *=, /=, %= | Right to Left |

In this tutorial on Java operators with examples, we have learned about the different types of operators that Java defines and their functionality in detail. We have even looked into briefly the order of precedence of operators and their associativity. To understand how to use operators more conveniently, keep practicing.

Happy Learning!!