Metadata or Annotation
-
Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
-
Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
-
Runtime processing — Some annotations are available to be examined at runtime.
Annotations look like class names but start with @ at the front. The annotation appears first, often by convention on its own line, and may include elements with named or unnamed values:
@Author(
name = "Benjamin Franklin",
date = "3/27/2003"
)
class MyClass() { }
or
@SuppressWarnings(value = "unchecked")
void myMethod() { }
If there is just one element named "value," then the name may be omitted, as in:
@SuppressWarnings("unchecked")
void myMethod() { }
Also, if an annotation has no elements, the parentheses may be omitted, as in:
@Override
void mySuperMethod() { }
There are three annotation types that are predefined by the language specification itself: @Deprecated, @Override, and @SuppressWarnings.
@Deprecated—the @Deprecated annotation indicates that the marked element is deprecated and should no longer be used.
@Deprecated
static void deprecatedMethod() { }
@Override—the @Override annotation informs the compiler that the element is meant to override an element declared in a superclass.
@Override
int overriddenMethod() { }
@SuppressWarnings—the @SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the example below, a deprecated method is used and the compiler would normally generate a warning. In this case, however, the annotation causes the warning to be suppressed.
@SuppressWarnings("deprecation")
Generic Types
Generic Types allows the developer of a class to specify specific types as parameters, instead of using Object and having to cast. This results in better type safety, because the compiler can better check arguments at compile time. One of the first places to see generic types in action is the Collections API. The next example uses the 1.4 libraries and the default javac compile mode.
ArrayList list = new ArrayList();
list.add(0, new Integer(50));
int total = ((Integer)list.get(0)).intValue();
The cast to Integer on the last line is an example of the typecasting issues that generic types aim to prevent. The issue is that the 1.4 Collection API uses the Object class to store the Collection objects, which means that it cannot pick up type mismatches at compile time. The first notification of a problem is a ClassCastException at runtime.
The same example with the generified Collections library is written as follows:
ArrayList list = new ArrayList();
list.add(0, new Integer(50));
int total = list.get(0).intValue();
The user of a generified API has to simply declare the type used at compile type using the notation. No casts are needed and in this example trying to add a String object to an Integer typed collection would be caught at compile time.
Generic types therefore 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. Designing your own Generic APIs is a little more complex than simply using them.
Autoboxing and Auto-Unboxing of Primitive Types:
Autoboxing and Auto-unboxing is automatic conversion between primitives and their wrappers. Converting between primitive types, like int, float, boolean, and their equivalent Object-based counterparts like Integer, Float and Boolean, can require unnecessary amounts of extra coding, especially if the conversion is only needed for a method call to the Collections API, for example.
The autoboxing and auto-unboxing of Java primitives produces code that is more concise and easier to follow. In the next example an int is being stored and then retrieved from an ArrayList. The 5.0 version leaves the conversion required to transition to an Integer and back to the compiler.
Java 1.4
ArrayList list = new ArrayList();
list.add(0, new Integer(50));
int total = (list.get(0)).intValue();
Java 1.5
ArrayList list = new ArrayList();
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. The compiler generates the looping code necessary and with generic types no additional casting is required.
Java 1.4:
ArrayList list = new ArrayList();
for (Iterator i = list.iterator(); i.hasNext();) {
Integer value=(Integer)i.next();
}
Java 1.5:
ArrayList list = new ArrayList();
for (Integer i : list) { ... }
Enumerated Types
Java 5 introduce enum as new Java Type. An enum type is a type whose fields consist of a fixed set of constants. Some common examples include compass directions (NORTH, SOUTH, EAST, and WEST), the days of the week, the months in a year etc.
Because they are constants, the names of an enum type's fields are in uppercase letters.
In the Java programming language, you define an enum type by using the enum keyword. For example, you would specify a days-of-the-week enum type as:
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
Static Import
The static import feature , implemented as "import static", 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.
import static java.awt.BorderLayout.*;
getContentPane().add(new JPanel(), CENTER);
Formatted Output
Developers now have the option of 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.
Most of the common C printf formatters are available, and in addition some Java classes like Date and BigInteger also have formatting rules. See the java.util.Formatter class for more information. Although the standard UNIX newline '\n' character is accepted, but for cross-platform support of newlines the Java %n is recommended.
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 Scanner methods like next and nextInt will block if no data is available. If you need to process more complex input, then there are also pattern-matching algorithms, available from the java.util.Formatter class.
The following example reads a String from standard input and expects a following int value.
Scanner s= new Scanner(System.in);
String param= s.next();
int value=s.nextInt();
s.close();
Variable argument or Varargs
The Variable argument i.e. varargs functionality allows multiple arguments to be passed as parameters to methods. It requires the simple ... notation (three dots) for the method that accepts the argument list and is used to implement the flexible number of arguments required for printf.
void argtest(String... args) {
for (String s : args) {
System.out.println(s);
}
}
argtest("test", "data");
Summary
These are some of the Java 5 features which are available to Java Programmers. There are many more new features and ehancements in Java 5 like new StringBuilder class that works essentially as an unsynchronized StringBuffer for performance enhancement, Garbage collection ergonomics that provides the automatic detection and choice of the client or server runtime compilers, Java 2D performance enhancement, Image I/O performance and memory usage improvement. Java 5 also adds many new features and enhancement for Java Virtual Machine, improvement in many base libraries, improvement in Integration Libraries like RMI, JDBC, JNDI, improvement in Java User Interface design technologies, new JVM Profiling API, and improved diagnostic utility. You can go through Java 5 release note for details of the new features.






