VIVEK SHAH

011011010010000001100001011001000110010001101001011000110111010001100101011001000010000001110100011011110010000001101010011000010111011001100001 Click Here

Custom Search



Here are 6 of the new features that have been completed:
  • Language support for collections
  • Automatic Resource Management
  • Underscores in numeric literals
  • Strings in switch
  • Binary literals
  • Simplified Varargs Method Invocation
An update was given on the new language changes that will be in Java 7. The JDK currently has a release date of September 2010.
There is a lot more to Java 7 then just these language changes. I’ll be exploring the rest of the release in future posts. One of the big debates is currently around Closures, which are a separate JSR.

Language support for collections

Java will be getting first class language support for creating collections. The style change means that collections can be created like they are in Ruby, Perl etc.
Instead of:
List list = new ArrayList<String>();
list.add("item");
String item = list.get(0);

Set set = new HashSet<String>();
set.add("item");

Map map = new HashMap<String, Integer>();
map.put("key", 1);
int value = map.get("key");
You will be able to use:
List list = ["item"];
String item = list[0];

Set set = {"item"};

Map map = {"key" : 1};
int value = map["key"];
These collections are immutable.

Automatic Resource Management

Some resources in Java need to be closed manually like InputStream, Writers, Sockets, Sql classes. This language feature allows the try statement itself to declare one of more resources. These resources are scoped to the try block and are closed automatically.
This:
BufferedReader br = new BufferedReader(new FileReader(path));
try {
   return br.readLine();
} finally {
   br.close();
}
becomes:
try (BufferedReader br = new BufferedReader(new FileReader(path)) {
   return br.readLine();
}
You can declare more than one resource to close:
try (
   InputStream in = new FileInputStream(src);
   OutputStream out = new FileOutputStream(dest))
{
 // code
}
To support this behaviour all closable classes will be retro-fitted to implement a Closableinterface.

Underscores in numeric literals

Long numbers are hard to read. You can now split them up using an underscore in ints and longs:
int one_million = 1_000_000;

Strings in switch

Currently you can only use numbers or enums in switch statements. String has been added as a candidate:
String s = ...
switch(s) {
 case "quux":
    processQuux(s);
    // fall-through

  case "foo":
  case "bar":
    processFooOrBar(s);
    break;

  case "baz":
     processBaz(s);
    // fall-through

  default:
    processDefault(s);
    break;
}

Binary literals

Java code, due to its C heritage, has traditionally forced programmers to represent numbers in only decimal, octal, or hexadecimal.
As quite a few domains are bit orientated, this restriction can introduce errors. You can now create binary numbers using an 0b prefix.
int binary = 0b1001_1001;

Simplified Varargs Method Invocation

When a programmer tries to invoke a *varargs* (variable arity) method with a non-reifiable varargs type, the compiler currently generates an “unsafe operation” warning. JDK 7 moves the warning from the call site to the method declaration. This will enable API designers to use varargs due to the reduction of warnings reported.
This one is slightly more involved so you are better off looking at the proposal.
Thanks to John Wright


0 comments:

Post a Comment