Autoboxing in Java 5

Surprisingly after Sun Microsystems included the JDK 5 and now it’s already JDK 6. JDK 7 is currently in closed beta mode and available for testing which shortly would be announced to be available sometime next year with a different syntax in certain codes.

Did some code sampling and simple display to show what has changed from Java 1.4 to Java 5. The changes were major. From here I am going to explain is Integer and int data type the same? This is what we call the concept of AUTOBOXING.

AutoBoxing Java

Some basic Integer and int Autoboxing comparison done.


public class TestInt {

 public static void main(String[] args) {

  Integer e = 1;
  int f = 1;
  //Delclare variables

  if (e == f)
   System.out.println("gila same");
  else
   System.out.println("The value is Not the same");
  System.exit(0); //Exit System
 }
}

If you are doing this code, try to copy this onto your IDE and compile it. Most probably you will get the ClassCastException or just a display saying “The value is not the same” if you’re running this code on JDK 1.4 or lower. The amazing feat by Java 5 and above is that now Java would know that though Integer and int are two different variable types, it will try to assimilate both to appear the same.

The resulting result is it will compare both the different data types and presto, Line 10 will display “Gila Same“.

Additional Notes : There are many more things Java has changed since JDK 1.4. More articles and examples will be out soon for this programming language.

No Responses

  1. Pingback: Cool New Loop in Java 5 | TechieDan October 27, 2009
  2. Pingback: More AutoBoxing in Java | TechieDan March 3, 2011

Leave a Reply