자바 코드에서 "loop :". 이것은 무엇이며 왜 컴파일됩니까?
이 코드는 몇 분 동안 내 화면을 응시하도록 만들었습니다.
loop:
for (;;) {
// ...
}
( 여기 137 행 )
나는 이것을 전에 본 적이 없으며 Java에 "루프"키워드가 있다는 것을 전혀 몰랐으며 (NetBeans는 키워드처럼 색상을 지정하지도 않음) JDK 6으로 잘 컴파일됩니다.
설명은 무엇입니까?
그것은 아니다 keyword
그것이이다 label
.
용법:
label1:
for (; ; ) {
label2:
for (; ; ) {
if (condition1) {
// break outer loop
break label1;
}
if (condition2) {
// break inner loop
break label2;
}
if (condition3) {
// break inner loop
break;
}
}
}
문서 .
다른 포스터가 말했듯이 키워드가 아니라 레이블입니다. 레이블을 사용하면 다음과 같은 작업을 수행 할 수 있습니다.
outer: for(;;) {
inner: for(;;) {
break outer;
}
}
이것은 외부 루프를 끊을 수 있습니다.
문서 링크 .
질문에 대한 답이 있지만 참고로 다음과 같습니다.
"이 Java 코드가 유효한 이유는 무엇입니까?"라는 인터뷰 질문에 대해 들어 본 적이 있습니다. (더 간단한 예를 제거했습니다. 여기에 더 비열한 것, Tim Büthe가 있습니다) :
url: http://www.myserver.com/myfile.mp3
downLoad(url);
Would you all know what this code is (apart from awful)?
Solution: two labels, url
and http
, a comment www.myserver.com/myfile.mp3
and a method call with a parameter that has the same name (url
) as the label. Yup, this compiles (if you define the method call and the local variable elsewhere).
That's not a keyword, it's a label. It's meant to be used with the break
and continue
keywords inside nested loops:
outer:
for(;;){
inner:
for(;;){
if(){
break inner; // ends inner loop
} else {
break outer; // ends outer loop
}
}
}
It's not a keyword; it's a label.
It allows you to go a labeled break
and labeled continue
.
This is really a reply to seanizer's comment on org.life.java's answer, but I wanted to put in some code so I couldn't use the comment feature.
While it is very rare that I find a use for "break label", it does happen occassionally. The most common case is when I am searching for something that is in a structure requiring a nested loop to search, like:
search:
for (State state : stateList)
{
for (City city : state.cityList)
{
if (city.zipcode.equals(wantZip))
{
doSomethingTo(city);
break search;
}
}
}
Usually in such cases I push the whole thing into a subroutine so that on a hit I can return the found object, and if it falls out the bottom of the loop I can return null to indicate a not found, or maybe throw an exception. But this is occasionally useful.
Frankly, I think the inventors of Java included this feature because between this and exception handling, they eliminated the last two legitimate uses for GOTO.
Very late addendum:
I saw a great gag line of code once. The programmer wrote:
http://www.example.com/xyz.jsp
for (Foo foo1 : foolist)
He didn't actually say "example.com" but our company's web site.
It gives the impression that there's a URL in the code. It compiles successfully, like it does something. But ... what does it do?
In reality it does nothing. "http:" is a label that he never references. Then the "//" makes the rest of the line a comment.
It's a break point label, to allow you to break out of a specified loop, rather than simply the innermost one you happen to be in.
It's used on line 148.
You could write almost anything, as it is a label... You have an example here
It's a label, though look at the following example:
int a = 0;
int b = 0
while (a<10){
firstLoop:
a++;
while(true){
b++
if(b>10){
break firstLoop;
}
}
}
When b>10
the execution flow goes to the outer loop.
It is a label, and labels in Java can be used with the break
and continue
key words for additional control over loops.
Here it is explained in a rather good way:
Thinking in Java, break and continue
It is not a keyword, but a label. If inside the for
loop you write break loop;
, and you exit that loop.
It is a label. Generally a label used in Java to transfer the control flow at desired location while all keywords, like continue and break, have a specified choice of location.
참고URL : https://stackoverflow.com/questions/3821827/loop-in-java-code-what-is-this-and-why-does-it-compile
'program story' 카테고리의 다른 글
Android : 새 버전의 gradle로 업데이트 한 후 "Manifest merger failed"오류가 발생 함 (0) | 2020.08.08 |
---|---|
문자열 끝에서 쉼표를 제거하려면 어떻게합니까? (0) | 2020.08.08 |
R을 사용하여 데이터 프레임 행을 조건부로 제거 (0) | 2020.08.08 |
Java에서 별도의 스레드로 메서드를 호출하는 방법은 무엇입니까? (0) | 2020.08.08 |
목록 확인 방법 (0) | 2020.08.08 |