Compare the Difference Between Similar Terms

Difference Between Wrapper Class and Primitive Type in Java

Key Difference – Wrapper Class vs Primitive Type in Java
 

Java is a popular programming language that is used to develop various applications. One advantage of Java is that it supports Object Oriented Programming (OOP). Using OOP, the program or the software can be modeled using objects. A class is used as a blueprint to create an object. In programming, it is necessary to store data. Reserved memory locations to store data are known as variables. Each variable has a specific data type. There are eight primitive types provided by the Java language. They are short, byte, int, float, double, char, boolean. Sometimes, it is required to convert the primitive type to an object and the object back to the primitive type. The wrapper classes are used for this conversion. This article discusses the difference between wrapper class and primitive type in Java. The key difference between wrapper class and primitive type in Java is that wrapper class is used to convert a primitive type to an object and object back to primitive type while a primitive type is a predefined data type provided by the Java programming language.

CONTENTS

1. Overview and Key Difference
2. What is Wrapper Class in Java
3. What is Primitive Type in Java
4. Similarities Between Wrapper Class and Primitive Type in Java
5. Side by Side Comparison – Wrapper Class vs Primitive Type in Java in Tabular Form
6. Summary

What is Wrapper Class in Java?

A Wrapper class in Java is used to convert a primitive data type to an object and object to a primitive type. Even the primitive data types are used for storing primary data types, data structures such as Array Lists and Vectors store objects. Therefore, it is required to use wrapper classes for the conversion. The corresponding wrapper classes for primitive types char, byte, short and int are Character, Byte, Short, and Integer. The corresponding wrapper classes for long, float, double and boolean are Long, Float, Double and Boolean.

Figure 01: Java Program that converts the Wrapper Classes to Primitive Types

According to the above program, intobj is an Integer wrapper class object. The floatobj is a Float wrapper class object. The doubleobj is a Double wrapper class object. The Integer object is converted into a primitive int using intValue (). Similarly, the Float object is converted into a primitive float using floatValue(). The Double object is converted into primitive double using doubleValue (). If the programmer writes the statement as int i = intobj; the compiler internally writes intobj.Value(). The process of automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. Collections such as ArrayLists use Wrapper class because they store objects.

What is Primitive Type in Java?

The primitive data types are the predefined data types provided by the Java programming language. There are eight primitive types. They are byte, short, int, long, float, double, boolean and char. The byte data type is used to store an 8-bit signed two’s complement integer. The short data type is used to store 16-bit signed two’s complement integer. An int data type is used to store 32-bit signed two’s complement integer while long data type is used to store 64-bit singed two’s complement integer. The float is used to store single precision 32-bit floating point value and the double is used to store double precision 64-bit floating point value. The boolean is used to represent true or false. The char is used to store a single character. Those are the eight primitive types in Java.

Figure 02: Java Program that converts the Primitive Types to Wrapper Classes

According to the above program, num1 is an int type. It is converted into an Integer by passing num1 to Integer.valueOf(). The float1 can store float values. It is converted into Float type by passing float1 into Float.valueOf(). Similarly, double1 can store double values. It is converted into Double type by passing double1 into Double.valueOf(). If the programmer writes the statement as Interger intobj = num1; the compiler internally writes Integer.valueOf(num1); The process of converting the primitive type to the corresponding wrapper class object automatically is known as autoboxing.

What is the Similarity Between Wrapper Class and Primitive Type in Java?

What is the Difference Between Wrapper Class and Primitive Type in Java?

Wrapper Class vs Primitive Type in Java

Wrapper class provides a mechanism to convert primitive type into object and object into primitive type. A primitive type is a predefined data type provided by Java.
 Associated Class
A Wrapper class is used to create an object; therefore, it has a corresponding class. A Primitive type is not an object so it does not belong to a class.
Null Values
The wrapper class objects allow null values. A primitive data type does not allow null values.
 Memory Required 
Required memory is higher than the primitive types.The Clustered Index does not require an additional space. Required memory is lower comparing to wrapper classes.
Collections
A Wrapper class can be used with a collection such as ArrayList, etc. A primitive type is not used with collections.

Summary – Wrapper Class vs Primitive Type in Java

Java language provides eight primitive data types. Sometimes it is required to convert the primitive types to object and also to convert the objects back to primitives. Wrapper classes can be used to achieve that task. The difference between wrapper class and primitive type in Java is that wrapper class is used to convert a primitive type to an object and object back to a primitive type while a primitive type is a predefined data type provided by the Java programming language.

Reference:

1.“Wrapper class in java – Javatpoint.” , Java Point. Available here
2.Singh, Chaitanya. “Wrapper class in Java.” , 10 Sept. 2017. Available here
3.Point, Tutorials. “Java Basic Datatypes.” , Tutorials Point, 27 Oct. 2017.Available here