Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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 Type | Wrapper 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.
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.
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
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 5th October,2024 5th October SAT&SUN (Weekend Batch) | View Details |
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co