Java Autoboxing and Unboxing with Example

Autoboxing is the automatic conversion of primitive value to the corresponding wrapper object by Java Compiler is called Autoboxig.
For example, converting an int to an Integer, a double to a Double . see the snippet below
Integer I = 10;
Example
package com.java2study.practice;
public class AutoboxingTest {
static Integer I = 100;//Autoboxing


public static void main(String[] args) {
System.out.println(I);

}

}
output
100
Unboxing is the automatic conversion of wrapper object to the corresponding primitive value by Java Compiler is called Unboxing.
For Example
int i = new Integer(100);
Example
package com.java2study.practice;

public class UboxingTest {
int i;

public static void
main(String[] args) {
int i = new Integer(100);// unboxing
System.out.println(i);

}

}
output
100

No comments:

Post a Comment

-->