The IoT Academy Blog

Explain Java Wrapper Classes with Examples – Beginners Guide

  • Written By  

  • Published on August 30th, 2023

Introduction

Java has a way to convert a primitive into an object and an object into a primitive. It is possible through a wrapper class in Java. Thus, a class in Java is a wrapper if its object contains or wraps primitive data types. Additionally, a wrapper class object that you build has a field where you can store primitive data types. 

The blog is all about Java wrapper classes including examples, the need for a wrapper class, advantages, and uses.

Why is the Need for Wrapper Classes in Java?

You require the Wrapper class in Java under below conditions:
 

  • The data structures in the Collection framework like ArrayList and Vector, store objects only (reference types). However, they do not store primitive types. Thus, an object is a must to provide synchronization in multithreading.
  • Java wrapper classes change primitive data types into objects. You also need the objects to modify the arguments entered into a method.
  • You must transform the objects into streams for conducting the serialization. So, if you have a primitive value, you can use the wrapper classes to turn it into an object.
  • The classes in the java.util package deals with only objects. Therefore, you make use of wrapper class in Java here.

How many Wrapper classes are there in Java?

There are eight wrapper classes:

Primitive Data type: char

Wrapper class: Char

Primitive Data type: double

Wrapper class: Double

Conversion method: public static double parseDouble(String)

Primitive Data type: float

Wrapper class: Float

Conversion method: public static float parseFloat(String)

Primitive Data type: int

Wrapper class: Integer

Conversion method: public static int parseInt(String)

Primitive Data type: long

Wrapper class: Long

Conversion method: public static long parseLong(String)

Primitive Data type: short

Wrapper class: Short

Conversion method: public static short parseShort(String)

Primitive Data type: boolean

Wrapper class: Boolean

Conversion method: public static boolean parseBoolean(String)

Primitive Data type: byte

Wrapper class: Byte

Conversion method: public static byte parseByte(String)

Benefits of Java Wrapper Classes
 

  • Object data permitted null values.
  • Serialization may permit only object data.
  • Collections permitted object data only.
  • Multiple methods like equals(), and toString(), can be called on object data. 
  • Cloning only processes objects.

Wrapper Class in Java Example
 

  • Example 1: Converting Wrapper Objects into Primitive Types

class Main {

public static void main(String[] args) {

// creating objects of the wrapper class

Integer xOyj = Integer.valueOf(35);

Double yOyj = Double.valueOf(7.75);

// converting into primitive types

int a = xOyj.intValue();

double y = yOyj.doubleValue();

System.out.println("The value of x: " + x);

System.out.println("The value of y: " + y);

}

}

Output

The value of x: 35

The value of y: 7.75
 

  • Example 2: Converting Primitive Types to Wrapper Objects

class Main {

public static void main(String[] args) {

// creating primitive types

int x = 5;

double y = 5.65;

//converts into wrapper objects

Integer xOyj = Integer.valueOf(x);

Double yOyj = Double.valueOf(y);

if(aObj instanceof Integer) {

System.out.println("Created an object of Integer.");

}

if(bObj instanceof Double) {

System.out.println(" Created an object of Double.");

}

}

}

Output

Created an object of Integer.

Created an object of Double.

What are Autoboxing and Unboxing in Java?
 

  • Unboxing

Unboxing is a way of transforming a wrapper type into an equivalent primitive type. Thus, it is autoboxing done the other way around. Since Java 5, there is no need to transform wrapper types into primitives using the intValue() method of wrapper classes.
 

  • Autoboxing

Autoboxing is a way of automatically transforming primitive data types into their respective wrapper classes. Thus, it can be from:
 

  • bytes into bytes
  • characters into characters
  • float into float
  • booleans into booleans
  • double into double
  • integers into integers
  • long into long
  • short into short.

Since Java 5, there is no longer a need to transform primitives into objects using the wrapper classes' valueOf() method.

Immutable In Java

An object is immutable when its state doesn't change after initialization.

Hence, programs must create a new object for each state change as you cannot change immutable objects. However, Immutable objects also have the following advantages:
 

  • An immutable class is ideal for caching since it eliminates any concern over value changes.
  • You don't need to be concerned about thread safety in multi-threaded contexts. This is because immutable classes are thread-safe by nature.

How To Create an Immutable Class in Java?

Follow the below guidelines for creating an immutable class in Java:
 

  • Start with declaring the class to be final to prevent extensions.
  • Further, make every field private to prevent direct access.
  • Avoid giving variables setter methods.
  • Make all mutable fields final. This will ensure that a field's value can only be assigned once. 
  • Use a constructor method that performs deep copy to initialize all fields.
  • Return a copy after cloning the objects in the getter methods. But, avoid returning the real object reference.

Key Takeaways for Java Wrapper Classes
 

  • You can use Wrapper class objects to pass arguments to methods that need to have their input parameters changed.
  • A Wrapper class is essential because the classes in the Java.util package only works with objects.
  • You must also turn primitive values into objects because synchronization in multithreading requires the support of an object.
  • You cannot store Null values in primitive data structures. But, you can store them in objects of the Wrapper class.
  • Using Java wrapper classes, you can also fulfill requirements. For instance, the desire to transform one primitive data type to another primitive data type. 
  • An object is said to be immutable if its state doesn't change after initialization.
  • A wrapper class helps in collection classes like LinkedList, ArrayList, etc. However, these classes only store objects and not primitive values.

Conclusion

We uncovered Java wrapper classes in this guide. Now, you know how to use a wrapper class to convert primitive types to objects. You are also aware of when to use objects over primitive types. In a nutshell, Java uses wrapper classes to turn primitive types (such as int, char, and float) into corresponding objects. For more technical updates, stay in touch with The IoT Academy!

Frequently Asked Questions

Q. Is void a wrapper class?

Ans.The class Void is regarded as a wrapper. However, it does not wrap any values.

Q.What is Java immutable?

Ans.In Java, an immutable class prevents content changes once an object is created. Additionally, in Java, all the wrapper classes and String classes are immutable. You can also create your immutable class.

 

About The Author:

logo

Digital Marketing Course

₹ 9,999/-Included 18% GST

Buy Course
  • Overview of Digital Marketing
  • SEO Basic Concepts
  • SMM and PPC Basics
  • Content and Email Marketing
  • Website Design
  • Free Certification

₹ 29,999/-Included 18% GST

Buy Course
  • Fundamentals of Digital Marketing
  • Core SEO, SMM, and SMO
  • Google Ads and Meta Ads
  • ORM & Content Marketing
  • 3 Month Internship
  • Free Certification
Trusted By
client icon trust pilot
1whatsapp