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.
Related
This question already has answers here:
Ways to iterate over a list in Java
(13 answers)
Closed 6 years ago.
The list declaration:
private List<SharedData> pairList = new ArrayList<>();
and somewhere in the code I'm trying to run a for loop on this List:
for(pairList.iterator().hasNext()) {
do something;
}
but, I receive an error:
Multiple markers at this line
- Syntax error on tokens, EnhancedForStatementHeaderInit expected
instead
- Syntax error, insert "; ; ) Statement" to complete ForStatement"
That's not so clear for me. Why do I need to add ";;"? I'm also not sure it will run correctly.
You are looking for the while loop - not for.
You should use while loop not for loop because hasNext()returns a boolean.
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 conditional compilation: how to prevent code chunks from being compiled?
(9 answers)
Conditional Java compilation
(11 answers)
Closed 7 years ago.
I am writing a cross platform game in IntelliJ IDEA using Java and I have run into a situation where I can't seem to find this feature which Visual Studio had:
This feature allowed me to have a condition, (2 lines of code), for example #if keyword1/#endif, and the code between those 2 lines, compiled only when the current project had keyword1 declared as compilation symbol through the project settings.
Is there any similar feature in IntelliJ IDEA ?
This is a preprocessor symbol, nothing IDE-dependant. There are some workarounds for this in java. For example you could use something like this:
public static final boolean DEBUG = false;
public void someMethod(){
if(DEBUG)
dosomething();
else
dosomethingElse();
}
Most precompilers will optimize this such that the result won't contain the if-else statement and reduce this to a simple call to dosomethingElse. But there's no preprocessor/precompiler statement like in c++ for java.
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:
"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.