Operators and Assignments Part-4

Operators in Java

Categorization of operators can be in the  following types:

  • instanceof

    instanceof operator is used to check whether the given object is of particular type or not. It can be provided with values of any type but there is a restriction in it, given values should have some relationship between them otherwise compiler will give compile time error. It always returns the boolean values. Syntax for using instanceof operator is:

    a instanceof b
    
    where,
           a is any reference
           b is class or interface

    For example,

    Thread t1 = new Thread();
    System.out.println(t1 instanceof Thread);
    System.out.println(t1 instanceof Runnable);
    System.out.println(t1 instanceof Object);

    Output:

    true
    true
    true

    Note- If we give the values to the operator as the reference of parent class and checking it with the child class then in such case we will get the output as false. For example,

    Object o = new Object();
    System.out.println(t1 instanceof Object);        //output will be true
    System.out.println(t1 instanceof String);        //output will be false

    Note- If we use null in the place of giving refernce or object, then the return value will always be false. For example,

    System.out.println(null instanceof String);
    //output will be false

  • Typecast Operator

    Type casting can be of 2 types:

    • Implicit Type Casting: Typecasting which the compiler performs internally is referred to as implicit type casting. We perform it when a we assign bigger data type to the value of lower data type. In this typecasting there is no loss of precision or information. When we perform such operation, it is known as upcasting of data type. For example,
      double a = 5;
      System.out.println(a);      //a is type casted from int to double data type
      //output will be 5.0
      
      int m = 'A';
      System.out.println(m);      //m is type casted from char to int data type
      //output will be 65
      
    • Explicit Type Casting: The programmer performs this kind of typecasting. It is generally required to store the value of larger data type to the smaller data type. Compiler cannot perform this type of type casting. When we perform such operation, it is known as downcasting of data type. For example,
      byte a = 130;
      //it will give compile time error
      
      byte a = (byte)130;      //successfully downcasted the value
      System.out.println(a);
      //output will be -126

      Note- In the downcasting, we loose the most significant bit of the value.


  • Assignment Operator

    Just like as the name indicates, assignment operator assigns the value to a variable. Assignment operation can be in 3 ways:

    • Simple Assignment Operator: In this operation, a single variable is assign with a single constant or variable. For example,
      int a = 10;    
      int b = a;
    • Chained-Assignment Operator: In this operation, multiple variables can be assign with a single value. For example,
      int a, b, c, d;
      a = b = c = d = 5;
      //Now all variable are assigned the value 5.

      Note-  We cannot perform this operation at the time of initialization.

    • Compound Assignment Operator: In this type, assignment operator are mixed up with arithmetic operators. For example,
      int a=5;
      a += 6;                         //it is equivalent a=a+6
      System.out.println(a);          //output will be 1
      

      Note- Possible compound operators are +=, -=, *=, /=, %=, &=, |=, ^=, >>=, >>>= and <<=. 


  • Conditional Operator

    It is the only turnary operator in Java. As an argument this operator takes 1 condition and 2 statements. Nesting of this operator is possible. Syntax:

    variable = (condition)?result1:result2;

    The operator works as, if the given condition is true then assign result 1 to the variable, but if the condition is false then assign result 2 to the variable. For example,

    int a=1, b=5;
    int c=(a<b)?25:52;
    System.out.println(c);

    Its output will be

    25

    Example of nesting of conditional operators:

    int x = ((10<20)?true:false)?25:100;
    System.out.println(x);
    //output will be 25

Leave a Comment

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