16 Java - Operators

What is Operator? This indicate what actions to perform like addition, subtraction etc.

What is Operand: This indicate the items, on which action has to apply on.

What is Expression: It consist of 1 or more Operand and 0 or more Operators.

Categories Of Operator

Arithmetic Operators

  • / (Division)

  • - (Subtraction)

  • + (addition)

  • % (Modulus)

  • * (Multiplication)

public class Main {
    public static void main(String[] args) {
        int division = 5 / 2;
        System.out.println(division);

        int mod = 5 % 2;
        System.out.println(mod);

        int sum = 3 + 4;
        System.out.println(sum);

        int subtract = 4 - 3;
        System.out.println(subtract);

        int multiply = 3 * 4;
        System.out.println(multiply);

    }
}

Relational Operators

(Compares two Operand relation and return true or false)

  • == (Equal to)

  • != (Not Equal to)

  • > (Greater than)

  • < (Less than)

  • >= (Greater than or equals to)

  • <= (Less than or equals too)

public class Main {
    public static void main(String[] args) {
        int a = 4;
        int b = 7;
        System.out.println(a==b);
        System.out.println(a!=b);
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a>=b);
        System.out.println(a<=b);
    }
}

Logical Operators

(Combines two or more conditions And return true or false)

  • && (Logical AND)

  • || (Logical OR)

Note:

  • For AND if first condition is false then it won't check the second condition (Whole condition is False)

  • For OR if first condition is true then it won't check the second condition (Whole condition is True)

public class Main {
    public static void main(String[] args) {
        int a = 4;
        int b = 7;
        //AND Operator
        System.out.println(a<3 && a!=b);
        System.out.println(a>3 && a!=b);
        //OR Operator
        System.out.println(a<3 || a!=b);
        System.out.println(a>3 || a!=b);
    }
}
/*
false
true
true
true
*/

Unary Operators

Requires only single Operand

  • ++ (Increment)

  • -- (Decrement)

  • - (Unary Minus)

  • + (Unary Plus)

  • ! (Logical NOT)

public class Main {
    public static void main(String[] args) {
        int a = 5;
        boolean flag = true;

        //Increment operator
        System.out.println(a++);
        System.out.println(++a);

        //Decrement Operator
        System.out.println(a--);
        System.out.println(--a);

        //logical NOT Operator
        System.out.println(!flag);

        //Unary Minus Operator
        System.out.println(-a);

        //Unary Plus Operator
        System.out.println(+a);

    }
}

Assignment Operators

Used to assign new value to the variable

  • =

  • +=

  • -=

  • *=

  • /=

  • %=

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

        variable = a;
        System.out.println(variable);

        variable = 0;
        variable+=a;
        System.out.println(variable);

        variable-=3;
        System.out.println(variable);

        variable*=a;
        System.out.println(variable);

        variable/=a;
        System.out.println(variable);
    }
}

/*
5
5
2
10
2
*/

Bitwise Operator

It works on bit i.e. 1 and 0. It's very fast.

  • & (Bitwise AND)

  • | (Bitwise OR)

  • ^ (Bitwise XOR)

  • ~ (Bitwise NOT)

    (It can be come under Unary too)

public class Main {
    public static void main(String[] args) {
        int a=4;
        int b=6;

        //Bitwise AND
        System.out.println(a & b);

        //Bitwise OR
        System.out.println(a | b);

        //Bitwise XOR
        System.out.println(a ^ b);

        //Bitwise NOT, bitwise complement of any integer n is -(n+1)
        System.out.println(~a);
    }
}
/*
4
6
2
-5
*/

How does BITWISE NOT works?

int a = 4

0100 (remember its not equal to 100 as int is a signed as it represent both positive and negative value and in Java there is nothing like unsigned integer like in C++)

0100 --------------> ~0100 = 1011

-> Now what is 1011?

1*(-2^3) + 0*(2^2) + 1*(2^1) + 1*(2^0)

= -8+0+2+1

=-5

Which is equivalent to -(N+1) = -(4+1) = -5

-> How can we confirm if -5 is 1011?

5 = 0101

To get the -5, we know that we have to find its 2nd complement

0101 =>

1st complement = 1010

2nd Complement = 1010 + 1 = 1011

Bitwise Shift Operators

Used to shift the bits of a number left or right

  • << (Signed Left Shift)

  • >> (Signed Right Shift)

  • >>> (Unsigned Right Shift)

There is no <<< (Unsigned Left Shift) as << and <<< are equal

Working of >>:

  • Its signed right shift

  • It fills the most significant bit with the sign of the number

  • Example 1: 1100010 its >> 1110001 (Add1 in MSB, i.e MSB of the original number)

  • Example 2: 01000110 its >> 00100011 (Add1 in MSB, i.e MSB of the original number)

Working of >>>:

  • Its Unsigned right shift it fills the most significant bit with the 0

  • Example 1: 11000110 its >>> 01100011

  • Example 2: 01000110 its >>> 00100011

public class Main {
    public static void main(String[] args) {
        int a=4;

        //left shift
        System.out.println(a<<1);
        System.out.println(a<<2);

        //right shift
        System.out.println(a>>1);
        System.out.println(a>>2);
    }
}
/*
8
16
2
1
*/

Ternary Operators

It mimics the if else condition

(condition) ? (expression1) : (expression2)

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

       int maxValue = (a>b) ? a:b;
        System.out.println(maxValue);
    }
}

/* 5 */

Type comparision Operator

It used to do the type check, whether particular object is of certain class or not.

instanceOf

(Sometimes also shown under Relational Operator list)

public class ParentClass {
}
public class ChildClass1 extends ParentClass{
}
public class ChildClass2 extends ParentClass{
}
public class Main {
    public static void main(String[] args) {
        ParentClass obj = new ChildClass2();
        System.out.println(obj instanceof ChildClass1); //false
        System.out.println(obj instanceof ChildClass2); //true

        ChildClass1 childObj = new ChildClass1();
        System.out.println(childObj instanceof ParentClass); //true

        String val = "hello";
        System.out.println(val instanceof String); //true

        Object unknownObject = new RandomClass();
        System.out.println(unknownObject instanceof ChildClass2); //false
    }
}

Operator Precedence and Associativity

If 2 operators have the same precedence, then its evaluated based on its Associativity (Left to Right or Right to Left)

The table starts with the highest priority and goes in decreasing order of priority.