×

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type.

The type wrapper classes are Double, Float, Long, Integer, Short, Byte, Character, and Boolean. They offer a wide range of methods that allows us to integrate primitive data type into Java’s object hierarchy completely.

Uses of Wrapper class

  • It is used to convert the primitive data type into objects.
  • Classes in “Java.util” package deals only with objects, so in this the wrapper classes plays its role.
  • We convert the primitive value to object using wrapper class because we need to convert the objects into streams to perform the serialization.
  • Synchronization in Java works with objects in multithreading. Therefore it is required to convert the value into objects.
  • The Collection framework in Java works with objects only.

The corresponding type wrapper class for the primitive type is in below table:

Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

As you can see in the above table, the primitive types are written in lower case. But the wrapper class’s first letter is capital. So always use them as given above. Each Wrapper classes are briefly explained below:

The Numeric Type Wrappers

The numeric type wrappers are mostly used. These are Byte, Short, Integer, Long, Float, and Double. All the type wrappers inherit the abstract class Number. Below are the methods that are declared by Number class which returns the value of an object in different number formats.

bytebyteValue()

doubledoubleValue()

floatfloatValue()

intintValue()

longlongValue()

shortshortValue()

Character Type Wrapper

It is a wrapper class for char. The constructor for Character is Character(char ch).

Here, ch denotes the character that will be wrapped by the Character object being created. To get the char value contained in a Character object, we need to call charValue() as show here:

charcharValue().

Boolean Type Wrapper

It is a wrapper class for boolean values. The constructor for boolean are:

Boolean(booleanboolValue)-boolValue must be either true or false.

Boolean(String boolString)-if boolString contains the string “true”, then the new Boolean object will be true else it will be false.

Autoboxing and Auto-unboxing

From JDK 5, Java added two other features: autoboxing and auto-unboxing. The process of automatically encapsulating primitive type into its equivalent type wrapper whenever an object type is needed is called Autoboxing. Whereas, the process by which the value of a boxed object is automatically unboxed from a type wrapper, when its value is needed is called as Auto-unboxing.

It also helps prevent errors by making ease of coding for several algorithms and removes the vulnerabilities of manually boxing and unboxing.

Example to demonstrate autoboxing/unboxing :

Class AutoboxDemo
 {
             public static void main (String args[])
             {
                         Integer first = 10; //autoboxing an int
                         inti = first; //auto-unboxing
                         System.out.println(i + " " + first); 
             }
 } 
 classWrapperDemo
 { 
 publicstaticvoid main(String args[]) 
     { 
 byte a = 10; 
 int b = 20; 
 float c = 20.5f; 
 char e='a'; 
 double d = 25.5;
         //autoboxing
         Byte byteobj = newByte(a);
         Integer intobj = newInteger(b); 
         Double doubleobj = newDouble(d); 
         Character charobj=e; 
         Float floatobj = new Float(c); 
         //printing the values from objects 
 System.out.println("---Values of objects (printing as objects)---"); 
 System.out.println("Byte object-byteobj:  " + byteobj); 
 System.out.println("Integer object-intobj:  " + intobj); 
 System.out.println("Float object-floatobj:  " + floatobj); 
 System.out.println("Double object-doubleobj:  " + doubleobj); 
 System.out.println("Character object-charobj:  " + charobj); 
         //un-boxing 
 bytebv = byteobj; 
 int iv = intobj; 
 floatfv = floatobj; 
 double dv = doubleobj; 
 char cv = charobj; 
         // printing the values from data types 
 System.out.println("---Printing primitive values---"); 
 System.out.println("byte value, bv: " + bv); 
 System.out.println("int value, iv: " + iv); 
 System.out.println("float value, fv: " + fv); 
 System.out.println("double value, dv: " + dv); 
 System.out.println("char value, cv: " + cv); 
     } 
 } 

Output:

---Values of objects (printing as objects)---
Byte object-byteobj:  10
Integer object-intobj:  20
Float object-floatobj:  20.5
Double object-doubleobj:  25.5
Character object-charobj:  a
---Printing primitive values---
byte value, bv: 10
int value, iv: 20
float value, fv: 20.5
double value, dv: 25.5
char value, cv: a 


Related Topics

How to Iterate a HashMap in Java?

Hash-map is a class of Java collections framework which has the functionality to implement the Map interface of Java. It stores the data in key-value pairs and can be accessed...

5 minutes read.

Java Class Name

How to write a class name? The following considerations should be made while writing class names. The name of the current class shouldn't be based on a preset or existing class. Java keywords...

3 minutes read.

String Array in Java

String Array in Java An array is alinear data structure that stores similar type of data. It allows us to store fixed number of elements.It can be of different data types...

6 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers. We separate...

2 minutes read.

Java Iterator

When iterating through, traversing, or retrieving the individual elements of a Collection or Flow object, a Java Cursor is leveraged as an iterator. In Java, there exist three types of...

4 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.

How Many Ways to Create an Object in Java?

As you know, a class is a blueprint for an object, and you can create objects from it. There are several ways to create objects of classes in Java. This...

6 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.

Time Class Operation in Java

The Java SQL package includes the time class. This class is merely a lightweight wrapper for java. util. THANKS TO THE recognize BC API can recognize this as a SQL...

3 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.