I can see below code compiles in Java but could not understand what could be the reason?
public class test {
@SuppressWarnings("unused")
public static void main(String[] args) {
abc:System.out.println(9);
}
}
The line in your main is a labelled statement.
These labels aren't much use in isolation, but they can be used with break and continue statements.
Well... Because it's a valid Java code.
If you ask about abc:System.out.println(9); the abc is just a label for a label statement which is legal in Java however if you have to use it, it means, that your code went wrong.
They are most often used for labeling the nested loops:
abc: for (int i = 0; i < 5; i++) {
for (int j = i; j < 10; j ++) {
if (j == 4) {
break abc; //breaks the outer loop.
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With