This question already has answers here:
Labeled Statement block in Java?
(4 answers)
Closed 8 years ago.
I have never come across such expression in Java. It is not even a switch case
//no code above to make it look like a switch case or loop
abc: {
// do some stuff
break abc;
}
Do you have any idea what this does?
They are labels, see http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html for a complete explanation.
abc:
is a label and {} introduces a new scope for that block.
This is likely a label referring to the code enclosed by the block. This allows you to transfer the flow of the program with more control as opposed to something like breaking out of a while loop.
Related
This question already has answers here:
Labeled Statement block in Java?
(4 answers)
Closed 5 years ago.
some time ago I randomly saw (in a decompiled Java code) something like this:
syntax: {
//some code
}
What does this do? And is there more stuff like this?
I was not able to find anything about this.
Greetings
I think this is called a label. And can be used with loops. See also using labels in java without "loops"
Apart from labels if you remove : from start
{ } can be used as init block which is used to write code that will execute as first line in constructor.
This question already has answers here:
Is there a way to access an iteration-counter in Java's for-each loop?
(16 answers)
Closed 5 years ago.
Can we know the count of the iteration (that is whether it's the first, second, third ... iteration ) while we are stepping over the code in a for each loop, in the eclipse debugger? Eclipse must be holding a count but how to get it show it to us. Apart from the workarounds of introducing an index in the code can we utilize something in built in eclipse.
You can initiate a counter and increment it in the for loop, it should be a workaround.
You can track the variable under Eclipse console below:
This question already has answers here:
"loop:" in Java code. What is this, and why does it compile?
(12 answers)
Closed 7 years ago.
In this code:
search :
for (int i = 1; i <= 30; i++) {
System.out.println(i);
if (i == 20) {
break search;
}
}
What is search and how does one use it?
And wouldn't this code do the same without it?
search: is a label. You use labels with loops (break and continue) to indicate which loop is to be exited or continued.
Tutorial: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
In this case, there's only one loop. So yes I think the program would work the same without the label. However, occasionally I've seen labels used as a kind of comment to indicate what exactly is being done, and I guess that's what the coder here has done
This question already has answers here:
Java logical operator short-circuiting
(10 answers)
Closed 8 years ago.
For instance if I have an if statement as follows:
if(returnsFalse() && timeConsumingFunction()){
//do whatever
}
Will the program run the time consuming function or will it realise that the if evaluates as false after the "returnsFalse()" function returns its value?
How does this work in different languages? Mainly interested in java and c.
No if you use && it will not continue on if the first statement is false.(Java) If you use & it will evaluate all expressions.
This question already has answers here:
"loop:" in Java code. What is this, and why does it compile?
(12 answers)
Closed 9 years ago.
I have the job of going through another's code and I'm trying to figure it all out when I came across this for loop.
//I don't understand the purpose of assetLoop
assetLoop: for (AssetObject asset : assets) {
//Some code
}
I've never seen this syntax and I can't find any reference to it anywhere through my google searches. Can anyone tell me what assetLoop: is doing? Or simply give me the name of this concept so I can do some non-mindless googling and read about it? :)
This is called a label.
It allows you to write break assetLoop from a nested loop to break out of the outer loop.
It's essentially a limited form of goto, and is rarely used.
It's a label. You can put them on any statement. break assetLoop; will break out of that loop, even if the break statement is within another for, while, do-while or switch statement. Similarly continure assetLoop; will jump to the next iteration of the loop.