Skip to main content

Wrapper class ,auto boxing and unboxing in Java



#Topic-6
Previous topic (5) we have discussed about Variables and Data Types in Java, we have gone through various data types and literals. If you have not gone through that topic, click this link What are the Variables and Data types using in Java?
In this section we will discuss about Wrapper class, auto-boxing, UN-boxing and many more!

Wrapper class in Java
Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper classes because they "wrap" the primitive data type into an object of that class. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs. 

Wrapper class in java are the Object representation of eight primitive types in java. All the wrapper classes in java are immutable and final. Java  auto boxing and UN-boxing allows easy conversion between primitive types and their corresponding wrapper classes in java programs.All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are sub classes of the abstract class Number.

The following two statements illustrate the difference between a primitive data type and an object of a wrapper class:  

int a = 25; //primitive data type
Integer b = new Integer(33); //using wrapper class


The first statement declares an int variable named a and initializes it with the value 25. The second statement instantiates an Integer object. The object is initialized with the value 33 and a reference to the object is assigned to the object variable b.




Why do we need wrapper classes?
· We need wrapper classes when we need a type that will fit in the Object world programming like Collection classes. We use primitive types when we want things to be simple. 
· Primitive types can’t be null but wrapper classes can be null. 
· Achieve Auto-boxing and UN-boxing.


Auto-boxing :
Auto-boxing is a process by which primitive type is automatically encapsulated (boxed) into its equivalent type wrapper 
Example of Auto-boxing in java:
class AutoBoxingDemo{
public static void main(String args[]){
int a=50;
Integer a1=new Integer(a); //Conversion – kind of autoboxing
Integer a2=5; //it is actual AutoBoxing , we are not using any construct or method
System.out.println(a2+" "+a3); 


}

UN-boxing: 
UN-boxing is a process by which the value of an object is automatically extracted from a type Wrapper class.
 Example of UN-boxing in java :
class UnboxingDemo{
public static void main(String args[]){
Integer i=new Integer(100);
int a=i; // Unboxing
System.out.println(a);
}
}


What is Next?
In the next section, we will be going through How to convert String into primitive data types and primitive data types into String.

If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. Thank you! - Shivananda Rai




Comments

Popular posts from this blog

Java Tutorial for Beginners

#Topic - 1: Learn Java in simple and easy steps starting from basic to advanced concepts with examples. Basically, you have to answer following questions before going to further. Now you want to be learning java. Great..!but why? Why would you like to learn java programming language? It’s okay if you don’t know the reason why you want to become a java developer. This is especially true if you are totally new and you have no idea what java language is, how it is simple or not, it has a good career path or not, etc. In order to help you have a better understanding and see if java programming language is for you or not, let me do a real quick introduction about java and answer some of common questions. I am going to cover these are all things below introduction topic. What is the prerequisite knowledge for learning Java or what skills are required for a fresher who wants to work in java?   I would suggest you to have some experience in C langu...

What is the software’s needed to start JAVA programming

#Topic-2 Last week we have discussed about some basic topics like how to start java , what is the use etc , If you are very new to Java programming then click this link Java Tutorials for Beginners. What is the software’s needed to start JAVA programming. If you are new to Java programming and wish to learn it right now by doing some hands-on practice, you have visited the right place. This tutorial will help you what is the software’s needed to write your first Java program, Throughout this tutorial, you will learn fundamental concepts and steps which are necessary for every Java fresher.  In order to write and run a Java program, you need below software / editor: 1. Java Development Kit (JDK) 2. Command prompt 3. Note pad OR 4.IDEs : These IDEs offer a variety of features, like: building Java applications, Testing, debugging, code inspections, code assistance, visual GUI builder and code editor, and more. While you can find several Java IDEs or integrated developmen...

How to convert String into primitive data types and primitive data types into String

#Topic-7 Previous topic (6) we have discussed about Wrapper class, auto-boxing, UN-boxing and other related topics. If you have not gone through that topic, click this link   Wrapper class, auto-boxing, UN-boxing in Java? In this section we will discuss about How to convert String into primitive data types and primitive data types into String. How to convert String into primitive data type: Note: value should be compatible to destination type (we can’t convert “java” into int)   //1st way Example of convert String to Integer:  String str1 = "5"; int result = Integer.parseInt(str1); // Using Integer.parsrInt() System.out.println(result); Example of convert String to Double: String str=”125”; double a1=Double.parseDouble(str); System.out.println(a1);  Example of convert String to Boolean: String a=”true”; boolean a=Boolean.parseBoolean(a);  System.out.println(a);   Example of convert...