Types of Java Operators – Nourish Your Fundamentals

Free Java courses with 37 real-time projects - Learn Java

Java Operators  are of prime importance in Java. Without operators we wouldn’t be able to perform logical , arithmetic calculations in our programs. Thus having operators is an integral part of a programming language.

Java operators

Java Operators

Operators in Java are the special type of tokens in Java which when coupled with entities such as variables or constants or datatypes result in a specific operation such as addition, multiplication or even shifting of bits.

Java Operators are mainly of the following types:

  • Arithmetic Operators
  • Logical Operators
  • Unary Operators
  • Assignment Operators
  • Ternary Operators
  • Relational Operators
  • Bitwise Operators
  • Shift Operators
  • instanceOf operator

1. Arithmetic Operators in Java

Java Arithmetic Operators are used to perform arithmetic operations. There are mainly 5 Arithmetic Operators in Java.

a. Addition(+)
b. Multiplication(*)
c. Subtraction(-)
d. Division(/)
e. Modulo(%)

The Addition operator performs addition between two entities on either side of the operator

The Multiplication operator performs multiplication between two entities on either side of the operator.

The Subtraction operator performs subtraction between two entities on either side of the operator

The Division operator performs division and returns the quotient value of the division.

The Modulo operator returns the remainder after dividing the two operands.

Java program to illustrate Arithmetic Operators:

package com.dataflair.operators;
import java.io. * ;
public class ArithmeticOperator {
  public static void main(String[] args) throws IOException {
    int add,
    sub,
    mul,
    div,
    mod;
    int num1 = 5,
    num2 = 8;
    add = num1 + num2;
    sub = num1 - num2;
    mul = num1 * num2;
    div = num1 / num2;
    mod = num2 % num1;
    System.out.println("Addition num1+num2 " + add);
    System.out.println("Subtraction num1-num2 " + sub);
    System.out.println("Multiplication num1*num2 " + mul);
    System.out.println("Division num1/num2 " + div);
    System.out.println("Modulus num2%num1 " + mod);
  }
}

Output:

Addition num1+num2 13
Subtraction num1-num2 -3
Multiplication num1*num2 40
Division num1/num2 0
Modulus num2%num1 3

2. Logical Operators in Java

Java Logical Operators, as the name suggests, perform logical operations on the two operands. There are primarily two types of logical operators

a. Logical AND in Java

Java Logical AND checks whether the two conditions on either side of the expression is true. If both the expressions are true then it returns false.

b. Logical OR in Java

This checks whether either of the two conditions in the expression is true. If any one of the expressions is true, it evaluates to true. However if none of the conditions are true then it returns false. One thing to note is that if the first expression is already true it doesn’t check the second expression and returns true.

Java program to illustrate Logical Operators in Java:

package com.dataflair.operators;
import java.io. * ;
import java.util. * ;
public class LogicalOperator {
  public static void main(String[] args) throws IOException {
    Scanner sc = new Scanner(System. in );
    String uname;
    String course;
    System.out.println("Enter the  username");
    System.out.println("Enter the course");
    uname = sc.next();
    course = sc.next();
    if (uname.equals("DataFlair") && course.equals("Java")) {
      System.out.println("Your course is going on at DataFlair");
    }
    System.out.println("Enter the  username");
    System.out.println("Enter the course");
    uname = sc.next();
    course = sc.next();
    if (uname.equals("DataFlair") || course.equals("Python")) {
      System.out.println("You are either studying at Dataflair or studying python");
    }

  }
}

Output:

Enter the username
Enter the course
DataFlair
Java
Your course is going on at DataFlair
Enter the username
Enter the course
DataFlair
Ruby
You are either studying at Dataflair or studying python

3. Unary Operator in Java

Unary operators are those which have only one operand. They are of the following types

a. Unary plus Operator
b. Unary minus Operator
c. Increment Operator
d. Decrement Operator
e. Logical Not Operator

a. Unary plus operator in Java

The Unary plus operator converts byte short and character datatype values into integer values. However it is redundant since an explicit conversion of character to integer can also be done.
Characters when converted to integers return their ASCII value.

b. Unary Negative Operator in Java

Deliberately converts a positive value to a negative value.

c. Increment Operator in Java

Java increment operator is used for increasing the value of a particular value by one. However there are a few concepts linked with the increment operator.

  • Pre-increment- In this case, the value of the variable is first increased and then its used/ or its result is computed. Example- ++a;
  • Post-increment- In this case the value of the variable is first computed and then incremented. Example- a++;

d. Decrement Operator in Java

Java decrement operator is just as opposite to the increment operator. It decreases the value of the variable by 1.

  • Pre-decrement- In this case the value of the variable is first decreased and then computed. Example- –a;
  • Post-decrement- In this case the value of the variable is first computed and then decremented by 1. Example- a–;

e. Logical Not Operator in Java

Java logical Not operator flips the value of a boolean value. It is denoted by a !.

