Java/J2EE and SOA (324 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

All You Need To Know About Wrapper Class In Java : Autoboxing And Unboxing

Last updated on Jul 31,2019 370 Views
60 / 72 Blog from Java Core Concepts

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Java programming language is one of the most popular programming language nowadays. With concepts like variables, data types, classes and objects comes another important concept of wrapper class in java, which is essential for synchronization in multithreading, collection framework etc. In this article, we will discuss the need of wrapper class in java with various examples. Following are the concepts discussed in this blog:

What Is Java Wrapper Class?

Wrapper class provides a mechanism to convert primitive data types into wrapper class objects. Following are the equivalent wrapper class objects of primitive data types.

Primitive Data TypeWrapper Class

int

Integer

char

Character

float

Float

boolean

Boolean

double

Double

short

Short

long

Long

byte

Byte

Following is an example to show how you can make a java wrapper class object.

class wrapperClass{
public static void main(String args[]){
Integer myInt = 5;
Character myChar = "Edureka";
System.out.println(myInt);
System.out.println(myChar);
}
}
Output : 5
Edureka

In the above program, we have used the wrapper class instead of primitive data types.

Following are the methods to get the associated value from the wrapper objects.

  1. intValue()
  2. byteValue()
  3. shortValue()
  4. longValue()
  5. doubleValue()
  6. charValue()
  7. floatValue()
  8. booleanValue()

Below is an example to use these methods in a program:

class wrapperClass{
public static void main(String args[]){
 
Integer myInt = 10;
Character myChar = "edureka";
Float myFloat = 10.25;
System.out.println(myInt.intValue());
System.out.println(myChar.charValue());
System.out.println(myFloat.floatValue());
}
}
Output : 10
edureka
10.25

Similarly you can use other methods like doubleValue(), shortValue(), longValue(), byteValue() to get the respective values of the wrapper class objects.

Need Of Java Wrapper Class

  • They convert the primitive data types into objects.
  • Objects are needed to modify the arguments in a method.
  • The classes in java.util package only works with objects.
  • Data structures in the collection framework only store objects.
  • Objects helps in synchronization in multithreading.

Autoboxing

Autoboxing is the automatic conversion of the primitive data types into objects of their corresponding wrapper class.

import java.util.ArrayList;
class Autoboxing {
public static void main(String args[]){
char ch = 'e';
Character e = ch;
ArrayList<Integer> arraylist = new ArrayList<Integer>();
arraylist.add(10);
System.out.println(arraylist.get(0));
}
}
Output : 10

Unboxing

It is the reverse of autoboxing, where the wrapper class object is converted to their corresponding primitive data type.

import java.util.ArrayList;
class Unboxing{
public static void main(String args[])
{
Character ch = 'e';
char 'e' = ch;

ArrayList<Integer> arraylist = new ArrayList<Integer> ();
arraylist.add(10);
int number = arraylist.get(0);
System.out.println(number);
}
}
Output: 10

In this article, we have discussed the wrapper class in java which helps in converting the primitive data types into their respective objects. It helps in synchronization during multithreading and various other applications as well. Java is a versatile language with an abundance of efficient and revolutionary concepts. The industry demands the modern age developer to be thorough with fundamental concepts in programming languages beforehand, master your skills in Java with Edureka’s Java certification program and kick-start your learning to become a Java developer.

Got a question for us? Please mention this in the comment section of the “Wrapper class in Java” article and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.