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:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...