My blog. Like it? Subscribe to my feed or check out the archives.

Quirky Java 22 Feb 09

This term I'm taking a course on compiler construction, CS 444. For the course I, along with my group members Peter and Ian, am developing a compiler for a subset of Java. So far we have finished the scanner and parser. It's been an illuminating experience learning about the quirkiness hidden deep in the Java Language Specification and I wanted to share a few of the more esoteric constructs we discovered.

I successfully compiled all of the following examples on my aging Windows XP laptop with javac 1.6.0_04. First, a single semicolon is valid Java file! So

// in Quirky.java
;

is a perfectly valid and so is

// in Quirky.java
;
class Quirky {
    ;
}

Javac will happily generate bytecode for both files. More single-character strangeness: a single $ or _ are valid Java identifiers. Hence, with a wanton willingness to abuse the language,

// in $.java
class $ {
    class $$ {
        $ $($ $) { return null; }
    }
}
class _ {
    _ _(_ _) { return null; }
}

can be compiled into $.class, $$$$.class and _.class! Finally the array brackets can appear in unexpected places as

// in Quirky.java
class Quirky {
    int[] twoDim() [] { return null; }
}

generates a method twoDim that returns a two-dimensional array int[][].

Next time chez Eric, I'm planning on releasing (via GitHub) my XNA sprite library that I used to develop Fishing Girl.

Comments

Karan Bhangui 22 Feb 09

Nice post. Here's a simple java puzzle for you:

public class RandomFun
{

    public static void main(String[] args)
    {
        http://google.com
    }
}

Without trying, will the follow code run, and why?

ps - I've been subscribed to your blog for a few months now, first year EE at UW :)

Eric 23 Feb 09

@Karan I'm happy to see that I have one devoted reader of my blog! In fact, I recognize your name from your comment on reddit when my OS appeared there.

As for the puzzle, your version will not actually compile. Javac will interpret it as a label ("http:") followed by a comment ("//google.com"), but a label without an subsequent statement is invalid. Adding an empty for loop ("for (;;) ;") would cause the code to compile and leave the illusion intact.

Peter 13 Mar 09

Pimping the third group member's website: http://www.student.cs.uwaterloo.ca/~i3stewar/

Eric 16 Mar 09

Thanks for the heads-up Peter! I've linked Ian's site in the post.

Post a comment