1
0

Special Operators in Java

Java Provides some special operators as given below.

  1. instanceof Operator

    This operator is used to know if an object is an instance of a particular class or not. This operator returns a “true” value if an object on the left-hand side is an instance of the class given on the right-hand side. Otherwise, it returns “false”. For example:

    circle instanceof Shape

    The above statement checks if the object “circle” is an instance of the class “Shape”. If yes, it returns “true”, else returns “false”.

    class Shape1{  
        public static void main(String args[]){  
            Shape1 s = new Shape1();  
            System.out.println("s is instanceof Shape: " + (s instanceof Shape1));
        }  
    }  

    The output of the above program is:

    s is instanceof Shape: true
  2. Dot Operator

    This operator is used to access the variables and methods of a class. For example:

    student.mark

    Here we are accessing the variable “mark” of the “student” object

    student.getMarks()

    Here we are accessing the method “getMethod()” of the “student” object.

    This operator also can be used to access classes, packages, etc.

    If these concepts are not clear at this moment don’t worry. Soon we’ll learn how to create objects in Java and then this will be more clear to you.

Leave a Reply