8 Types of Java Operators – Nourish Your Fundamentals

Java Operators are the symbol which tells the interpreter or compiler to execute specific task like – arithmetic, relational, logical, etc. to get desired results. Java is a very popular language. There are many features and operators in java which are used to perform different operations.

Today, we come up with the different types of operators with their examples to learn and master the concept quickly.

Before we start, you should know what are classes and objects in Java.

So, get ready to explore Java Operators.

Types of Java Operators

Java Operators are used for performing many operations. Following are the types of operators available in Java.

types of operators in java

Let’s discuss these Java operators in detail:

1. Arithmetic Operators in Java

Java arithmetic operator takes numerical values as operands, operates on them, and returns a single numerical value. Java Arithmetic operators are used for simple mathematic operations, they are

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

Do you know what are Strings in Java?

Example-

package JavaOperatorsDemo;
public class UsingArithematicOperator {
 public static void main(String[] args)
	         {
	             int operand1 = 20, operand2 = 10;
	             String stringname1 = "DataFlair's", stringname2 = " Java Tutorial";
	  // using + operator with strings it will concatenate the strings
	             System.out.println("Welcome to "+stringname1 +stringname2);
	  // using + and - arithmetic operator
	             System.out.println("Adding(+) two operands= "+(operand1 + operand2));
	             System.out.println("Subtracting(-) two operands= "+(operand1 - operand2));
	  // * and / operator
	             System.out.println("Multiplying(*) two operands= "+(operand1 * operand2));
	             System.out.println("Dividing(/) two operands= "+(operand1 / operand2));
	 // modulus operator gives remainder on dividing first operand with second
	             System.out.println("Modulus(%) of two operands= "+(operand1 % operand2));
	         }
	   }

Output-
Arithematic Operator

2. Unary Operators in Java

Unary Operator operates on a single value. Following are the unary operators:

Operator NameDescription
Unary Minus (-)For decreasing the value
Unary Plus (+)For converting the negative values into positive
Increment operator (++)Used for increasing value of the operand by 1
Post-IncrementThe value is incremented first then the result is computed
Pre-IncrementThe value is incremented later first the result is computed
Decrement OperatorUsed for decreasing the value of operand by 1
Post-DecrementThe value is decremented first then the result is computed
Pre-DecrementThe value is decremented later first the result is computed
Logical not Operator (!) Used for inverting the values of boolean

Example-

package JavaOperatorsDemo;
public class UsingUnaryOperator {
  // Java program to illustrate unary operators
public static void main(String[] args)
            {
            int operand1 = 20, operand2 = 10, operand3 = 0, operand4 = 20, operand5 = 40;
            boolean condition = true;
  // pre-increment operator
            operand3 = ++operand1;// operand1 = operand1 + 1
            System.out.println("Value of operand3 (++operand1) = " + operand3);//operand3 = operand1;
  // post increment operator
             operand3 = operand2++;// operand3 = b
             System.out.println("Value of operand3 (operand2++) = " + operand3);//operand2 = operand2 + 1
  // pre-decrement operator
             operand3 = --operand4;// operand4 = operand4-1 
             System.out.println("Value of operand3 (--operand4) = " + operand3);//operand3=operand4
  // post-decrement operator
             operand3 = --operand5;// operand3 = operand5
             System.out.println("Value of operand3 (--operand5) = " + operand3);// operand5 = operand5 - 1
  // Logical not operator
             System.out.println("Value of !condition =" + !condition);
           }
      }

Output-

Unary Operator

3.  Assignment Operators in Java

An assignment operator assigns a value to the left operand based on the value of its right operand with the help of equals = sign. Thus, x = y assigns the value of y into x. Its types are,

Operator NameDescription
+=To add the right and left operator and then assigning the result to the left operator
-=To subtract the two operands on left and right and then assign the value to the left operand
*=To multiply the two operands on left and right and then assign the value to the left operand
/=To divide the two operands on left and right and then assign the value to the left operand
^=To raise the value of left operand to the power of right operator
%=To apply modulus operator

Recommended Reading – Primitive and Non-primitive Data Types in Java

Example-

