Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1010 :
Posted: Thu Mar 07, 2013 12:12 pm
by ETS User
Sun being lazy here
Why String label allowed but not for?? just curious!!
Re: About Question enthuware.ocajp.i.v7.2.1010 :
Posted: Thu Mar 07, 2013 7:14 pm
by admin
Because String is not a keyword. for is a keyword.
Re: About Question enthuware.ocajp.i.v7.2.1010 :
Posted: Wed Jan 29, 2014 2:16 pm
by tn1408
Hi Paul,
It's correct if we replaced the break point "for" by "String", the program will print "hello" 2 times.
My question is why does the outer loop keep on going and the program ultimately prints "hello" 10 times. I don't see what's keeping it from continue looping. It loops only three times and exits without printing the 3rd time?
Thanks
Re: About Question enthuware.ocajp.i.v7.2.1010 :
Posted: Wed Jan 29, 2014 10:23 pm
by admin
That is because of the break. It is pointing to the outer for loop. So as soon as i+j is >10, break String will cause the outer for loop to break.
Re: About Question enthuware.ocajp.i.v7.2.1010 :
Posted: Sun Nov 22, 2015 1:46 pm
by prasanna_ls
I did execute the code that was given in the explanation for the question. Sort of mind blown! So... we can have the same identifier for the class name, variable name, method name aaannnd label name?!
How exactly does this work? For my understanding, I was imagining a separate identifier list for classes, methods and variables till now. Now it seems like I'll have to add the label list in my mind as well. How many of these "lists" are there? ("lists" might not be how they work but I try to imagine it in that way to help me understand stuff. I hope you get what I mean to say i.e. how many different things can have the same identifier?)
Re: About Question enthuware.ocajp.i.v7.2.1010 :
Posted: Sun Nov 22, 2015 8:52 pm
by admin
Only keywords have a special meaning for the compiler. That is why they cannot be used as variable names etc. The meaning of everything else depends on the context and so it is possible to use the same name for a class, a variable, and a label.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1010 :
Posted: Thu Mar 30, 2017 7:18 am
by Chandu
How come Hello is printed Twice

Re: About Question enthuware.ocajp.i.v7.2.1010 :
Posted: Thu Mar 30, 2017 9:30 am
by admin
Which code are you talking about? The one in the problem statement will not compile. The one in explanation will print hello twice. You will need to execute it step by step and work out the values of each variable at each step.
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1010 :
Posted: Thu Mar 30, 2017 10:43 am
by Chandu
class TestClass{
public static void main(String[] args){
String : for(int i = 0; i< 10; i++){
for (int j = 0; j< 10; j++){
if ( i+ j > 10 ) break String;
}
System.out.println( "hello");
}
}
}
// How hello is printed twice please explain
Re: About Question enthuware.ocajp.i.v7.2.1010 :
Posted: Thu Mar 30, 2017 10:49 am
by admin
When i is 0 or 1, i+j can never be > 10, so that prints a hello in first two iterations of outer for loop.
In the third iteration of outer for loop, break String; will get to execute and both the loops will be terminated.