New Adventures in Software


A Java Syntax Quirk

Posted in Java by Dan on August 24th, 2008


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

Want more articles like this? Subscribe to the feed.

34 Responses to 'A Java Syntax Quirk'

Subscribe to comments with RSS

  1. ajlopez said,

    on August 24th, 2008 at 11:52 pm

    A label, plus a comment!

    Very funny, Thanks!

  2. Casper Bang said,

    on August 25th, 2008 at 2:06 am

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

  3. Roger said,

    on August 25th, 2008 at 2:35 am

    Nice :)


  4. on August 25th, 2008 at 5:19 am

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

  5. Ronald Iwema said,

    on August 25th, 2008 at 8:12 am

    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.

  6. Dan said,

    on August 25th, 2008 at 12:37 pm

    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.


  7. on August 25th, 2008 at 1:32 pm

    Very nice!

  8. Patrick said,

    on August 25th, 2008 at 8:38 pm

    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. ;)

  9. Ben said,

    on August 25th, 2008 at 10:18 pm

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

  10. Dan said,

    on August 25th, 2008 at 10:29 pm

    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.

  11. Praveen said,

    on August 26th, 2008 at 5:26 am

    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

  12. Subu said,

    on August 26th, 2008 at 5:51 am

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


  13. on August 26th, 2008 at 11:08 am

    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…

  14. You don't need my name said,

    on August 26th, 2008 at 11:44 am

    Single-line comments are in C99 IIRC.


  15. on August 26th, 2008 at 12:41 pm

    [...] A Java Syntax Quirk [...]

  16. Dan said,

    on August 26th, 2008 at 12:54 pm

    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.


  17. on August 26th, 2008 at 3:01 pm

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

  18. Alexander said,

    on August 26th, 2008 at 7:56 pm

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


  19. on August 26th, 2008 at 7:58 pm

    [...] A Java Syntax Quirk [...]


  20. on August 26th, 2008 at 10:16 pm

    It’s also legal Javascript !

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

  21. atc said,

    on August 26th, 2008 at 10:22 pm

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

    Nice one :)

  22. throwaway said,

    on August 26th, 2008 at 11:09 pm

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

  23. Kai said,

    on August 26th, 2008 at 11:11 pm

    Hahaha.. great.. famous..

  24. PENIX said,

    on August 27th, 2008 at 1:20 am

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

  25. SteveB said,

    on August 27th, 2008 at 3:08 am

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

    main(…) {

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

    }

  26. szanne said,

    on August 27th, 2008 at 3:11 am

    That is cool. Thanks!

  27. slavus said,

    on August 27th, 2008 at 8:27 am

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


  28. on August 27th, 2008 at 10:03 am

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

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


  29. on August 27th, 2008 at 12:39 pm

    [...] A Java Syntax Quirk [...]

  30. Vijay said,

    on August 28th, 2008 at 7:59 am

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

    Keep posting such tricks(?)


  31. on September 8th, 2008 at 5:45 pm

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

  32. Rauno said,

    on September 9th, 2008 at 9:49 am

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

  33. Dan said,

    on September 9th, 2008 at 12:18 pm

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

    It’s just an interesting corner case.


  34. on October 6th, 2008 at 12:23 am

    [...] source: New Adventures in Software [...]