HomeJavaOperators in Java

Operators in Java

In this article, you will be learning about the operators in Java, its usage and examples.

Operators in Java

Operators are generally special symbols that perform specific operations on one, two, or three operands, and then return a result. Java provides a vast set of operators to manipulate variables.They are classified based on the functions they provide. The following operators are available in Java programming:

  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Misc Operators

Let’s see each operator in detail.

Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations such as addition, subtraction etc.

Let’s see the arithmetic operators and its operations

OperatorOperation
+Addition
Subtraction
*Multiplication
/Division
%Modulo Operation

Sample program using arithmetic operator

Input

public class Main {
  public static void main(String[] args) {
    int a = 7, b = 5;

    System.out.println("Addition of a + b = " + (a + b));
    System.out.println("Subtraction of a - b = " + (a - b));
    System.out.println("Multiplication of a * b = " + (a * b));
    System.out.println("Division of a / b = " + (a / b));
    System.out.println("Modulus if a % b = " + (a % b));
  }
}
Output
Addition of a + b = 12
Subtraction of a - b = 2
Multiplication of a * b = 35
Division of a / b = 1
Modulus if a % b = 2

Relational Operators

Relational operators are used to check the relationship between two operands. They primarily return results in Boolean. They are extensively used in looping statements as well as conditional if else statements.

Syntax
variable relation_operator value

The following operators are relational operators.

OperatorDescription
==Is Equal To
!=Not Equal To
>Greater Than
<Less Than
>=Greater Than or Equal To
<=Less Than or Equal To
Sample program using a relational operator
Input
public class Main {
  public static void main(String[] args) {
    int a = 7, b = 5;
    System.out.println( "Checking if a equal to b : " + ( a == b ) );  
    System.out.println("Checking if a is not equal to : " + (a != b) ); 

    System.out.println("Checking if a is greater than : " +(a &gt; b));  
    System.out.println("Checking if a is lesser than : " +(a &lt; b));  
    System.out.println("Checking if a is greater than or equal to b : " +(a &gt;= b));

    System.out.println( "Checking if a is lesser than or equal to b : " +(a&lt;= b)); 
  }
}
Output

Checking if a equal to b : false
Checking if a is not equal to : true
Checking if a is greater than : true
Checking if a is lesser than : false
Checking if a is greater than or equal to b : true
Checking if a is lesser than or equal to b : false

Bitwise Operators

Bitwise operators in Java are used to perform operations on individual bits. Below are the bitwise operators and their functions in detail

OperatorDescription
~Bitwise Complement
<<Left Shift
>>Right Shift
>>>Unsigned Right Shift
&Bitwise AND
^Bitwise exclusive OR

Logical Operators

Logical operators are used to check whether an expression is true or false. They are used in decision making.

OperatorMeaning
&&(Logical AND)
 (Logical OR)
!(Logical NOT)
Sample program using logical operators in Java
Input
public class Main {
  public static void main(String[] args) {

    System.out.println((5 &gt; 7) &amp;&amp; (7&gt; 5)); 
    System.out.println((5 &gt; 7) &amp;&amp; (7 &lt; 5));

    System.out.println((5 &lt; 7) || (7 &gt; 5));  
    System.out.println((5 &gt; 7) || (7 &lt; 5));  
    System.out.println((5 &lt; 7) || (7 &lt; 5));  
    System.out.println(!(5 == 7));
    System.out.println(!(5 &gt; 7));  
  }
}
Output
false
false
true
false
true
true
true

Assignment Operators

Assignment operators in Java are used to assign values to the designated variables The following assignment operators are supported in Java programming

|+=|It is used for adding left operand with right operand and then assigning it to variable on the left.| |- |-=|It is used for subtracting left operand with right operand and then assigning it to variable on the left. |*=| It is used for multiplying left operand with right operand and then assigning it to variable on the left.| |/=|It is used for dividing left operand with right operand and then assigning it to variable on the left.| |%=| It is used for assigning modulo of left operand with right operand and then assigning it to variable on the left|

Sample program using assignment operators in Java programming
Input
public class Main{  
public static void main(String args[]){  
int a=57;  
int b=75;  
a+=5; 
b-=6;  
System.out.println(a);  
System.out.println(b);  
}}
Output
62
69
Misc operator

Misc operators or the miscellaneous operators . There are two types of misc operators used in Java. They are:

Conditional Operator ( ? : )

Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions.

Syntax
variable x = (expression) ? value if true : value if false
Sample program
Input
class Java {
  public static void main(String[] args) {

    int februaryDays = 29;
    String result;

     result = (februaryDays == 28) ? "Not a leap year" : "Leap year";
    System.out.println(result);
  }
}
}
Output
Leap year
instanceof Operator

instanceof operator is more of a keyword that is used for checking if the reference variable is containing a given type of object or not

Syntax
(Object reference variable ) instanceof  (class/interface type)
Sample program for instanceof operator in Java
Input
public class Main {
  public static void main(String[] args) {

    String str = "CodersEditor";
    boolean result;

    result = str instanceof String;
    System.out.println("Is str an object of String? " + result);
  }
}
Output
Is str an object of String? true

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
  • Java
  • March 6, 2023
Java is a programming language and computing platform that is used to create various types of applications. It is used...
  • Java
  • March 6, 2023
In this post, you’ll learn how to download and install JUnit in Eclipse so that you can use in your...
  • Java
  • October 9, 2022