New Adventures in Software

A Java Syntax Quirk

This little trick is shamelessly stolen from Daniele Futtorovic’s post on comp.lang.java.programmer.

This is legal, compilable Java:

public class Oddity
{
    public static void main(String[] args)
    {
        http://blog.uncommons.org
        System.out.println("Why is the URL allowed above?");
    }
}

Why doesn’t the URL being in there upset the compiler?  If you’re not sure why it’s valid, click “show” for a spoiler.

show

34 Responses to “A Java Syntax Quirk”

  1. ajlopez says:

    A label, plus a comment!

    Very funny, Thanks!

  2. Casper Bang says:

    I’ve seen it somewhere before, isn’t it in the puzzler book?

  3. Cool :) Are you trying to get popular on digg? :) lol

  4. Ronald Iwema says:

    Joshua Bloch and Neal Gafter wrote about this in their book: Java Puzzlers. I don’t really think it’s a quirk in the syntax, just a creative way to combine things.

  5. Dan says:

    Casper/Ronald, you’re right, it’s puzzle 22. I have that book but I didn’t remember it when I saw Daniele’s post on CLJP.

  6. Patrick says:

    Very nice… and funny at the same time…

    I guess this is not just a Java quirk afterall… isn’t it valid for most C based languages? Appears so to be so. ;)

  7. Ben says:

    Nice one. Would be easier to spot with syntax highlighting! :)

  8. Dan says:

    Patrick, you’re probably right. Single-line // comments aren’t strictly valid in C (they are a C++ innovation), but most compilers would probably accept them.

  9. Praveen says:

    That’s really nice trick!

    I was curious if all compilers out there does silently leave this.. It turns out that C# compiler issues a warning that “the label is not used”. :D

  10. Subu says:

    Is there a better way to read comp.lang.java.programmer. instead of going through google groups?

  11. Un problema de sintaxis en Java…

    Este texto ha sido "robado" sin vergüenza, del post A Java Syntax Quirk de Dan Dyer que a su…

  12. You don't need my name says:

    Single-line comments are in C99 IIRC.

  13. Dan says:

    Subu, there are other web interfaces to USENET, though I can’t remember any at the moment.

    Otherwise you’ll need a proper news reader. I just use the one built-in to Opera since that’s the browser that I use. You can use Thunderbird or Outlook Express or any of several others.

  14. [...] New Adventures in Software » A Java Syntax Quirk (tags: java puzzle syntax humor programming) [...]

  15. Alexander says:

    It’s valid in c#. I get a compile error that the label is not in use.

  16. It’s also legal Javascript !

    The syntax hilight really makes it look strange, though. Looks buggy, but isn’t :)

  17. atc says:

    Worked it out in seconds, but it was very interesting to see. Never thought of it myself.

    Nice one :)

  18. throwaway says:

    // comments are valid in C99, the latest C revision.

  19. Kai says:

    Hahaha.. great.. famous..

  20. PENIX says:

    And if you ever use this in practice, you should be fired.

  21. SteveB says:

    You can only do it once though. This doesn’t work

    main(…) {

    http://somesite.com
    http://someothersite.com

    }

  22. szanne says:

    That is cool. Thanks!

  23. slavus says:

    URL being there upset the compiler . Iit ain’t valid, unless you are referencing this label somwhere in your code

  24. Oh, it’s old like the world :) What about this ?

    class String
    {
    public static void main(String args[])
    {
    String String = new String();
    }
    }

  25. Vijay says:

    Nice one. I would treat this as a trick(?) created by combining two java features. :)

    Keep posting such tricks(?)

  26. [...] Idea originally from here. [...]

  27. Rauno says:

    Am I missing something? What could you use this quirk for? Forgive me, I’m a frontend developer.

  28. Dan says:

    Rauno, I don’t think you should use it anywhere :)

    It’s just an interesting corner case.