Java program to understand the concept of Unary Operators:

package com.dataflair.operators;
import java.io. * ;
import java.util. * ;
class UnaryOperator {
  public static void main(String[] args) throws IOException {
    int num1 = 10,
    num2 = 5,
    res;
    int num3 = 1;
    boolean flag = true;
    char character = 'a';

    res = +character; //The unary + converts  the character into a integer value
    System.out.println("The + operator on character transforms it to ASCII value " + res);
    num3 = -num3;
    System.out.println("The - operator on num1 positive  value " + num3);
    res = num1++; //First res=num1 then num1++ executed
    System.out.println("The res=num1++ returned value of " + res);
    res = ++num1; //First num1=num1+1 then res=num1
    System.out.println("The res=++num1 returned num1 value of " + res);
    res = num2--; //First res=num2 then num2=num2-1 executed
    System.out.println("The res=num2-- returned value of " + res);
    res = --num2; //First num2=num2-1 then res=num2 executed
    System.out.println("The res=--num2 returned num1 value of " + res);
    //Learning about NOT operator
    System.out.println("The NOT operator returns num1 value of " + !flag);

  }
}

Output:

The + operator on character transforms it to ASCII value 97
The – operator on num1 positive value -1
The res=num1++ returned value of 10
The res=++num1 returned num1 value of 12
The res=num2– returned value of 5
The res= –num2 returned num1 value of 3
The NOT operator returns num1 value of false

4. Assignment Operators in Java

Java Assignment operators are used to assign values to the variables on the left side of the equals sign. The associativity is from right to left. Meaning that the values on the right are assigned to the values on the left. The right hand side has to be a constant or a defined variable.

There are the following types of assignment operators
a. += This returns left=left+right
b. -= This returns left=left-right
c. *= This returns left=left*right
d. /= This returns left=left/right
e. %=This returns left=left%right

An example of java assignment operator:

package com.dataflair.operators;
import java.io. * ;
import java.util. * ;
public class AssignmentOperator {
  public static void main(String[] args) throws IOException {
    int num1 = 10,
    num2 = 5,
    result;
    num1 += 5; //action of num1=num1+ 5
    System.out.println("The output of num1+=5 is " + num1);
    num1 -= 5; //action of num1=num1+ 5
    System.out.println("The output of num1-=5 is " + num1);
    num1 *= 5; //action of num1=num1+ 5
    System.out.println("The output of num1*=5 is " + num1);
    num1 /= 5; //action of num1=num1+ 5
    System.out.println("The output of num1/=5 is " + num1);
    num1 %= 5; //action of num1=num1+ 5
    System.out.println("The output of num1%=5 is " + num1);
  }
}

Output:

The output of num1+=5 is 15
The output of num1-=5 is 10
The output of num1*=5 is 50
The output of num1/=5 is 10
The output of num1%=5 is 0

5. Ternary Operators in Java

Java ternary operator minimizes and mimics the if else statement. It consists of a condition followed by a question mark(?). It contains two expressions separated by a colon(:). If the condition evaluates to true, then the first expression is executed, else the second expression is executed.

Syntax :
(Condition)?(expression 1):(expression 2);

Java program to evaluate ternary operator:

package com.dataflair.operators;
import java.io. * ;
import java.util. * ;
public class TernaryOperator {
  public static void main(String[] args) throws IOException {
    int num1 = 10,
    num2 = 5,
    result;
    String result = (num1 == 10) ? "num1 has value of 10": "num1 does not have value of 10";
    System.out.println(s); //the value of s is printed
    result = (num2 == 10) ? "num2 has value of 10": "num2 does not have value of 10";
    System.out.println(result); //the value of result is printed

  }

}

Output:

num1 has value of 10
num2 does not have value of 10

6. Relational Operators in Java

Java Relational Operators are used to check the relation between values or variables. The output of relational operators is always a boolean value. Relational operators are used by if conditions and for loop constraints.

Some relational operators are:
a. <(Less than) -returns true if left entity lesser than right entity
b. >(Greater than)- returns true if left entity greater than right entity.
c. <=(Less than or equal to)- returns true if the left entity is smaller than or equal to right entity.
d. >=(Greater than or equal to)- returns true if left variable is greater than or equal to right entity
e. ==(equals to) – returns true if the left and the right entities are equal
f. !=(not equals to) – returns true if left and right entities are not equal.

Program to implement java relational operators:

package com.dataflair.operators;
import java.io. * ;
import java.util. * ;
public class RelationalOperator {
  public static void main(String[] args) throws IOException {
    int num1 = 10,
    num2 = 5,
    e;
    System.out.println("The values of num1 and num2 are " + num1 + " " + num2 + " respectively");
    System.out.println("The value returned when num1>num2 is checked " + (num1 > num2));
    System.out.println("The value returned when num1<num2 is checked " + (num1 < num2));
    System.out.println("The value returned when num1>=num2 is checked " + (num1 >= num2));
    System.out.println("The value returned when num1<=num2 is checked " + (num1 <= num2));
    System.out.println("The value returned when num1==num2 is checked " + (num1 == num2));
    System.out.println("The value returned when num1!=num2 is checked " + (num1 != num2));

  }

}

