Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can this Java code compile?

Tags:

java

puzzle

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.

like image 354
Dave Webb Avatar asked May 27 '09 16:05

Dave Webb


4 Answers

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

like image 136
Carl Manaster Avatar answered Oct 05 '22 12:10

Carl Manaster


You have a label

http:

followed by a comment

//www.google.com
like image 38
Brian Agnew Avatar answered Oct 05 '22 12:10

Brian Agnew


Easy. The highlighting on this site shows why.

http: is a label, as in break http;

//www.google.com is a comment.

like image 37
uncleO Avatar answered Oct 05 '22 12:10

uncleO


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

like image 33
Rohit Avatar answered Oct 05 '22 12:10

Rohit