package JavaOperatorsDemo;
public class UsingAssignmentOperator {
  // Java program to illustrate assignment operators
 public static void main(String[] args)
          {
             int operand1 = 20, operand2 = 10, operand3 = 10, operand4 = 4;
  // simple assignment operator
             operand3 = operand2;
             System.out.println("Value of operand3 = " + operand3);
  // This following statement would throw an exception as value of right operand must be initialised before an assignment, and the program would not compile.
  // operand3 = operand4;
  // instead of below statements,shorthand assignment operators can be used to provide same functionality.
             operand1 = operand1 + 1;
             operand2 = operand2 - 1;
             operand3 = operand3 * 2;
             operand4 = operand4 / 2;
  System.out.println("operand1,operand2,operand3,operand4 = "
             + operand1 + "," + operand2 + "," + operand3 + "," +operand4);
             operand1 = operand1 - 1;
             operand2 = operand2 + 1;
             operand3 = operand3 / 2;
             operand4 = operand4 * 2;
  // shorthand assignment operator
             operand1 += 1;
             operand2 -= 1;
             operand3 *= 2;
             operand4 /= 2;
  System.out.println("operand1,operand2,operand3,operand4 (using shorthand operators)= " +
             operand1 + "," + operand2 + "," + operand3 + "," + operand4);
         }
    }

Output-

Assignment Operator

4. Relational Operators in Java

Relational Java operators are used to check the equality of operands. We can also use them for comparison of two or more values.

Operator NameExampleDescription
== (equals to) x==yTrue if x equals y, otherwise false
!= (not equal to)x!=yTrue if x is not equal to y, otherwise false
< (less than) x<yTrue if x is less than y, otherwise false
> (greater than)x > yTrue if x is greater than y, otherwise false
>= (greater than or equal to) x>=yTrue if x is greater than or equal to y, otherwise false
<= (less than or equal to) x<=yTrue if x is less than or equal to y, otherwise false

Example-

package JavaOperatorsDemo;
public class UsingRelationalOperator {
  public static void main(String[] args)
  {
  int operand1 = 20, operand2 = 10;
  String stringName1 = "DataFlair's ", stringName2 = "Java Tutorial";
  int ar[] = { 1, 2, 3 };
  int br[] = { 1, 2, 3 };
  boolean condition = true;
  //various conditional operators
  System.out.println("operand1 == operand2 :" + (operand1 == operand2));
  System.out.println("operand1 < operand2 :" + (operand1 < operand2));
  System.out.println("operand1 <= operand2 :" + (operand1 <= operand2));
  System.out.println("operand1 > operand2 :" + (operand1 > operand2));
  System.out.println("operand1 >= operand2 :" + (operand1 >= operand2));
  System.out.println("operand1 != operand2 :" + (operand1 != operand2));
  // Arrays cannot be compared with relational operators because objects store references not the value
  System.out.println("stringName1 == stringName2 : " + (ar == br));
  System.out.println("condition==true :" + (condition == true));
  }
  }

Output-

Relational Operator

It’s right to explore the concept of Variables in Java

5. Logical operators in Java

Operator NameDescription
&& (Logical AND)Returns the value if both the conditions are true otherwise returns zero.
II (Logical OR)Returns the value even if one condition is true

Example-

package JavaOperatorsDemo;
import java.util.Scanner;
public class UsingLogicalOperator {

  public static void main(String[] args)
  {
  String stringName1 = "DataFlair";
  String stringName2 = "Techvidvan";
  Scanner scannerClassObject = new Scanner(System.in);
  System.out.print("Enter username:");
  String userID = scannerClassObject.next();
  System.out.print("Enter password:");
  String userPassword = scannerClassObject.next();
  // Check if user-name and password match or not.
  if ((userID.equals(stringName1) && userPassword.equals(stringName2)) ||
  (userID.equals(stringName2) && userPassword.equals(stringName1))) {
  System.out.println("Welcome user.");
  }
  else
  {
  System.out.println("Wrong userID or Password");
  }
  }
  }

Output when the userID and Password is correct-

Logical Operator Correct

Output when the userID and password is incorrect-

Logical Operator

6. Ternary Operators

Ternary operators in Java are a shorthand version of ‘if-else’ statements.

Syntax of Ternary Operator is- condition?value1: value2. First value i.e. value1 executes if the condition is true and if the condition is false value2 executes.

Example-

package JavaOperatorsDemo;

public class UsingTernaryOperator {
  public static void main(String[] args)
  {
  int operand1 = 20, operand2 = 10, operand3 = 30, result;
  //result holds max of three numbers
  result = ((operand1 > operand2) ? (operand1 > operand3) ? operand1 :
  operand3 : (operand2 > operand3) ? operand2 : operand3);//using ternary operator
  System.out.println("Maximum out of three numbers = "+result);
  }
  }

Output-

Ternary Operator

Don’t forget to check the Access Modifiers in Java

7.  Bitwise Operators

Bitwise operators are used to perform operations on single bitwise values. First, it converts the numerical value to its binary representation and then it performs the necessary operation and gives the result.

Operator NameDescription
Java Bitwise AND operator (&)The & operator compares corresponding bits of two operands. If both bits are 1, it gives 1 else 0.
Java Bitwise OR operator (|)The | operator compares corresponding bits of two operands. If either of the bits is 1, it gives 1 else 0.
Bitwise XOR operator (^)The ^ operator compares corresponding bits of two operands. If corresponding bits are different, it gives 1 else 0.
Bitwise Complement operator (~)The ~ operator inverts the bit pattern. It makes every 0 to 1 and every 1 to 0.

