Javatpoint Logo
Javatpoint Logo

Cast Generic Type to Specific type Java

Java is a programming language that supports the development of generic classes and methods. Java's generics feature enables programmers to design code that can operate on several object types without the use of typecasting. A generic type must occasionally be changed into a specific type, though. In this section, we'll look into casting a generic type in Java to a particular type.

Let's first define generics in Java before we proceed to casting a generic type to a particular type.

Java's generics enable the parameterization of classes, interfaces, and methods by one or more types. It implies that several object types can be handled by a single piece of code without requiring additional rewriting for each kind. Moreover, generics offer compile-time type safety, which aids in the early detection of problems during the development process.

Now, let's look at how to cast a generic type to a specific type in Java. The process involves specifying the target type in angle brackets (<>) after the generic type. For example, consider the following snippet:

In this illustration, a generic List object that can accommodate any kind of object is created. Then, we add a String object to the list and use the get() method to obtain it. As the return value from the get() method is an Object, we must cast it using the (String) syntax.

Another way to cast a generic type to a specific type is to use the instanceof operator. The instanceof operator tests whether an object is an instance of a particular class or interface. For example:

In this example, we first check whether the object returned by the get() method is an instance of the String class using the instanceof operator. If it is, we then cast it to a String using the (String) syntax.

It is significant to remember that when casting an object from a generic type to a specific type, a ClassCastException may occur if the object is not of the desired type. Consequently, it's crucial to confirm the object's type before casting it to a certain type. You must exercise caution when casting, though, as not all types can be cast to one another. Making ensuring that the object being cast is the right type is necessary to avoid receiving a runtime exception. When using the instanceof operator, you can determine an object's type.

Here's an example program in Java that demonstrates casting a generic type to a specific type, taking a generic List of Objects, and casting its elements to Strings before printing them out:

GenericTypeCasting.java

Output:

String value: hello
Not a string
Not a string

In this instance, a text, an integer, and a double are created as a List of Objects. Following that, we iterate through every element of the list, use the instanceof operator to determine if it's a String, and then cast it to a String before publishing it. We print a message stating that an element is not a String if it is found. The software converts the list's first entry, a String, to a String before printing the string value and adding the comment "String value:". The software writes out "Not a string" for each of the second and third components because they are not Strings.

Conclusion

In conclusion, Java's generics feature makes it possible to write code that can be used with several object types without having to rewrite it for each type. But occasionally it could be essential to cast a generic type into a particular type. The (specificType) syntax or the instanceof operator can be used to accomplish this. To prevent a ClassCastException, it is crucial to verify the object's type before casting it to a certain type.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA