BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Wrapper class in Java

Last Updated: November 8, 2022 by Chaitanya Singh | Filed Under: java

In the OOPs concepts guide, we learned that object oriented programming is all about objects. The eight primitive data types byte, short, int, long, float, double, char and boolean are not objects, Wrapper classes are used for converting primitive data types into objects, like int to Integer, double to Double, float to Float and so on. Let’s take a simple example to understand why we need wrapper class in java.

For example: While working with collections in Java, we use generics for type safety like this: ArrayList<Integer> instead of this ArrayList<int>. The Integer is a wrapper class of int primitive type. We use wrapper class in this case because generics needs objects not primitives. There are several other reasons you would prefer a wrapper class instead of primitive type, we will discuss them as well in this article.

Primitive Data TypeCorresponding Wrapper class
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

Why we need wrapper class in Java

1. As I mentioned above, one of the reason why we need wrapper is to use them in collections API. On the other hand, the wrapper objects hold much more memory compared to primitive types. So use primitive types when you need efficiency and use wrapper class when you need objects instead of primitive types.

The primitive data types are not objects so they do not belong to any class. While storing in data structures which support only objects, it is required to convert the primitive type to object first which we can do by using wrapper classes.

Example:

HashMap<Integer, String> hm = new HashMap<Integer, String>();

So for type safety we use wrapper classes. This way we are ensuring that this HashMap keys would be of integer type and values would be of string type.
2. Wrapper class objects allow null values while primitive data type doesn’t allow it.

Lets take few examples to understand how the conversion works:

Wrapper Class Example 1: Converting a primitive type to Wrapper object

public class JavaExample{  
   public static void main(String args[]){  
	//Converting int primitive into Integer object  
	int num=100;  
	Integer obj=Integer.valueOf(num);  

	System.out.println(num+ " "+ obj);  
   }
}

Output:

100 100

As you can see both primitive data type and object have same values. You can use obj in place of num wherever you need to pass the value of num as an object. The conversion of primitive data type to object is known as autoboxing and the conversion from object to primitive type is known as unboxing, this concept is covered in detail at: Autoboxing and Unboxing in Java.

Wrapper Class Example 2: Converting Wrapper class object to Primitive

public class JavaExample{  
   public static void main(String args[]){  
	//Creating Wrapper class object 
	Integer obj = new Integer(100);  
		
	//Converting the wrapper object to primitive
	int num = obj.intValue();

	System.out.println(num+ " "+ obj);  
   }
}

Output:

100 100

Custom Wrapper Class

We can also create a custom wrapper class to wrap a primitive type to an object. Here, we have a int data type that belongs to class XYZ. We can use this primitive data type as object using the constructor and getter setter methods of XYZ class as shown below:

class XYZ{
  private int num;
  //default constructor
  XYZ(){}
  //parameterized constructor
  XYZ(int num){
    this.num=num;
  }
  //getter and setter methods
  public int getIntValue(){
    return num;
  }
  public void setIntValue(int i){
    this.num=i;
  }
  @Override
  public String toString() {
    return Integer.toString(num);
  }
}
public class JavaExample{
  public static void main(String[] args){
    XYZ obj = new XYZ(10);
    System.out.println(obj);
    obj.setIntValue(100);
    System.out.println(obj.getIntValue());
  }
}

Output:

10
100

Conclusion

In this guide, we learned what are the advantages of objects over primitive data types. How to convert primitive types to objects using wrapper class. We also learned when to use primitive types and when to use objects. If you want to learn more such topics related to Java then head over to the Java Tutorial section.

Top Related Articles:

  1. Passing a List to a Varargs method
  2. Java Integer byteValue() Method
  3. Java Annotations tutorial with examples
  4. Java – Static Class, Block, Methods and Variables
  5. Constructor Overloading in Java with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap