Your SlideShare is downloading. ×

New features and enhancement

780

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
780
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Tiger Features and Enhancements J2SE 5.0
  • 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. Autoboxing/Unboxing The new autoboxing/unboxing feature eliminates this manual conversion.
  • 4.
    • Generics
    • Consider the following class, Ex1, which creates a collection of
    • two Strings and one Integer, and then prints out the collection:
            • import java.util.*;
            • public class Ex1 {
            • private void testCollection() {
            • List list = new ArrayList();
            • list.add(new String("Hello world!"));
            • list.add(new String("Good bye!"));
            • list.add(new Integer(95));
            • printCollection(list);
            • }
            • private void printCollection(Collection c) {
            • Iterator i = c.iterator();
            • while(i.hasNext()) {
            • String item = (String) i.next();
            • System.out.println("Item: "+item);
            • }
            • }
            • public static void main(String argv[]) {
            • Ex1 e = new Ex1();
            • e.testCollection();
            • }
            • }
  • 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 "main" java.lang.ClassCastException: java.lang.Integer at Ex1.printCollection(Ex1.java:16) at Ex1.testCollection(Ex1.java:10) at Ex1.main(Ex1.java:23) Generics, which are also known as parameterized types, provide compile-time typesafety for collections and eliminate the drudgery of casting.
  • 6.
      • import java.util.*;
      • public class Ex2 {
      • private void testCollection() {
      • List <String> list = new ArrayList <String> ();
      • list.add(new String(&quot;Hello world!&quot;));
      • list.add(new String(&quot;Good bye!&quot;));
      • list.add(new Integer(95));
      • printCollection(list);
      • }
      • private void printCollection(Collection c) {
      • Iterator <String> i = c.iterator();
      • while(i.hasNext()) {
      • String item = i.next();
      • System.out.println(&quot;Item: &quot;+item);
      • }
      • }
      • public static void main(String argv[]) {
      • Ex2 e = new Ex2();
      • e.testCollection();
      • }
      • }
  • 7.
    • Typesafe Enumerations
    • An enumerated type is a type whose values consist of a
    • fixed set of constants.
    • public enum MainMenu {FILE, EDIT, FORMAT, VIEW};
    • The word enum is reserved and therefore if you have been using
    • it as an identifier, you should adjust your code when compiling with a
    • J2SE 5.0 compiler.
    • The above enum declaration generates a class (MainMenu),
    • which automatically implements the Comparable<MainMenu>
    • and Serializable interfaces, and provides several members including:
      • Static variables FILE, EDIT, FORMAT, and VIEW
  • 8.
      • Static method values(), which is an array containing the constants
      • in the enum
      • static method valueOf(String) that returns the appropriate enum for
      • the string passed in
      • Appropriately overloaded equals(), hashCode, toString(), and
      • compareTo() methods.
  • 9.
    • Static Imports
    • package com.name;
    • interface XYZ {
    • public static final double Constant1 = someValue;
    • public static final double Constant2 = anotherValue;
    • }
    • Now, the constants in the XYZ interface can be used as follows:
    • public class MyClass implements XYZ {
    • ....
    • double value = 2 * Constant1;
    • ...
    • }
    • In J2SE 5.0,
        • import static com.name.XYZ.*;
        • public class MyClass {
        • ...
        • double value = 2 * Constant1;
        • ...
        • }
  • 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.
    • Others
    • Variable Arguments :
    • The variable arguments new functionality in J2SE 5.0
    • allows multiple arguments to be passed as parameters to methods as in:
      • void someMethod(Object ... args) {
      • // do something
      • }
      • // invoke the method
      • someMethod(&quot;arg1&quot;, &quot;arg2&quot;, &quot;arg3&quot;);
  • 12.
      • Formatted Output : The variable arguments functionality has
      • been used to implement the flexible number of arguments required
      • for printf.
      • That is right! J2SE 5.0 provides C-like printf functionality, so now
      • it is possible to easily format output using printf:
      • System.out.printf(&quot;%s %3d&quot;, name, age);
  • 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 (System.in)); 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(System.in); int n = reader.nextInt();

×