I always laughed at colleagues at university when they ended up as Java monkey^Wprogrammer after graduation. Now I’m one of them. I think that’s karma ;-) FWIW, I’m now part of the Graal compiler team. A lot of geeks around me now, so I’m feeling quite comfortable :-)

Although I avoided Java at university where possible, it isn’t unfamiliar to me. At the HTL St. Pölten (on a similar level as high school, but still rather unique in Europe), we had a quite good introduction to Java—I’m even a “Sun Certified Java Programmer”. Take that!

Java…

I had a hard time to get started with eclipse, but with vrapper it feels already like a retarded home :p Of course, I could’ve used vim, but the IDE support by eclipse is just too essential.

As a Haskell guy, I’m looking forward to JSR335. There’re some nice features where my Haskell-infected-mind thinks “Shut up and take my money!”, although it feels still crippled ;-P

During the last month I’ve studied a lot of existing Java code and discovered interesting techniques/features of Java I didn’t know yet:

try-with-resources

I like extensions to languages which reduces boilerplate code. try-with-resources is one of them, introduced with Java SE 7. Simple example (stolen from the tutorial); you can replace this:

static String fun1(String path) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(path));
    try {
        return br.readLine();
    } finally {
        if (br != null) br.close();
    }
}

with that

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

How does it work? BufferedReader has to implement a interface AutoCloseable, which has a method close(). From a bytecode point of view, the latter version is more complicated, because it involves a mechanism to suppress exceptions occurring in the resource block. Since it’s done by the compiler, we shouldn’t care so much :-)

return with side-effects

int foo() {
    try {
        return this.bar;
    } finally {
        this.bar++;
    }
}

I call it return with side-effects, dunno if there’s a official name for it. Of course, this is a stupid example. I’ve found this pattern in an implementation of Iterator.next() that deals with references. It looked reasonable to me, although it was a bit irritating first.

type patterns in catch statement

Again a feature of Java SE 7 to eliminate boilerplate code:

} catch (IOException ex) {
    logger.log(ex);
    throw ex;
} catch (SQLException ex) {
    logger.log(ex);
    throw ex;
}

rewrites to this:

} catch (IOException|SQLException ex) {
    logger.log(ex);
    throw ex;
}

wildcard in catch … wait a minute!

Not really a feature, but convenient if you already know the _ wildcard in Haskell:

} catch (Exception _) {
    System.exit(1);
}

This works because _ is a valid identifier in Java.



blog comments powered by Disqus

Author:

lewurm

Published

01 April 2013

Tags