Skip to content

Module 2: Operators

Rishabh Malviya edited this page Feb 7, 2023 · 1 revision

Operators

InstanceOf

  • In Java, InstanceOf is operator that checks if the object is an instance of specified class.
  • It returns either true or false.
  • InstanceOf operator also works for ancestors of the class. syntax:
// class instanceof class
class Student {
    public static void main(String[] args) {
        Student s = new Student();
        String a = "Hello";
        System.out.println(s instanceof Student); //true
        System.out.println(a instanceof String); // true
    }
}

Conditional Operator

  • Conditional Operator checks the condition and decides the result on the basis of both conditions.
  • It returns either true or false.
  • There are three types of Conditional Operator.
    • Conditional AND :
      • AND is applied between two boolean expression and returns true if both the condition are true.
      • if(x<y && y<z) //consider x,y, and z are imaginary integer variable
    • Conditional OR :
      • OR also applied between two boolean expression and returns true if any of both the condition is true.
      • if(x<y || y<z) //consider x,y, and z are imaginary integer variable
    • Ternary Operator :
      • Ternary Operator is works like if-else statement It takes three operands. and evaluate on the basis of the condition provided.
      • boolean isTrue = (x<y) ? 10 : 20 // x and y are imaginary integer variable

Logical Operator

  • Logical Operator is used to perform logical operations on boolean expression and let us combine multiple conditional statements.
  • There are three types of Logical Operator.
    • Logical AND :
      • AND is applied between two boolean expression and returns true if both the condition are true.
      • if(x<y && y<z) //consider x,y, and z are imaginary integer variable
    • Logical OR :
      • OR also applied between two boolean expression and returns true if any of both the condition is true.
      • if(x<y || y<z) //consider x,y, and z are imaginary integer variable
    • Logical NOT :
      • NOT is applied on a single boolean expression and returns true if the condition is false or vice-versa.
      • if(!(x<y)) //consider x and y are imaginary integer variable

Arithmetic Operator

  • Arithmetic Operator is used to perform simple and complex mathematical operations on all primitive data types including integer, float, and characters.

Note : Arithmetic Operator is not applicable on boolean data type.

Note : Arithmetic (+) Operator also works on Strings for concatenation.

  • There are two types of Arithmetic Operators.
    • Unary Operator : Unary Operator is used to perform operations on a single operand.

    • int a = 10; a++; // a = 11
      Unary Operator is divided into two types :

      • Increment :
        • Increment is used to increase the value of operand by 1.
        • int a = 10; a++; // a = 11
      • Decrement :
        • Decrement is used to decrease the value of operand by 1.
        • int a = 10; a--; // a = 9
    • Binary Operator :

      • Binary Operator is used to perform operations on two operands.
      • int a = 10 + 20; // a = 30

      Binary Operator is further divided four types :

      • Addition :
        • Addition is used to add two operands.
        • int a = 10 + 20; // a = 30
      • Subtraction :
        • Subtraction is used to subtract two operands.
        • int a = 20 - 10; // a = 10
      • Multiplication :
        • Multiplication is used to multiply two operands.
        • int a = 10 * 20; // a = 200
      • Division :
        • Division is used to divide two operands.
        • int a = 20 / 10; // a = 2
      • Modulus :
        • Modulus is used to find the remainder of two operands.
        • int a = 20 % 10; // a = 0

Clone this wiki locally