New features and enhancement


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

New features and enhancement

  1. 1. Tiger Features and Enhancements J2SE 5.0
  2. 2. Enhanced for Loop The new enhanced for construct lets you iterate over collections and arrays without using iterators or index variables. The new form of the for statement has the following syntax: for (FormalParameter : Expression) Statement
  3. 3. Autoboxing/Unboxing The new autoboxing/unboxing feature eliminates this manual conversion.
  4. 4. <ul><li>Generics </li></ul><ul><li>Consider the following class, Ex1, which creates a collection of </li></ul><ul><li>two Strings and one Integer, and then prints out the collection: </li></ul><ul><ul><ul><ul><ul><li>import java.util.*; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public class Ex1 { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>private void testCollection() { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>List list = new ArrayList(); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>list.add(new String(&quot;Hello world!&quot;)); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>list.add(new String(&quot;Good bye!&quot;)); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>list.add(new Integer(95)); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>printCollection(list); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>private void printCollection(Collection c) { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Iterator i = c.iterator(); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>while(i.hasNext()) { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>String item = (String); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>System.out.println(&quot;Item: &quot;+item); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>public static void main(String argv[]) { </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Ex1 e = new Ex1(); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>e.testCollection(); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul></ul>
  5. 5. The problem here is that an explicit cast is required in the printCollection method. This class compiles fine, but throws a ClassCastException at runtime as it attempts to cast an Integer to a String: Item: Hello world! Item: Good bye! Exception in thread &quot;main&quot; java.lang.ClassCastException: java.lang.Integer at Ex1.printCollection( at Ex1.testCollection( at Ex1.main( Generics, which are also known as parameterized types, provide compile-time typesafety for collections and eliminate the drudgery of casting.
  6. 6. <ul><ul><li>import java.util.*; </li></ul></ul><ul><ul><li>public class Ex2 { </li></ul></ul><ul><ul><li>private void testCollection() { </li></ul></ul><ul><ul><li>List <String> list = new ArrayList <String> (); </li></ul></ul><ul><ul><li>list.add(new String(&quot;Hello world!&quot;)); </li></ul></ul><ul><ul><li>list.add(new String(&quot;Good bye!&quot;)); </li></ul></ul><ul><ul><li>list.add(new Integer(95)); </li></ul></ul><ul><ul><li>printCollection(list); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>private void printCollection(Collection c) { </li></ul></ul><ul><ul><li>Iterator <String> i = c.iterator(); </li></ul></ul><ul><ul><li>while(i.hasNext()) { </li></ul></ul><ul><ul><li>String item =; </li></ul></ul><ul><ul><li>System.out.println(&quot;Item: &quot;+item); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public static void main(String argv[]) { </li></ul></ul><ul><ul><li>Ex2 e = new Ex2(); </li></ul></ul><ul><ul><li>e.testCollection(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  7. 7. <ul><li>Typesafe Enumerations </li></ul><ul><li>An enumerated type is a type whose values consist of a </li></ul><ul><li>fixed set of constants. </li></ul><ul><li>public enum MainMenu {FILE, EDIT, FORMAT, VIEW}; </li></ul><ul><li>The word enum is reserved and therefore if you have been using </li></ul><ul><li>it as an identifier, you should adjust your code when compiling with a </li></ul><ul><li>J2SE 5.0 compiler. </li></ul><ul><li>The above enum declaration generates a class (MainMenu), </li></ul><ul><li>which automatically implements the Comparable<MainMenu> </li></ul><ul><li>and Serializable interfaces, and provides several members including: </li></ul><ul><ul><li>Static variables FILE, EDIT, FORMAT, and VIEW </li></ul></ul>
  8. 8. <ul><ul><li>Static method values(), which is an array containing the constants </li></ul></ul><ul><ul><li>in the enum </li></ul></ul><ul><ul><li>static method valueOf(String) that returns the appropriate enum for </li></ul></ul><ul><ul><li>the string passed in </li></ul></ul><ul><ul><li>Appropriately overloaded equals(), hashCode, toString(), and </li></ul></ul><ul><ul><li>compareTo() methods. </li></ul></ul>
  9. 9. <ul><li>Static Imports </li></ul><ul><li>package; </li></ul><ul><li>interface XYZ { </li></ul><ul><li>public static final double Constant1 = someValue; </li></ul><ul><li>public static final double Constant2 = anotherValue; </li></ul><ul><li>} </li></ul><ul><li>Now, the constants in the XYZ interface can be used as follows: </li></ul><ul><li>public class MyClass implements XYZ { </li></ul><ul><li>.... </li></ul><ul><li>double value = 2 * Constant1; </li></ul><ul><li>... </li></ul><ul><li>} </li></ul><ul><li>In J2SE 5.0, </li></ul><ul><ul><ul><li>import static*; </li></ul></ul></ul><ul><ul><ul><li>public class MyClass { </li></ul></ul></ul><ul><ul><ul><li>... </li></ul></ul></ul><ul><ul><ul><li>double value = 2 * Constant1; </li></ul></ul></ul><ul><ul><ul><li>... </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  10. 10. Metadata The J2SE 5.0 metadata feature is a facility that allows developers to annotate their code so that tools can generate boilerplate code (e.g stub generation to remote procedure calls) as directed by annotations. This facility allows for parsing of your Java files and generating artifacts such as XML descriptors or source code.
  11. 11. <ul><li>Others </li></ul><ul><li>Variable Arguments : </li></ul><ul><li>The variable arguments new functionality in J2SE 5.0 </li></ul><ul><li>allows multiple arguments to be passed as parameters to methods as in: </li></ul><ul><ul><li>void someMethod(Object ... args) { </li></ul></ul><ul><ul><li>// do something </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>// invoke the method </li></ul></ul><ul><ul><li>someMethod(&quot;arg1&quot;, &quot;arg2&quot;, &quot;arg3&quot;); </li></ul></ul>
  12. 12. <ul><ul><li>Formatted Output : The variable arguments functionality has </li></ul></ul><ul><ul><li>been used to implement the flexible number of arguments required </li></ul></ul><ul><ul><li>for printf. </li></ul></ul><ul><ul><li>That is right! J2SE 5.0 provides C-like printf functionality, so now </li></ul></ul><ul><ul><li>it is possible to easily format output using printf: </li></ul></ul><ul><ul><li>System.out.printf(&quot;%s %3d&quot;, name, age); </li></ul></ul>
  13. 13. Enhanced Input : Prior to J2SE 5.0, in order to read an integer value from the keyboard, it has to be read as a String and then parsed as follows BufferedReader br = new BufferedReader(new InputStreamReader (; String str = br.readLine(); int n = Integer.parseInt(str); In J2SE 5.0, the java.util.Scanner class can be used to accomplish the same thing but with less code as follows: Scanner reader = new Scanner(; int n = reader.nextInt();