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.
Related
This question already has answers here:
Java: Not a statement
(2 answers)
Closed 2 years ago.
I want to know why the following is invalid in Java. Java compiler says that it is not a valid statement.
1+1;
I know the following works.
int i = 1+1;
Please explain why the second one is valid while the first is not. Thanks in advance.
Because you are doing nothing with 1+1. That is not a statement, it's an expression that returns a value that should be stored somewhere, like in the second example you give. If your statements have no effect, they are excluded from the language grammar.
The Java syntax needs the variable to be declared like the following
Class name = value;
You can't create a value without a variable definition and can't create a variable without a name and a class.
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:
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.
This question already has answers here:
Converting Boolean to Integer in Java without If-Statements
(12 answers)
Closed 8 years ago.
I'm fairly new to Java and I'm writing an android game - and there's one line of code in a tight loop that's really annoying me.
targetBlocksRemain += letterArray[column][row].isTargetBlock() ? 1 : 0;
I feel like must be possible to write this without the conditional statement and therefore without the potential branch prediction performance hit.
I'll optimise out the tight loop eventually so I'm not massively concerned in the long run, but it'd be nice to know if there's a way to resolve similar situations.
edit
Just to clarify - this is a pseudo theoretical questions - there are lots of other things I could do to improve this, such as store isTargetBlock as an int. I'm not really interested in other suggestions, just wondering if there's a way do resolve this particular issue.
edit 2
Hoping to look at what the Dalvik VM is doing shortly to work out how it handles this.
I know it might not look good but will avoid conditional statement:
do this once in the activity:
HashMap<Boolean, Integer> map=new HashMap<Boolean, Integer>();
map.put(false, 0);
map.put(true, 1);
and in loop
targetBlocksRemain += map.get(letterArray[column][row].isTargetBlock());