Javatpoint Logo
Javatpoint Logo

Java instanceOf() Generics

In Java, generics are majorly used for providing a method for the creation of classes and methods which are capable of working with any type of data including the type safety.

When generics are utilized in Java, the type of an object is often checked during the runtime execution. This is the main purpose of the instanceOf() operator in Java. Let's understand about this in detail with the help of few example programs.

Java instanceOf()

The main purpose of the instanceOf() operator in Java is to check whether an object is the instance of a specific type or not. In Java, type erasure can be defined as a process which is used for removing the information during the compilation and making it unavailable during the runtime execution.

The usage of instanceOf() method in Java must be followed by caution as it might indicate that the code has not been fully taken advantage of the type information that is provided by generics. In various cases, the preferable designed classes and methods are made in such way that they allow us to directly work with the types of generics.

Let us understand about instanceOf() operation in Java in detail with the help of Java example programs.

Example 1:



It's a Dog!
Fetching the ball
Woof! Woof!
It's a Cat!
Purring contentedly
It's a Parrot!
Mimicking human speech
Squawk! Squawk!
It's a Snake!
Slithering on the ground

Example 2:



It's a Circle!
Circle with radius: 5.0
Area: 78.53981633974483
It's a Rectangle!
Rectangle with length: 4.0 and width: 6.0
Area: 24.0
It's a Triangle!
Triangle with base: 3.0 and height: 8.0
Area: 12.0
It's a Three-Dimensional Shape!
Area: 113.09733552923255
It's a Three-Dimensional Shape!
Area: 96.0

This is the required explanation about the instanceOf() generics operator in Java.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA