Playing With Java 7

It’s been a while since Oracle released Java 7 to the wild. I haven’t had the opportunity to play with it that much because I mainly use my Java skills for Android development. As of now Android doesn’t support Java 7. However, I recently had the chance to look at the new features of it. So much more work has been done on code simplifications, there’s a new set of package for files system manipulation and a major improvement to dynamic language support. Below are the features that caught my eyes. Especially with the code simplifications.

String in Switch statement
This is a feature I’m really happy about and ready to use. There have been countless times when I wished I could have just used a String in a switch statement. When I hit this block when coding, I’m forced by Java to refactor my code to use a primitive datatype in my switch statement. With Java 7, I don’t have to worry anymore.

Code speaks louder than words. See for yourself.

1
2
3
4
5
6
7
8
9
10
11
12
13
String fullName = boys.getFullName();

    switch (fullName) {
        case "Addo Quaye":
            System.out.println("He is the SRC president");
            break;
        case "Boateng Nicolas":
            System.out.println("He is the SU president");
            break;
        default:
            System.out.println("He is no body!");
            break;
    }

Diamond Operator
The Diamond operator <> is a Java 7 feature that provide type inference when creating object of a Generic class. It means you don’t have to declare the type on both sides when creating an object of a Generic class.

Old Syntax

 List<String> wildLife = new ArrayList<String>();

New Syntax

List<String> wildLife = new ArrayList<>();

Underscore in numeric literals
This doesn’t change in functionality at all. It makes code easier to read and will save some time and aggravation.

1
2
3
4
5
6
7
8
// Compared to
int aMillionOld = 1000000;

// even more examples
int aBillionNew = 1_000_000_000;

// compared to
int aBillionOld = 1000000000;

You see it’s way easier to read huge numeric literal values now.

Automatic resource management aka Try with resources
With the old Java, when you make a reference to a class inside a  try block to make use of a resource, you’ll have to manually free up the resource inside the  finally block. With Java 7, there is a new syntax that makes you avoid having to free up the resource manually. The JVM does this for you automatically.

New syntax

1
2
3
4
5
6
7
// Java 7 syntax
        try(FileOutputStream fOutputStream = new FileOutputStream("nofile.txt");
                DataOutputStream dOutputStream = new DataOutputStream(fOutputStream);) {
            dOutputStream.writeUTF("This is very sweet");
        }catch(IOException e) {
            e.printStackTrace();
        }

Old syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        // Old Java syntax
        FileOutputStream fOutStream = null;
        DataOutputStream dOutStream = null;

        try {
            fOutStream = new FileOutputStream("oldfile.txt");
            dOutStream = new DataOutputStream(fOutStream);
            dOutStream.writeUTF("This is so much work");
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fOutStream !=null)
                    fOutStream.close();

                if (dOutStream !=null)
                    dOutStream.close();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }

Do you see the code simplification between the Java 7 syntax and the old Java?

Multiple exception in a single catch block
With this feature you can catch multiple exceptions in one catch declaration. If a method call throws three different exceptions, you can easily catch them a single catch block compared to the old Java syntax where you have to catch them in different catch blocks.

1
2
3
4
5
6
    try {
        // This method call throws these exceptions below
        watchKeyRegister();
    }catch(UnsupportedOperationException | IllegalArgumentException | ClosedWatchServiceException | IOException | SecurityException sEx ) {
        sEx.printStackTrace();
    }