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

    thisIsALabel://And this is a comment.
    System.out.println("So it's all fine.");
Want more articles like this? Subscribe to the feed.

34 Responses to 'A Java Syntax Quirk'

Subscribe to comments with RSS

ajlopez said,

on August 24th, 2008 at 11:52 pm

A label, plus a comment!

Very funny, Thanks!


on August 25th, 2008 at 2:06 am

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

Roger said,

on August 25th, 2008 at 2:35 am

Nice :)


on August 25th, 2008 at 5:19 am

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

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.

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.


on August 25th, 2008 at 1:32 pm

Very nice!

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

Ben said,

on August 25th, 2008 at 10:18 pm

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

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.

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

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?


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…

You don't need my name said,

on August 26th, 2008 at 11:44 am

Single-line comments are in C99 IIRC.


on August 26th, 2008 at 12:41 pm

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

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.


on August 26th, 2008 at 3:01 pm

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

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.


on August 26th, 2008 at 7:58 pm

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


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 :)

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 :)

throwaway said,

on August 26th, 2008 at 11:09 pm

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

Kai said,

on August 26th, 2008 at 11:11 pm

Hahaha.. great.. famous..

PENIX said,

on August 27th, 2008 at 1:20 am

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

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

}

szanne said,

on August 27th, 2008 at 3:11 am

That is cool. Thanks!

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


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();
}
}


on August 27th, 2008 at 12:39 pm

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

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(?)


on September 8th, 2008 at 5:45 pm

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

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.

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.


on October 6th, 2008 at 12:23 am

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



You are viewing a mobilized version of this site...
View original page here

How do you rate mobile version of this page?

Mobilized by Mowser Mowser