Home > java > Coding with JDK7

Coding with JDK7

November 16th, 2009 CertPal Leave a comment Go to comments

jdk7 codeThe JDK7 milestone 5 update is available for download. Developers now have a chance to try coding with the new language semantics and see for themselves what it is like. The 4 major changes that affect the way one codes in java as of JDK 7 are

  • Using underscores in numerals.
  • Diamond syntax used to work with collections + generics.
  • Using Strings in switch statements.
  • Making use of binary literals

Here is a short code sample that you can use to check the new features out. Use a plain text editor and your old friends javac and java, to test it out. IDEs will not support the new syntax and will most likely complain.

Sample JDK 7 Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class Jdk7Tests {
 
    public static void main(String[] args) {
        Jdk7Tests jdk7Tests = new Jdk7Tests();
        jdk7Tests.integersWithUnderscores();
        jdk7Tests.stringSwitch();
        jdk7Tests.binaryLiteral();
        jdk7Tests.diamond();
    }
 
    private void integersWithUnderscores()
    {
        int i = 1_2;
        System.out.println(i);
        i*=10;
        System.out.println(i);
        int j=2_0;
        System.out.println(i-j);       
    }
 
    private void stringSwitch()
    {
        String key = "akey";
        switch (key)
        {
        case "":
        {
            System.out.println("Nothing");
            break;
        }
 
        case "akey":
        {
            System.out.println("Matched akey");
            break;
        }
        default:
        break;
        }
    }
 
    private void binaryLiteral()
    {
        byte aByte = (byte)0b001;
        short aShort = (short)0b010;
        System.out.println(aByte + " " + aShort);
    }
 
    private void diamond()
    {
        Set<String> set = new TreeSet<>();
        set.add("c");
        set.add("b");
        set.add("a");
        for (String val : set)
        {
            System.out.println(val);
        }
    }
}

Underscores and numerals:

This feature will be great to process SSNs / phone numbers. No more putting the data into a String, parsing it, stripping it off underscores, and then putting it into an integer. But confusion arises when you try to do some arithmetic with the numeral. What is 1_2 * 2 ? The JVM strips the underscores when processing the operation as can be seen from the code example above. Thus 1_2 * 10 = 120 and 120 – 2_0 = 100. Simple

Diamond:

This should simplify instantiating collections. Time to update my auto complete code templates in anticipation of JDK 7. It is a little easier to read the code inside the diamond() method, in my opinion. Nothing more to the syntax though.

Switch with strings:

This is a pretty neat feature. You can now switch using Strings besides int, etc etc. Code that used to assign int variables to their corresponding String counterparts can now use this feature to simplify their code. Often code that contains call back methods in several places might set a int variable in one place, indicating that a particular String was found and then process this at a later point in time, using a switch construct. Some developers use this technique to write code that processes XML with SAX. They should be able to chop a few lines off that code using this feature.

Binary literals:

You can now use a binary literal representation and have that converted to a data type, say a byte or short, with very little code. I am not able to think of a scenario where I would use this regularly. But some of those coding practice problems that involve bits should be easier to write up :)

Other features of interest that do not necessarily affect programming, involve better algorithm implementations and performance improvements. These improvements are said to bring a many fold increase to performance in some applications. Test them out if you have the time.




Categories: java Tags:
  1. November 17th, 2009 at 08:00 | #1

    Thanks for the update, very informative.

    I’m not sure I get the real benefit of the underscores and numerals though. They allow for arithmetic on them, without error. Why not create a simple wrapper object, rather than a primitive type for things like phone numbers and such.

    But I will wait it out ;) Do you have a link with more info on this new feature, I couldn’t find anything.

    Kind regards

  2. November 17th, 2009 at 09:06 | #2

    @Nick Verlinde
    The only useful link I could find with the numerals was from the openjdk site

    http://mail.openjdk.java.net/pipermail/coin-dev/2009-April/001628.html

    Not aware of any other link of use.

  3. Ben
    November 17th, 2009 at 15:25 | #3

    “This feature will be great to process SSNs / phone numbers.”

    A poor example! Just because SSNs and phone numbers are composed of digits, doesn’t make them numerals in any meaningful sense.

  4. Carl Smotricz
    November 17th, 2009 at 15:57 | #4

    Is anyone else underwhelmed by the number of significant, useful changes? Out of the 4, the “switch on Strings” is the only one that’s not IMO trivial fluff. Properties and closures, on the other hand, failed to make the grade. Well, I’m sure Java will be a worthy heir to COBOL.

  5. November 17th, 2009 at 15:57 | #5

    @Ben
    These were 2 examples that immediately came to mind and I still think it will make for easier coding. If you think this feature will be put to better use elsewhere feel free to drop a reply.

  6. November 17th, 2009 at 16:19 | #6

    @Carl Smotricz
    The multiple exceptions in catch is also something that I would have liked to see as an update, but I don’t feel let down by the changes. Perhaps these changes will be included down the line. Time will tell.

  7. Randy
    November 17th, 2009 at 18:27 | #7

    These are all mostly syntactic sugar.

    Switch with strings are the most useful.

    I think the underscores will be more useful to compiler developers, OS developers and unit tests where more long constants might be used. When was the last time that anyone *hardcoded* a telephone number or credit card number or SSN in their code? (At least I hope mine aren’t there. :-)

    Randy

  8. Mis Tigi
    November 17th, 2009 at 19:32 | #8

    Underscores are useful for bigger numbers, to make them easier to read in the code, e.g.:
    5_000_000, 2_600, 547_544_221

    @Carl Smotricz Yes, I am underwhelmed too, I think thats why this is called update to JDK, ie. JDK 7 and not to language itself – like Java 7

  9. November 18th, 2009 at 05:53 | #9

    @Randy
    Nice examples there. I see now why SSN and phone numbers are not great examples. I had initially assumed that the numbers can be processed and displayed with the underscores. This assumption turned out to be incorrect and the same was highlighted under the ‘LIBRARY SUPPORT:’ section on the open JDK site.

    @Mis Tigi
    Good observation

  10. November 19th, 2009 at 00:37 | #10

    No need to dust off Emacs, vi, or Notpad; there is a developer build of NetBeans that supports the language features in JDK 7 milestone 5; read more about the support at

    http://blogs.sun.com/darcy/entry/project_coin_milestone_5_netbeans

  11. November 19th, 2009 at 04:03 | #11

    @Joe Darcy
    Thanks for the link Joe. A very useful one indeed

  1. November 23rd, 2009 at 15:56 | #1