Boxing & Unboxing in Java

What is boxing and unboxing?
Wrapper classes are those whose objects wraps a primitive data type within them. In the java.lang package java provides a separate class for each of the primitive data type namely Byte, Character, Double, Integer, Float, Long, Short.
Converting primitive datatype to object is called boxing.

Example

Integer obj = new Integer ("9339");
Whereas, converting an object into corresponding primitive datatype is known as unboxing.

Example

public class Sample {
   public static void main (String args[]){
      Integer obj = new Integer("9339");
      int i = obj.intValue();
      System.out.println(i);
   }
}

Output

9339

No comments:

Post a Comment

any problem in any program comment:-

Write a program to accept two binary numbers and display their sum.

 import java.util.Scanner; class BinaryAdditionNumeric {     public static void main(String[] args) {         Scanner sc = new Scanner(Syste...