# Deep Dive into Java Operators

by Vibrant Publishers

## Introduction:

One of the most basic requirements of a programming language is to perform mathematical operations. Java is an Object-Oriented Programming language. It provides a rich set of operators to manipulate variables. In this blog, let’s take a look at the different types of Java operators and a few examples of sample code.

We can classify the Java operators into the following groups:

- Arithmetic Operators
- Relational Operators
- Bit Operators
- Logical Operators
- Assignment Operator

## Arithmetic Operators:

Arithmetic operators are used for mathematical expressions. In the following table, you will find a list of all arithmetic operators, as well as their descriptions and example usage.

The examples in the table given below assume that the value of the integer variable X is 20 and the value of variable Y is 40.

One thing to note here is the number of operands involved in each case. While addition, subtraction, multiplication & Java modulo operators require two operands, the increment & decrement operators operate on a single operand.

The sample code given below will show all the use-cases of the above-mentioned operators in detail.

public class TestArithmaticOperators { public static void main(String [] args){ //operand variable declaration and initializatoin. int a = 5; int b = 22; int c = 16; int d = 33; System.out.printIn(“a + b =” + (a + b)); System.out.printIn(“a – b =” + (a – b)); System.out.printIn(“a * b =” + (a * b)); System.out.printIn(“b / a =” + (b / a)); System.out.printIn(“b % a =” + (b % a)); System.out.printIn(“c % a =” + (c % a)); System.out.printIn(“a++ =” + (a++)); System.out.printIn(“a– =” + (a–));
// Check the difference between d ++ and ++ d System.out.printIn(“d++ =” + (d++)); System.out.printIn(“++d =” + (++d)); } } |

The following output is thus produced:

$ javac TestArithmeticOperators.java $java -Xmx128M -Xms16M TestArithmaticOpertors a + b = 27 a – b = -17 a * b = 110 b / a = 4 b % a = 2 c % a = 1 a++ = 5 a– = 6 d++ = 33 ++d = 35 |

## Relational Operators:

When we want to relate or compare two values, we use relational operators in our Java code. Two operands are expected for the operator as inputs.The table given below will provide a detailed description of relational operators in Java.

Assuming the values of variables are X = 20 & Y = 40.

Let’s look at a sample code that uses the above operators:

public class code { public static void main (String [] args){
// operand varibale declaration and initialization. int a = 5; int b = 22; int c = 16; int d = 33;
System.out.println( “a == b =” + (a == b)); System.out.println( “a != b =” + (a != b)); System.out.println( “a < b =” + (a < b)); System.out.println( “a > a =” + (a > a)); System.out.println( “b <= a =” + (b <= a)); System.out.println( “c >= a =” + (c >= a)); } } |

After successful compilation, the above code will produce the following result:

Output:
a == b = false a != b = true a < b = true a > a = false b <= a = false c >= a = true |

## Bit Operator:

Java defines bit operators for integer types (int), long integers (short), short integers (char), and byte types (byte).

Bit operators operate on all bits and perform bit-level operations. There are four bitwise operations and three bitshift operations that bit operators perform.

For example, if we take Bitwise AND operator which is a bitwise operator, the operation will be as follows:

Suppose X = 60 and Y = 13; their binary (bit) representation would be as follows:

X = 0011 1100 Y = 0000 1101 |

Now when we perform a Bitwise AND operation, ie. if both bits are 1, the result is 1 else it’s 0. So here the result will be:

X & Y = 0000 1100 |

The following table gives a summary of the available bitwise operators and bit shift operators in Java:

## Logical Operators:

The following table lists the basic operations of logical operators, assuming Boolean variable X is true and variable Y is false.

## Assignment Operator:

We have discussed this section in detail here: Java Assignment Operator

## Other Operators:

## Instanceof Operator:

This operator is used to manipulate an object instance and check whether the object is a class type or interface type. The instanceof Java operator uses the following format:

(Object reference-variable) instanceof (class / interface type) |

If the object pointed to by the variable on the left side of the operator is an object of the class or interface on the right side of the operator, the result is true. Below is an example:

## Conditional Operator (?:):

The conditional operator in Java is also called ternary operator. The ternary Java operator has 3 operands. The conditional operator is used to determine the boolean values. The main purpose of this operator is to finalize which value among the two should be assigned to a variable.

The syntax would be:

variable test_variable = (expression)? value if true : value if false; |

## Java operator precedence:

When multiple operators are used in one Java statement, it is important to understand which one will act first. To solve this confusion, the Java language has something known as ‘operator precedence.’ This will allow the highest priority operators to act first and then the other operators act, following the precedence order. It’s important to understand that in a multi-operator expression, different operator precedence can lead to very different results.

## End Note:

We have learned about the different types of operators in Java language that are used for performing various mathematical operations. We also saw examples of their syntax.

Besides Java Operators, you must also be well-versed with the concepts of Flow control statements and Assertion, Java collections and Stream, Threading in Java, etc. You can take a look at our blogs on these topics for in-depth understanding.

## Get one step closer to your dream job!

Prepare for your Java programming interview with Core Java Questions You’ll Most Likely Be Asked. This book has a whopping 290 technical interview questions on Java Collections: List, Queues, Stacks, and Declarations and access control in Java, Exceptional Handling, Wrapper class, Java Operators, Java 8 and Java 9, etc. and 77 behavioral interview questions crafted by industry experts and interviewers from various interview panels. We’ve updated our blogs to reflect the latest changes in Java technologies.

This blog was previously uploaded on March 31th, 2020, and has been updated on January 12th, 2022.

Share