Posts Tagged ‘JDK 7’

What’s New in the compiler of the JDK 7

Monday, September 7th, 2009

JavaLogo_060909

Darcy Joseph tells us of the developments that have the compiler in JDK 7. They are usually minor changes discussed at the forums, which include various amenities in the language syntax.

The news of the JDK 7 will be:

  • Switch to objects of type String
  • Automatic resource management
  • Improved type inference for the creation of Generics
  • Invocation of methods with varargs simplified
  • Improvements in numeric literals
  • Language Support for Collections
  • Language support for JSR 292

Let’s see in detail these features.

Switch to objects of type String

It will be possible to use the switch operator with objects of type String. This will avoid potential “if-then-else”. So this new feature would allow us to:

  String s = ...
  switch (s) {
   case "A1":
      processA1 (s);
      // And continue ...
 
    case "A2":
    case "A3":
      processA2A3 (s);
      break;
 
    case "A4"
       processA4 (s);
      // And continue ...
 
    default:
      processDefault (s);
      break;
  }

Automatic resource management

A “resource” is an object that must be closed manually, such as java.io.InputStream, OutputStream, Reader, Writer, Formatter;
java.nio.Channel; java.net.Socket; java.sql.Connection, Statement, ResultSet among others. These resources are usually open in a try block and close in a finally.

Recursos automatic management is a special form of the operator where you try one or more state resources. The scope of these resources is limited to the block. When the block ends, either normally or abruptly, all declared resources are automatically closed.

The main advantage is that it eliminates the need for manual closing, and it causes errors. Furthermore, it “prevents” the second exception (the close ()), always leaving the exception of the block that is usually more interesting.

An example usage would be:

 static String readFirstLineFromFile(String path) throws IOException {
 
        try (BufferedReader br = new BufferedReader(new FileReader(path)) {
 
           return br.readLine();
 
        }
 }

As we see, the try block () declares a resource, is then used within the block. When finished, it closes automatically. It is also possible to declare more than one resource:

try (InputStream in = new FileInputStream(src);
     OutputStream out = new FileOutputStream(destination)) {
 
            byte[] buf = new byte[8192];
            int n;
            while ((n = in.read(buf)) >= 0)
                out.write(buf, 0, n);
}

Improved type inference for the creation of Generics

This also includes the possibility to infer the types for instantiation using Generics. In cases parameterized types are obvious from the context, this parameter in the constructor can be omitted and replaced with an empty set <>.

For example, the following line:

 Map<String, List<String>> testmap = new HashMap<String, List<String>>();

can be replaced by:

Map<String, List<String> testmap = new HashMap<>();

Improvements in numeric literals

Just as we can declare decimal numeric literals, octal and hexadecimal literals now we can declare binary. This is very useful in certain areas. For this, use the prefix 0b, which is also the same compilers that use C / C + +, Ruby and Python. For example:

// A literal 'byte' 8-bit
byte wcByte = (byte)0b00100101;
 
// A literal 'short' 16-bit
short wcShort = (short)0b1010000101010101;
 
//  More literal 'int' of 32-bit 'int'
int wcInt1 = 0b10100001010001011010000101000101;
int wcInt2 = 0b101;

These variables are then used normally, for example:

class A2 {
public static void main(String[] args) {
    System.out.println("The decimal value of 10100101 is " + 0b10100101);
}

On the other hand, added the possibility to use an underscore (_) separator of numbers, to facilitate the reading of them. Thus, the compiler will ignore the character “_” when used in numeric literals. For example:

int numberOfTelephone = 757_757_7575;
long CreditCard = 1234_5678_9123_4567L;
long bundle = 777_77_7575L;
float money = 12_345_132.75;
long hexBytes = 0xFF_EC_AE_5E;
long hexWords = 0xFFEC_AE5E;
long maxLong = 0x7fff_ffff_ffff_ffffL;
long bytes = 0b11010010_01101001_10010101_10010010;

Language Support for Collections

It adds language support to treat like Arrays Collection.

For example, you can write the following to declare an immutable List:

final List wcDigits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9];

It will also be possible to access lists and maps using array syntax:

public class Main {
  public static void main(String[] arguments) {
    List wcArrayList = Arrays.asList(new String[] {"a","b","c"});
    String fristElement = wcArrayList[0]
 
    Map testmap = new HashMap(4);
    testmap[Integer.valueOf(1)] = "One";
}

Language support for JSR 292

This low-level change made minor changes to the Java language to be simpler to work with some features of the JVM. This will allow Java code to interoperate and / or implement libraries in other languages. That is, will ensure greater integration between Java and code in other languages. For more information read the proposal of support for JSR 292.