Wrapper classes in java with example programs


















Wrapper Class is an important class of java. Wrapper class objects create a wrapper for the primitive data types. While creating an object of the wrapper class, space is created in the memory where primitive data type is stored.

Primitive data type refers to the int, float, char, double, byte, etc. In the below-given example, we can see i is an integer data type. Sometimes in the java integer needs to be passed as a type of object. In this case, we can use the wrapper class to convert an integer into an object.

In the above-given syntax, we can see how a primitive data type is being converted to an object using an Integer class object. Also, we can say primitive data type is wrapped as an object. Autoboxing: Autoboxing refers to the automatic conversion of the primitive data type to object using Wrapper classes. It is known as Autoboxing. Unboxing: Unboxing is the reverse process of Autoboxing.

Automatically converting wrapper class objects to the corresponding primitive data type is known as Unboxing. 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. Arrays in Java. Interfaces in Java. Java ArrayList. Popular Examples Check prime number. Print the Fibonacci series. Next Autoboxing in Java. Pankaj I love Open Source technologies and writing about my experience about them is my passion. Follow Author. Comments rahul says:. May 31, at am. Manvendra says:. May 23, at am. Neeraj says:. May 12, at am.



0コメント

  • 1000 / 1000