Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
JAVA 5 FEATURES Byju Veedu Metadata (Annotations) • The metadata feature in J2SE 5.0 provides the ability to associate additional data alongside Java classes, interfaces, methods, and fields. This additional data, or annotation, can be read by the javac compiler or other tools, and depending on configuration can also be stored in the class file and can be discovered at runtime using the Java reflection API. • Annotation processing tool (Apt) includes a set of new reflective APIs and supporting infrastructure to process program annotations. • The apt reflective APIs provide a build-time, source-based, read-only view of program structure which cleanly models the Java programming language's type system. • First, apt runs annotation processors that can produce new source code and other files. Next, apt can cause compilation of both original and generated source files, easing development. Declaring annotation import java.lang.annotation.*; /** * Indicates that the annotated method is a test method. * This annotation should be used only on parameter less static methods. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @interface Test { } Using Annotation class Foo { @Test public static void m1() { } public static void m2() { } @Test public static void m3() { throw new RuntimeException("Boom"); } public static void m4() { } @Test public static void m5() { } public static void m6() { } @Test public static void m7() { throw new RuntimeException("Crash"); } public static void m8() { } } Processing annotation public class RunTests { public static void main(String[] args) throws Exception { int passed = 0, failed = 0; for (Method m : Class.forName(args[0]).getMethods()) { if (m.isAnnotationPresent(Test.class)) { try { m.invoke(null); passed++; } catch (Throwable ex) { System.out.printf("Test %s failed: %s %n", m, ex.getCause()); failed++; } } } System.out.printf("Passed: %d, Failed %d%n", passed, failed); } } Generic Types • Generic types enable an API designer to provide common functionality that can be used with multiple data types and which also can be checked for type safety at compile time. • The Collections API provides common functionality like LinkedLists, ArrayLists and HashMaps that can be used by more than one Java type • The user of a generified API has to simply declare the type used at compile type using the <> notation Generics Example • Without Generics ArrayList list = new ArrayList(); list.add(0, new Integer(42)); int total = ((Integer)list.get(0)).intValue(); • Using Generics ArrayList<Integer> list = new ArrayList<Integer>(); list.add(0, new Integer(42)); int total = list.get(0).intValue(); Autoboxing and Auto-unboxing • Automatic converting between primitive types, like int, boolean, and their equivalent Object-based counterparts like Integer and Boolean etc. • Without Boxing ArrayList<Integer> list = new ArrayList<Integer>(); list.add(0, new Integer(42)); int total = (list.get(0)).intValue(); • With Boxing ArrayList<Integer> list = new ArrayList<Integer>(); list.add(0, 42); int total = list.get(0); Enhanced for loop The new enhanced for loop can replace the iterator when simply traversing through a Collection as follows • Before ArrayList<Integer> list = new ArrayList<Integer>(); for (Iterator i = list.iterator(); i.hasNext();) { Integer value=(Integer)i.next(); } • After ArrayList<Integer> list = new ArrayList<Integer>(); for (Integer i : list) { ... } Enumerated Types • This type provides enumerated type when compared to using static final constants . public enum StopLight { red, amber, green }; Static Import • Static Import enables you to refer to static constants from a class without needing to inherit from it. Instead of BorderLayout.CENTER each time we add a component, we can simply refer to CENTER. • Eg: import static java.awt.BorderLayout.*; getContentPane().add(new JPanel(), CENTER); Formatted Output • Using printf-type functionality to generate formatted output. This will help migrate legacy C applications, as the same text layout can be preserved with little or no change. • Eg: System.out.printf("name count%n"); System.out.printf("%s %5d%n", user,total); Formatted Input • The scanner API provides basic input functionality for reading data from the system console or any data stream. The following example reads a String from standard input and expects a following int value. • Eg: Scanner s= new Scanner(System.in); String param= s.next(); int value=s.nextInt(); s.close(); Varargs • The varargs functionality allows multiple arguments to be passed as parameters to methods. It requires the simple ... notation for the method that accepts the argument list and is used to implement the flexible number of arguments required for printf. • Eg; void argtest(Object ... args) { for (int i=0;i <args.length; i++) { } } argtest("test", "data"); Concurrency Utilities • Java.cocurrent.util package provides powerful, high-level thread constructs, including executors, which are a thread task framework, thread safe queues, Timers, locks (including atomic ones), and other synchronization primitives. Hands-on • Using annotations • Enhanced for loop • Static import • Generics using collections • Autoboxing • Enums