tags:

views:

316

answers:

7

A colleague came across some code that looked like this and couldn't understand how it could ever compile:

class FooClass {
  public static void bar(String arg) {
     System.out.println("arg = " + arg);
     http://www.google.com
     System.out.println("Done!");
  }
}

Basically, there was a random URL pasted in the middle of a method but javac didn't care.

We worked out so I'll post the answer if no-one else finds out but I thought it was interesting enough to post.

+16  A: 

"http:" is interpreted as a label. What follows is an end-of-line comment.

Carl Manaster
break http; is just a secret identity of a GOTO statement they keep saying is EVIL and will not make a part of the language. I had no idea there were labels in Java... although I don't mean to say I consider GOTO and labels as evil as many others...
Peter Perháč
+1  A: 

Easy. The highlighting on this site shows why.

http: is a label, as in break http;

//www.google.com is a comment.

UncleO
+1  A: 

http: is the label. // starts the comment.

Rohit
+3  A: 

You have a label

http:

followed by a comment

//www.google.com
Brian Agnew
+1  A: 

"http:" is a label, and the part after the "//" is, of course, a comment

mandaleeka
+4  A: 

Because it's GOOGLE man!

GreenieMeanie
A: 

Another example with two http://

public class Main {
    {
        http://en.wikipedia.org/wiki/Hello_world_program
        System.out.print("Hello ");
    } {
        http://java.sun.com/docs/books/tutorial/getStarted/application/index.html
        System.out.println("World!");
    }

    public static void main(String... args) {
        new Main();
    }
}
Peter Lawrey