Output:

The values of num1 and num2 are 10 5 respectively
The value returned when num1>num2 is checked true
The value returned when num1<num2 is checked false
The value returned when num1>=num2 is checked true
The value returned when num1<=num2 is checked false
The value returned when num1==num2 is checked false
The value returned when num1!=num2 is checked true

7. Bitwise Operators in Java

Java Bitwise operators are generally used to perform operations on bits of data. The individual bits of a number are considered in calculation and not the entire number itself. There are the following types of bitwise operators:

a. AND(&)
b. OR(|)
c. XOR(^)
d. COMPLEMENT(~)

Java program to understand the Bitwise Operators:

package com.dataflair.operators;
import java.io. * ;
import java.util. * ;
public class BitwiseOperator {
  public static void main(String[] args) throws IOException {
    int num1 = 10,
    num2 = 5,
    e;
    //num1=10 1010
    //num2=5  0101

    System.out.println("The result after performing num1&num2 " + (num1 & num2));
    System.out.println("The result after performing num1|num2 " + (num1 | num2));
    System.out.println("The result after performing num1^num2 " + (num1 ^ num2));
    System.out.println("The result after performing ~num2 " + (~num2));

  }

}

Output:

The result after performing num1&num2 0
The result after performing num1|num2 15
The result after performing num1^num2 15
The result after performing ~num2 -6

8. Shift Operators in Java

Java Shift Operators are also a part of bitwise operators. There are the following types of shift operators.

a. Left Shift- Shifts the bits of the number two places to the left and fills the voids with 0’s.
b. Right Shift- Shifts the bits of the number two places to the right and fills the voids with 0’s The sign of the number decides the value of the left bit.
c. Unsigned RIght Shift- It is also the same as the right shift however it changes the leftmost digit’s value to 0.

Example program in Java to understand shift operators:

package com.dataflair.operators;
import java.io. * ;
import java.util. * ;
public class ShiftOperator {
  public static void main(String[] args) throws IOException {
    int num1 = 10,
    num2 = 5,
    e;
    //num1=10 1010
    //num2=5  0101
    System.out.println("The result after performing left shift " + (num1 << 2));
    System.out.println("The result after performing right signed shift " + (num1 >> 2));
    System.out.println("The result after performing right unsigned shift " + (num1 >>> 2));

  }

}

Output:

The result after performing left shift 40
The result after performing right signed shift 2
The result after performing right unsigned shift 2

9. instanceOf Operator in Java

This is a type-check operator. It checks whether a particular object is the instance of a certain class or not.
It returns true if the object is a member of the class and false if not.

Java program to test the instanceOf Operator:

package com.dataflair.operators;
import java.io. * ;
import java.util. * ;
public class InstanceOperator {
  public static void main(String[] args) throws IOException {
    InstanceOperator ob = new InstanceOperator();
    System.out.println("Is ob an instance of InstanceOperator class? " + (ob instanceof InstanceOperator));

  }
}

Output:

Is ob an instance of InstanceOperator class? true

Java Operators Precedence and Association

Precedence of operators defines which operator will be executed first if both operators are mentioned in a single expression.

Example in the expression – a+b*c. Here the compiler will read it as (a+(b*c)).

Association is the method by which the compiler decides which expression to evaluate first if the operators are of the same precedence.

For example, the expression a=b=c=100 will be evaluated by the compiler as (a=(b=(c=100)))

Association groups the expressions together. However some operators do not have an order of association. These are called non-associative operators. Associative is of two types namely left to right and right to left.

A table with the operator precedences in Java is mentioned below:

LevelOperatorDescriptionAssociativity
1=,+=,-=,*=,/=,%=,&=,^=,|=,<<=,>>=,>>>=Basic assignment OperatorsRight to left
2?:ternaryRight to left
3||Logical ORLeft to right
4&&Logical ANDLeft to Right
5|bitwise ORleft to right
6^bitwise XORleft to right
7&bitwise ANDleft to right
8==,!=equality operatorleft to right
9<,<=,>,>=,instanceofrelationalnot associative
10<<,>>,>>>shiftleft to right
11+,-additiveleft to right
12*,/,%multiplicativeleft to right
13(), newobject creationright to left
14++,–,+,-,!,~pre-increment,pre- decrement,plus,minus,NOT,bitwise NOTright to left
15++,–unary post increment,unary post decrementnot associative
16[ ], . , ()access array element,object member,parenthesesleft to right

Quiz on Java Operators

Summary

In conclusion, we did learn a lot about Java operators and their types. A good knowledge of operators and when to use them would allow us to develop our programming skills to a great extent.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

13 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.

  9. Hemant kumar says:

    Deep knowledge in this material.

Leave a Reply

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