Here is an interesting piece of code I came across: It looks quite strange but it does compile.
public static void main(String[] args) {
http://servername:port/index.html
System.out.println("Hello strange world");
}
Thoughts?
Here is an interesting piece of code I came across: It looks quite strange but it does compile.
public static void main(String[] args) {
http://servername:port/index.html
System.out.println("Hello strange world");
}
Thoughts?
http:
is a label. The rest is just an inline comment //
.One can ask, why do we have labels in Java?
First thought: goto
. But, goto
implementation has been removed from Java as needless. The only thing which stayed is a reserved keyword goto
. Read more here.
So, what's the usage of labels?
Labels can be used with break
statement. Example from documentation, with search
label:
class BreakWithLabelDemo {
public static void main(String[] args) {
int[][] arrayOfInts = {
{ 32, 87, 3, 589 },
{ 12, 1076, 2000, 8 },
{ 622, 127, 77, 955 }
};
int searchfor = 12;
int i;
int j = 0;
boolean foundIt = false;
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length; j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at " + i + ", " + j);
} else {
System.out.println(searchfor + " not in the array");
}
}
}
A label followed by an inline coment
Yes, Java has label
public static void main(String[] args) {
hello:
}
And of course online comments
public static void main(String[] args) {
int i; // this is a comment.
}