这是我遇到的一段有趣的代码:它看起来很奇怪但它确实编译了.
public static void main(String[] args) {
http://servername:port/index.html
System.out.println("Hello strange world");
}
Run Code Online (Sandbox Code Playgroud)
思考?
http:是一个标签.其余的只是内联评论//.有人可以问,为什么我们在Java中有标签?
首先想到:goto.但是,goto实现已从Java中删除,因为不必要.唯一保留的是保留关键字goto.在这里阅读更多.
那么,标签的用途是什么?
标签可以与break声明一起使用.带有标签的文档示例search:
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)