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 method which accept parameter of type java.lang.Object like compareTo method of Comparable class. Since two objects of different type can not be equal to each other if we know how to determine type of Object from object itself than we can, not only avoid ClassCastExcpetion but also optimized the equals method. Java provides three different ways to find 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 super type. As we all know that Java throws ClassCastException if you try to cast object into wrong type, which makes finding type of Object at runtime even more important for robust Java programs, Though type casting can be minimized by using Generics in Java but you still have some places where you need to cast object. In this Java tutorial we will see examples three ways of determining type of Object from Java program itself. On same note Java programming language does not support RTTI(Runtime type Identification) which was C++ capability to find Object type at runtime but as I said Java has its own way of facilitating this.
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 method which accept parameter of type java.lang.Object like compareTo method of Comparable class. Since two objects of different type can not be equal to each other if we know how to determine type of Object from object itself than we can, not only avoid ClassCastExcpetion but also optimized the equals method. Java provides three different ways to find 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 super type. As we all know that Java throws ClassCastException if you try to cast object into wrong type, which makes finding type of Object at runtime even more important for robust Java programs, Though type casting can be minimized by using Generics in Java but you still have some places where you need to cast object. In this Java tutorial we will see examples three ways of determining type of Object from Java program itself. On same note Java programming language does not support RTTI(Runtime type Identification) which was C++ capability to find Object type at runtime but as I said Java has its own way of facilitating this.