Example-

package JavaOperatorsDemo;
public class UsingBitwiseOperator {
  public static void main(String[] args)
  {
  int operand1 = 0x0005;
  int operand2 = 0x0007;
  // bitwise AND
  // 0101 & 0111 = 0101
  System.out.println("operand1 & operand2 = " + (operand1 & operand2));
  // bitwise OR
  // 0101 | 0111=0111
  System.out.println("operand1 | operand2 = " + (operand1 | operand2));
  // bitwise Complement
  // ~ 0101 = 1010
  System.out.println("~ operand1 = " + ~operand1);
  // bitwise XOR
  // 0101 ^ 0111 = 0010
  System.out.println("operand1 ^ operand2 = " + (operand1 ^ operand2));
  
  // can also be combined with assignment operator to provide shorthand assignment
  // operand1 = operand1 & operand2
  operand1 &= operand2;
  System.out.println("operand1= " + operand1);
  }
}

Output-

Bitwise Operators in Java

8. Shift Operators

Operator NameDescription
<< (left shift operator)It shifts x in binary representation y bits to the left, shifting in zeros from the right.
>> ( right shift operator)It shifts x in binary representation y bits to the right, discarding bits shifted off.
>>> (unsigned right shift operator)It shifts x in binary representation y bits to the right, discarding bits shifted off, and shifting in zeros from the left.

Example-

package JavaOperatorsDemo;
public class UsingShiftOperator 
{
  public static void main(String[] args)
  {
  int a = 0x0005;
  int b = -10;
  // left shift operator
  // 0000 0101 << 2 = 0001 0100
  System.out.println("a << 2 = " + (a << 2));
  // right shift operator
  // 0000 0101 >> 2 =0000 0001
  // similar to 5 / (2 ^ 2)
  System.out.println("a >> 2 = " + (a >> 2));
  // unsigned right shift operator
  System.out.println("b >>> 2 = "+ (b >>> 2));
  }
  }

Output-

Shift Operators in Java

Summary

Java operators are one of the most important fundamental concepts. Operators help programmers to perform a specific operation (Arithmetic, Unary, Assignment, Relational, Logical, Ternary, Bitwise, and Shift operation). Until and unless fundamentals are not clear, one can’t master the language. So, try hard and make your foundation solid.

It’s not the end, it’s time to learn Loops in Java

Hope, you liked the explanation. Share your experience with us!

12 Responses

  1. Sarika Bhutare says:

    Very nice study material.

    • Data Flair says:

      Hi Sarika,
      Thank you, for taking a part in our journey and commenting on our “Java Operators” Tutorial. We are always motivated after reading this kind of loyal feedbacks.
      Keep Visiting and Keep Learning.
      Regards,
      Data-Flair

  2. SURYA says:

    The best and helpful.

    • Data Flair says:

      Your Appreciation means a lot to us, we keep trying to give a detailed and advanced piece of information for our readers.
      “Education is the most powerful WEAPON” which you can use to CHANGE the WORLD”
      Regards,
      Data-Flair

  3. Amarnathreddy says:

    Good study material

    • Data Flair says:

      Hi Amarnathreddy,
      We are glad you comment on “Java Operators Tutorial”.
      Comments like these let us know we’re on the right track trying to deliver the best content to our readers. You should also check out our Java Quizl, to check your Java Knowledge.
      Regards,
      Data-Flair

  4. Divyansh says:

    Please provide all outputs

  5. Nihal Singh says:

    Java is a little difficult for the students but it is most important for all the applications to develop the software. So students compulsory study java programming still the students get a burden to learn but Your article helps to learn the program for the students. Thanks for sharing

    • DataFlair Team says:

      Hello Nihal,
      Thanks for the information and yes, Java is one of the highly demanding languages of the IT sector. The importance of Java is increasing day by day. To meet the industrial requirements, we have 100+ Java tutorials from beginners to expert.
      So, complete all the tutorials and ready to knock the doors of your desired company.

  6. hritika says:

    Points are easy t understand and thanks for programs describe with points
    Do your website provide projects for final year computer engineering students of diploma with java source code

  7. Jeffrey says:

    Hello, love most of the content provided by this website. I noticed an error though.

    “Post-Increment The value is incremented first then the result is computed”
    “Pre-Increment The value is incremented later first the result is computed”

    This should be reversed.

    Pre-increment: value is incremented first, then it’s computed.
    Post-increment: value is computed first, then incremented.

  8. Jeffrey says:

    Forgot to include the same error for decrement.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.