Runtime Type identification in Java
Determining Type of object at runtime in Java means finding what kind of object it is. For those who are not familiar with What is a Type in Java, Type is the name of class e..g for �abc� which is a String object, Type is String. Finding Type of any object at runtime is also known as Runtime Type Identification in Java. Determining Type becomes increasingly important for a method which accepts parameter of type java.lang.An object like compareTo method of the Comparable class. Since two objects of different type can not be equal to each other if we know how to determine the type of Object from the object itself then we can, not only avoid ClassCastExcpetion but also optimized the equals method. Java provides three different ways to find the type of object at runtime e.g. instanceof keyword, getClass() and isInstance() method of java.lang.Class. Out of all three only getClass() is the one which exactly find Type of object while others also return true if Type of object is the super type.
Determining Type of object at runtime in Java means finding what kind of object it is. For those who are not familiar with What is a Type in Java, Type is the name of class e..g for �abc� which is a String object, Type is String. Finding Type of any object at runtime is also known as Runtime Type Identification in Java. Determining Type becomes increasingly important for a method which accepts parameter of type java.lang.An object like compareTo method of the Comparable class. Since two objects of different type can not be equal to each other if we know how to determine the type of Object from the object itself then we can, not only avoid ClassCastExcpetion but also optimized the equals method. Java provides three different ways to find the type of object at runtime e.g. instanceof keyword, getClass() and isInstance() method of java.lang.Class. Out of all three only getClass() is the one which exactly find Type of object while others also return true if Type of object is the super type.
0 Comment: