What is wrapper class in java explain with example




















Multi-threading in Java. Table of Contents. Save Article. Improve Article. Like Article. Previous Access Modifiers in Java. Next Need of Wrapper Classes in Java. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Java. Split String method in Java with examples Arrays.

More related articles in Java. Autoboxing : It is the automated transformation of primitive types to the object of their corresponding wrapper classes—for example, conversion of long to Long and int to Integer, double to Double. Unboxing is the reverse of Autoboxing.

It is the method of converting a Wrapper class object within its corresponding Primitive Data type. For example, we can convert an Integer object into a primitive data type, as int. We hope this article helped you with the understanding of the wrapper class in Java.

Data Science. Data Science All Courses M. Sc in Data Science — University of Arizona. Software Engineering All Courses M. Table of Contents. Leave a comment. In the above example, we have used the valueOf method to convert the primitive types into objects.

Here, we have used the instanceof operator to check whether the generated objects are of Integer or Double type or not. However, the Java compiler can directly convert the primitive types into corresponding objects. For example,. This process is known as auto-boxing. To learn more, visit Java autoboxing and unboxing. Note : We can also convert primitive types into wrapper objects using Wrapper class constructors.

But the use of constructors is discarded after Java 9. To convert objects into the primitive types, we can use the corresponding value methods intValue , doubleValue , etc present in each wrapper class. In the above example, we have used the intValue and doubleValue method to convert the Integer and Double objects into corresponding primitive types.

However, the Java compiler can automatically convert objects into corresponding primitive types. This process is known as unboxing. Note : Primitive types are more efficient than corresponding objects. Hence, when efficiency is the requirement, it is always recommended primitive types.

Course Index Explore Programiz. Java for Loop.



0コメント

  • 1000 / 1000