Breaking News
Home / Java / Java FAQ / Major Changes in JDK7

Major Changes in JDK7

JDK 7 changesJDK 7.0 is would be released to the general public some time soon. Developers can try milestone version now. The 4 major changes in the JDK 7 from a programming perspective are:


  • Ability to use underscores in numerals.
  • Ability to use Diamond syntax with collections + generics.
  • Ability to use Strings for comparison in switch statements.
  • Ability to use binary literals

<!>

Let’s review of these changes in a bit detail. 

Underscores in Numerals

With this feature you can use underscore in numerals to make them more readable.

int i = 1_5;
System.out.println(i);
i = i*10;
System.out.println(i);
int j=20_5;
System.out.println(i-j);

This seems to help with the readability of the numbers. The JVM would strip all of these underscores for doing all math operations.

Example

1_5 * 10 would be 15 * 10 = 150

Diamond Syntax

In cases where parametrized types need to be explicitly declared for a constructor, and the full parametrized type < T1 , T2 , …Tn > of that constructor is obvious from the context, then the  parametrized type of the constructor can be replaced with an empty set of type parameters: <>. The <>  construct is legal to use when constructing an object and either assigning it to a variable, or when passing  it as a parameter.

For example, consider the following assignment statement:

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

This is rather lengthy, so it can be replaced with this:

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

String in Switch Statement

Now you can compare String in the case statement and don’t use int data type only like before. However should keep in mind that String comparison are slower than the int comparison but for sure is handy feature.

String s = “bar”;
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;
}

Use of Binary Literals

With this feature one can define binary numbers as literals in the code by prefixing it with 0b. Then these can be converted into other data types like byte , short etc. with simple casting.

 byte b = (byte)0b0001;
 short s = (short)0b0010;
 System.out.println(“Byte is : ” + b + ” Short is : ” + s);

Out of the these 4 changes, the most useful is the String usage in switch statement followed by <> syntax.

The other changes that did not make JDK7 were:

  •       Improved Exception Handling for Java
  •       Elvis and Other Null-Safe Operators
  •       Large arrays
[pb_builder]

Check Also

When is OCAJP Java certification going live?

After completing the BETA phase of testing last year, the Oracle’s new OCAJP Java certification, …

Advertisment ad adsense adlogger