Wednesday, April 11, 2007

There a lots of sites that talk about the Language Enhancements in JDK 1.5. To Summarizse the Enhancements :-

Generics
Enhanced for Loop
Autoboxing/Unboxing
Typesafe Enums
Varargs
Static Import
Metadata (Annotations)

Here's my code that demonstrates all these features :-




import static java.lang.Math.max;

import java.util.ArrayList;

public class NewFeatures
{
public static void main(String[] args)
{


NewFeatures.autoBoxing();
NewFeatures.forLoop();
NewFeatures.newEnums();
NewFeatures.staticImports();
NewFeatures.variableArguments("Hello","Sandeep","to","JDK 1.5");
NewFeatures.generics();
NewFeatures.annotations();


}

public static void autoBoxing()
{
int i = 1;
Integer objInteger = 1;

i = i + objInteger;

System.out.println(" NewFeatures.autoBoxing :: The Value of the Integer is :: "+i);
}

public static void forLoop()
{
int iArray[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};

System.out.println();

for (int iArrayElement : iArray)
{
System.out.println(" NewFeatures.forLoop :: The Array Element is :: "+iArrayElement);
}

System.out.println();

}

public static void newEnums()
{
for (Names objNames : Names.values())
{
System.out.println(" NewFeatures.newEnums :: " + objNames);
}
}

public enum Names { Sandeep, Seshan };


public static void staticImports()
{
double a = 100.1, b = 103.25;

System.out.println(" NewFeatures.staticImports :: The Maximum value is :: "+ max(a,b));

}

public static void variableArguments(String... args)
{
System.out.println(" NewFeatures.variableArguments :: The Number of Arguments are :: " + args.length);
}

public static void generics()
{
ArrayList alStringArrayList = new ArrayList();

alStringArrayList.add("I");
alStringArrayList.add("am");
alStringArrayList.add("Sandeep");
alStringArrayList.add("Seshan");

for( String strElement : alStringArrayList )
{
System.out.println(" NewFeatures.generics :: " + strElement);
}
}

public static void annotations()
{
System.out.println(" NewFeatures.annotations :: " + new NewFeatures());
}

@Override
public String toString()
{
return "New Features in JDK1.5";
}
}





Here's the Batch File to compile & run this :-



REM ***** START TESTING FOR JDK 1.5 *****

del *.class

SET PATH=D:\JDeveloper\jdk\bin;%PATH%

SET CLASSPATH=.;%CLASSPATH%

java -fullversion

javac NewFeatures.java

java NewFeatures

REM ***** END TESTING FOR JDK 1.5 *****



Here's the output :-



NewFeatures.autoBoxing :: The Value of the Integer is :: 2

NewFeatures.forLoop :: The Array Element is :: 0
NewFeatures.forLoop :: The Array Element is :: 1
NewFeatures.forLoop :: The Array Element is :: 2
NewFeatures.forLoop :: The Array Element is :: 3
NewFeatures.forLoop :: The Array Element is :: 4
NewFeatures.forLoop :: The Array Element is :: 5
NewFeatures.forLoop :: The Array Element is :: 6
NewFeatures.forLoop :: The Array Element is :: 7
NewFeatures.forLoop :: The Array Element is :: 8

NewFeatures.newEnums :: Sandeep
NewFeatures.newEnums :: Seshan
NewFeatures.staticImports :: The Maximum value is :: 103.25
NewFeatures.variableArguments :: The Number of Arguments are :: 4
NewFeatures.generics :: I
NewFeatures.generics :: am
NewFeatures.generics :: Sandeep
NewFeatures.generics :: Seshan
NewFeatures.annotations :: New Features in JDK1.5



I apologize for the psychedellic colors :)