«Back to Home

Core Java

Topics

Operators In Java

Operators

In Java, there are many types of Operators. Operators are use to perform some operations.
 
Java has around 30 operators such as,
  • Assignment operator.
  • Arithmetic operator.
  • Relational operator.
  • Unary operator.
  • Logical operator.
  • Conditional operator.
  • Comparison operator.
  • Bitwise operator.
  1.  Assignment Operator.

    ( = ).

    Assignment Operator is the very common operator, which we use to assign the value on its right to the operand on its left.

    For example
    1. int a = 5;  
    2. String msg = “hello java”;  
  2. Arithmetic operators.

    Arithmetic operators are used to achieve the mathematical calculations like algebra. The following table lists of all the arithmetic operators, which are,

     Operators  Explanation.
     +  Addition (or concatenation) operator
     -  Subtraction operator
     *  Multiplication operator
     /  Division operator
     %  Remainder operator

    Let’s see the examples, given below.

    Code
    1. public class ArithmeticOperatorEx1 {  
    2.     public static void main(String[] args) {  
    3.         int p = 25;  
    4.         int q = 15;  
    5.         int res = p + q;  
    6.         System.out.println("p + q = " + res);  
    7.         res = p - q;  
    8.         System.out.println("p - q = " + res);  
    9.         res = p * q;  
    10.         System.out.println("p * q = " + res);  
    11.         res = p / q;  
    12.         System.out.println("p / q = " + res);  
    13.         res = p % 2;  
    14.         System.out.println("p % 3 = " + res);  
    15.     }  
    16. }  
    51

    Output

    52

  3. Relational Operators

    Relational operators are used to judge against the two operands and give result in a boolean form. The following table lists all the relational operators, which are given below.

     Operators  Explanation.
     ==  equal to
     !=  not equal to
     >  greater than
     >=  greater than or equal to
     <  less than
     <=  less than or equal to

    Let’s see the examples, given below.

    Code
    1. public class RelationalOperatorEx2 {  
    2.     public static void main(String[] args) {  
    3.         int p = 25;  
    4.         int q = 35;  
    5.         boolean res = p == q;  
    6.         System.out.println("p == q : " + res);  
    7.         res = p != q;  
    8.         System.out.println("p != q : " + res);  
    9.         res = p > q;  
    10.         System.out.println("p > q : " + res);  
    11.         res = p >= q;  
    12.         System.out.println("x >= q : " + res);  
    13.         res = p < q;  
    14.         System.out.println("p < q : " + res);  
    15.         res = p <= q;  
    16.         System.out.println("p <= q : " + res);  
    17.     }  
    18. }  
    53

    Output



  4. Unary Operators.

    Unary Operators are engaged in a single operand. The following table lists all unary operators, which are,

     Operators  Explanation.
     +  Unary plus operator; indicates positive value.
     -  Unary minus operator; negate an expression.
     ++  Increment operator; increments a value by 1.
     --  Decrement operator; decrements a value by 1;
     !  Logical complement operator; return value of a boolean.

    Let’s see the examples, given below.

    Code
    1. public class UnaryOperatorEx3 {  
    2.     public static void main(String[] args) {  
    3.         int p = 20;  
    4.         int q = 40;  
    5.         int res = +p;  
    6.         System.out.println("+p = " + res);  
    7.         res = -q;  
    8.         System.out.println("-q = " + res);  
    9.         res = ++p;  
    10.         System.out.println("++p = " + res);  
    11.         res = --q;  
    12.         System.out.println("++q = " + res);  
    13.         boolean a = false;  
    14.         System.out.println(a);  
    15.         System.out.println(!a);  
    16.     }  
    17. }  
    55

    Output

    56

  5. Logical Operators.

    Logical operator is used to do logical operations in Java. The following table lists all the logical operators, which are,

     Operators  Explanation.
     &&  Logical AND operator. (when two operands are not zero then the condition give true).
     ||  Logical OR operator. (If any two operands are not zero then the condition give true).
     !  Logical NOT operator. It uses to reverse the logic.

    Let’s see the example, given below.

    Code
    1. public class LogicalOperatorEx4 {  
    2.     public static void main(String[] args) {  
    3.         boolean p = true;  
    4.         boolean q = false;  
    5.         boolean res = (p && q);  
    6.         System.out.println("p && q = " + res);  
    7.         res = (p || q);  
    8.         System.out.println("p || q = " + res);  
    9.         res = !(p && q);  
    10.         System.out.println("p ! q = " + res);  
    11.     }  
    12. }  
    57

    Output

    58

  6. Conditional Operators.

    Ternary Operator is a conditional operator in Java. Conditional operators are? and: which forms a ternary (three operands) such as

    res = P ? Q: R

    In other words, we can say that it is similar to if-else condition like: if P then Q else R. so, this is also referred as shorthand for an if-then-else statement. The table of conditional operator is given below.

     Operators  Explanation.
     ? :  Ternary operator in form of: A ? B : C

    Let’s see the example, given below.

    Code
    1. public class TernaryOperatorEx6 {  
    2.     public static void main(String[] args) {  
    3.         int p = 10;  
    4.         int q = 20;  
    5.         int res = (p > 10) ? p : q;  
    6.         System.out.println("result 1 is: " + res);  
    7.         res = (q > 10) ? p : q;  
    8.         System.out.println("result 2 is: " + res);  
    9.     }  
    10. }  
    59

    Output

    60

  7. Comparison Operators. 

    Instance of an operator is used as a comparison operator in Java. This operator tests that object of a class is an instance of a class or not and gives the result in a Boolean value. The table of comparison operator is given below.

     Operators  Explanation.
     Instanceof  Instanceof operator checks the type of object.

    Let’s see an example, given below.

    Code
    1. public class InstanceofOperatorEx7 {  
    2.     public static void main(String[] args) {  
    3.         String name = "Hello";  
    4.         if (name instanceof String) {  
    5.             System.out.println("It is an instance of  a String class");  
    6.         }  
    7.         // test for subclass of Object  
    8.         if (name instanceof Object) {  
    9.             System.out.println("It is an instance of  an Object class");  
    10.         }  
    11.     }  
    12. }  
    61

    Output

    62

  8. Bitwise Operator.
     
    Bitwise operators are hardly ever used. These operators perform bit shift operations on only integral types and not other types. The following table lists of all bitwise operators, which are,

     Operators  Explanation.
     ~  unary bitwise complement; inverts a bit pattern
     <<  signed left shift
     >>  signed right shift
     >>>  unsigned right shift
     &  bitwise AND
     ^  bitwise exclusive OR
     |  bitwise inclusive OR

    Let’s see the example, given below.

    Code
    1. public class BitwiseOperatorEx8 {  
    2.     public static void main(String[] args) {  
    3.         int p = 10;  
    4.         int res = p >> 1;  
    5.         System.out.println("Before right shift: " + p);  
    6.         System.out.println("After right shift: " + res);  
    7.     }  
    8. }  
    63

    Output

    64
Summary

Thus, we learnt that the operators are used to perform some operations and also learnt how to use it in Java.