About Question enthuware.ocajp.i.v7.2.1010 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1010 :
Sun being lazy here
Why String label allowed but not for?? just curious!!
Why String label allowed but not for?? just curious!!
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1010 :
Because String is not a keyword. for is a keyword.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Wed Dec 04, 2013 7:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1010 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1010 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Wed Nov 11, 2015 11:59 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1010 :
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?)
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?)
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1010 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Thu Mar 30, 2017 7:15 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1010 :
How come Hello is printed Twice
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1010 :
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.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Thu Mar 30, 2017 7:15 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1010 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1010 :
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.
In the third iteration of outer for loop, break String; will get to execute and both the loops will be terminated.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Google [Bot] and 108 guests