How to use labels in java code?

后端 未结 3 1699
小鲜肉
小鲜肉 2020-12-06 19:10

I am getting error while using break statements in labels in java code. This is showing undefined label. It is wrong to write code like this. Please assist me i

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 19:25

    A break with a label is not the same as a goto statement. Java does not have a goto statement.

    A label marks the statement that follows it. You can use it to break out of that statement, and only out of that statement. Control of flow will always transfer to the end of the labeled statement.

    So what do you have here?

            label149: if (!localIterator2.hasNext());
    

    Because of the semicolon after the if, this is in fact the entire labeled statement. So your break label149 is not within its scope at all. If you did not have that semicolon, the if block would include the while, and then the break label149 would work. But control would be transferred to the line after the end of the while block.

           label246: ((HashMap)Saved.this.keynamedata.get(i)).put("is_paid", "0");
    

    This is the statement marked by label246. Again, the break label246 is not inside it, so it is not in its scope, and you can`t break out of a statement you are not inside of.

提交回复
热议问题