Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this code compiles in java? [duplicate]

Tags:

java

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);
    }
}
like image 286
pd30 Avatar asked Jul 12 '26 11:07

pd30


2 Answers

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.

like image 169
Dawood ibn Kareem Avatar answered Jul 15 '26 01:07

Dawood ibn Kareem


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.
             }
         }
     }
like image 36
xenteros Avatar answered Jul 14 '26 23:07

xenteros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!