Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Javatutorials Autoboxing in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Autoboxing in Java

Advertisement
Autoboxing in Java is the automatic transformation of primitive data types ((int, float, double) into their corresponding Wrapper class object (Integer, Float, Double) by Java compiler.

Autoboxing in Java is the automatic transformation of primitive data types ((int, float, double) into their corresponding Wrapper class object (Integer, Float, Double) by Java compiler.

One must also know about Unboxing, which means he automatic transformation of wrapper class object into their corresponding primitive data types.

Autoboxing or automatic transformation of primitive data types into Wrapper class by Java compiler takes place when:

  • a primitive data type is passed in a parameter of method, which is expecting an object of corresponding wrapper class
  • a primitive data type is assigned to a variable of the corresponding wrapper class.
Primitive data type Wrapper class
boolean Boolean
int Integer
char Character
float Float
double Double
byte Byte
long Long
short Short

Following is the example of Autoboxing in Java:

public class MainClass {
public static void main(String args[]) {
Boolean booleanObject = true;

if (booleanObject){
System.out.println("a is true");
}

Integer i = 'x'; // box a int
int i2 = i; // unbox a int

System.out.println("i2 is " + i2);
}
}

Output:

a is true
i2 is x

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 15, 2013

Related Tutorials

Discuss: Autoboxing in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com