Operators in Java

In this tutorial we are going to learn about the operators in java and how vital they are for doing different operations. We will use examples for deep understanding.


Java has a big collection of operators that can be used to perform operations on values in variables. We classify these operators based on the functionality that we provide. Some of the operators in Java are:

  1. Arithmetic operators
  2. Assignment operators
  3. Logical operators
  4. Comparison operators
  5. Bitwise operator

operators in java

Recommended Books



Arithmetic Operators

These type of operators are used for applying arithmetic operations on primitive data types such as:

  1. Multiplication
  2. Division
  3. Modulo
  4. Addition
  5. Subtraction

Let’s write a program to illustrate it further:

public class MyClass {
public static void main(String args[]) {
int x=10;
int y=25;



//Addition and Subtraction
System.out.println("Sum of x+y = " + (x + y));
System.out.println("Difference of x-y = " + (x - y));

//Multiplcation and Division
System.out.println("Multiplication of x*y = " + (x * y));
System.out.println("Division of x/y = " + (x / y));

//Modulo gives remainder
System.out.println("Remainder of x%y = " + (x % y));


}
}

Assignment operator

This type of operator is used to assign or connect a value to any variable. Sentence of declaring for assigning an operator between variable and value is:

variable = value;

However, there are few cases when we want to create a shorter version of a statement called compound statement. So instead of writing sum = sum + 10, we can write sum += 10. It is important to remember that the arithmetic operator you want to utilise should be starting from left to right ( += ). Let’s look at an example:

public class MyClass {
public static void main(String args[]) {
int x=10;
int y=25;


//Assigning Operator
x = y;

//Printing Values
System.out.println("Value of x = y is " + x);
System.out.println("Value of x += y " + (x+=y));


}
}

Relational operators

Relational operators or comparison operators are used for checking comparing equality, greater than equal to signs etc. Some of the comparison operators are:

  1. Equal to ( == )
  2. Not equal ( != )
  3. Greater than ( > )
  4. Less than ( < )
  5. Greater than or equal to ( >= )
  6. Less than or equal to ( <= )




Logical operators

Logical operators are used for checking the logic between two two values, some of the logical operators are:

  1. Logical and ( && ) returns true both conditions are met.
  2. Logical or ( || ) returns true only if one of the condition is met.
  3. Logical not ( ! ) it shows a reverse result, means it turned out to be false when the result is true.

Let’s look at a program to see how these conditions are met:

public class MyClass {
public static void main(String args[]) {
int x=10;
int y=25;

//Logical operators
System.out.println(x > 1 && y < 30);


}
}

The above code execution will turn out to be true since all the given conditions are met.

Bitwise operators

Bitwise operators are used for performing binary logic on the bits of an integer. Sum of the bitwise operators are:

  1. Bitwise and operator ( & )
  2. Bitwise or operator ( | )
  3. Bitwise XOR operator ( ^ )
  4. Bitwise complement operator ( ~ )