Wrapper classes Vs Primitive Data Types
image credits: TechStack9

Wrapper classes Vs Primitive Data Types

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.

Wrapper Classes:

  • For each data type, Java provides a predefined class called Wrapper Class.
  • Wrapper classes wrap primitive data type value into a class object. It is this wrapper class that helps to make Java object-oriented.
  • All the Wrapper classes present in Java are present inside java.lang package. And java.lang package is the default package in Java.

image credits : Medium

Java Autoboxing - Primitive Type to Wrapper Object

In autoboxing, the Java compiler automatically converts primitive types into their corresponding wrapper class objects. For example,

int a = 56;

// autoboxing
Integer aObj = a;        

Autoboxing has a great advantage while working with Java collections.

Example: Java Autoboxing

import java.util.ArrayList;

class Main {
   public static void main(String[] args) {

      ArrayList<Integer> list = new ArrayList<>();

      //autoboxing
      list.add(5);
      list.add(6);

      System.out.println("ArrayList: " + list);
   }
}        

Output

ArrayList: [5, 6]        

Java Unboxing - Wrapper Objects to Primitive Types

In unboxing, the Java compiler automatically converts wrapper class objects into their corresponding primitive types. For example,

// autoboxing
Integer aObj = 56;

// unboxing
int a = aObj;        

Like autoboxing, unboxing can also be used with Java collections.

Example: Java Unboxing

import java.util.ArrayList;

class Main {
   public static void main(String[] args) {

      ArrayList<Integer> list = new ArrayList<>();

      //autoboxing
      list.add(5);
      list.add(6);

      System.out.println("ArrayList: " + list);

      // unboxing
      int a = list.get(0);
      System.out.println("Value at index 0: " + a);
   }
}        

Output

ArrayList: [5, 6]
Value at index 0: 5        

?A wrapper class wraps (encloses) around a data type and gives it an object appearance, this automatic conversion of primitive data types into equivalent wrapper type is known as Boxing/Autoboxing. Wherever, the data type is required as an object, this object can be used.

Wrapper classes include methods to unwrap the object and give back the primitive data type. This operation of converting objects to primitive data types is known as ?Unboxing.

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.

image credits : Ansari

  • byte : It is 1 byte(8-bits) integer data type. Value range from -128 to 127. Default value zero. example: byte b=10;
  • short : It is 2 bytes(16-bits) integer data type. Value range from -32768 to 32767. Default value zero. example: short s=11;
  • int : It is 4 bytes(32-bits) integer data type. Value range from -2147483648 to 2147483647. Default value zero. example: int i=10;
  • long : It is 8 bytes(64-bits) integer data type. Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Default value zero. example: long l=100012;

  • double: It is 8 bytes(64-bits) float data type. Default value 0.0d. example: double db=11.123;
  • float : It is 4 bytes(32-bits) float data type. Default value 0.0f. example: float ff=10.3f;
  • Char: This group represent char, which represent symbols in a character set, like letters and numbers.It is 2 bytes(16-bits) unsigned unicode character. Range 0 to 65,535.?example: char c=’a’;
  • Boolean:?Boolean type is used when we want to test a particular condition during the execution of the program. There are only two values that a Boolean type can take: true or false.?

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.


image credit: Logicmojo

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

  • Both Wrapper class and Primitive Type in Java can be used to store data in programming.

Wrapper Class to Primitive Data Type

Wrapper classes in Java provide a way to convert between primitive data types and their corresponding objects. This conversion, known as unboxing, allows for seamless interchangeability and enables access to the primitive value within the wrapper class object.

Primitive Data Type to Wrapper Class

Java allows the conversion from primitive data types to their corresponding wrapper classes, known as autoboxing. This automatic conversion simplifies code by enabling direct assignment of primitive values to wrapper class objects, facilitating operations that require objects instead of primitives.

Boxing and Unboxing

Wrapper classes facilitate the process of converting between primitive types and objects through boxing and unboxing. Boxing involves wrapping a primitive value within its corresponding wrapper class object, while unboxing extracts the primitive value from the wrapper object. This allows you to work with primitive types in I/O operations that require objects.

image credits : Yourbutlerspantry

Conclusion:

Java Wrapper classes offer several advantages over primitive data types, including encapsulation, type conversion, autoboxing and unboxing, and utility methods. They are also thread-safe and immutable, making them ideal for multi-threaded applications.


Devisrinivas Kandikattu

?? Java Enthusiast |Tech Talk Curator??|Join me for cutting-edge tech updates ??|Chairperson of IEEE SB NBKRIST| ISC2 Candidate| Undergraduate CSE student at N.B.K.R Institute of Science & Technology| Volunteer

7 个月

Informative ?? autoboxing and unboxing is one of the important concepts in Java, Thanks for sharing ?

要查看或添加评论,请登录

社区洞察

其他会员也浏览了