About Question enthuware.ocajp.i.v7.2.1280 :
Moderator: admin
Re: About Question enthuware.ocajp.i.v7.2.1280 :
In this question, there is no 'public static void main(String[] args)' statement. How will the program then even run, and give an output? Can you please clarify on this what are we supposed to do in such a case?
Thanks
Thanks
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
In this case, the question doesn't say that it is executed from the command line. It just says, "what will be the output if 0 (integer value zero) is passed to loopTest()". So the absence main method is not an issue here.
The loopTest method can be executed from another class (which may in turn have a main method) using:
new TestClass().loopTest(0);
The loopTest method can be executed from another class (which may in turn have a main method) using:
new TestClass().loopTest(0);
If you like our products and services, please help us by posting your review here.
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Why is that compiler will not complain about unreachable code?
System.out.println(j);//for this line
System.out.println(j);//for this line
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Because compiler doesn't know that it is unreachable. Compiler doesn't compute values of variables. JVM does.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sun Jul 12, 2015 6:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Hi,
Is there a particular technique for articulating loops on paper? A best practice, or something like "UML" suitable for working out loops quickly and accurately by hand?
Thanks
Is there a particular technique for articulating loops on paper? A best practice, or something like "UML" suitable for working out loops quickly and accurately by hand?
Thanks
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Unfortunately there is no special technique other than to write down the value of each variable as you execute each iteration of the loop.
Sorry
Paul.
Sorry

Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Sun Jul 12, 2015 6:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Ok, that's actually quite helpful information. Thank you.
-
- Posts: 14
- Joined: Mon Jun 20, 2016 11:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Code: Select all
public class TestClass{
public void loopTest(int x){
loop: for (int i = 1; i < 5; i++){
for (int j = 1; j < 5; j++){
System.out.println(i);
if (x == 0) { continue loop; }
System.out.println(j);
}
}
}
}
In this program the statement System.out.println(j); is not reachable?so the code shouldn't compile?Am I wrong?I have read in the earlier reply that compiler doesn't know that its not reachable.I just dint get that.Please if you could help..
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Think about it this way. How do "you" know that it is unreachable? You know it because you know that x is 0 and x==0 will be false. You have executed the code in your mind and made that determination.
But the compiler doesn't know that x will be 0 at runtime. It cannot execute code. It can only make use of the information that it is 100% sure about at compile time. This means if you have a final variable, then the compiler can make use of that fact because it knows that its value will be the same at run time also. But a normal variable's value can be different.
You may go through this interesting discussion: http://stackoverflow.com/questions/3795 ... iler-error
But the compiler doesn't know that x will be 0 at runtime. It cannot execute code. It can only make use of the information that it is 100% sure about at compile time. This means if you have a final variable, then the compiler can make use of that fact because it knows that its value will be the same at run time also. But a normal variable's value can be different.
You may go through this interesting discussion: http://stackoverflow.com/questions/3795 ... iler-error
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sat Apr 23, 2016 3:44 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
You said "you know that x is 0 and x==0 will be false"? Isn't this a false statement, pun intended? Did you perhaps mean to say "x is 0 and x==0 will be true"?
Run the following code:
int x;
x = 0;
if(x == 0)
{
System.out.println("x == 0 evaluates to true");
}
else
{
System.out.println("x == 0 evaluates to false");
}
Run the following code:
int x;
x = 0;
if(x == 0)
{
System.out.println("x == 0 evaluates to true");
}
else
{
System.out.println("x == 0 evaluates to false");
}
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Yes, sorry, I meant x==0 is true.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
I tried to run it by adding package and main method like this:
I'm not getting any output. What is wrong with the code?
(I'm a newbie. Studied and then started with mocks but new to practical coding. Please help!)
(When I tried to include loopTest within the main method, there were other errors, like: loopTest cannot have void return type.) So, how could I change the code?
Code: Select all
package happeningPackage;
public class TestClass {
public static void main(String[] args) {}
public void loopTest(int x){
loop: for (int i = 1; i < 5; i++){
for (int j = 1; j < 5; j++){
System.out.println(i);
if (x == 0) { continue loop; }
System.out.println(j);
}
}}}
(I'm a newbie. Studied and then started with mocks but new to practical coding. Please help!)
(When I tried to include loopTest within the main method, there were other errors, like: loopTest cannot have void return type.) So, how could I change the code?
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Please post exact error message that you get. Screenshot would be even better.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Here is the snap when I ran it.
(Hope its not too difficult to read. Had to reduce resolution and compress it to keep it within the limit:256kb)
(Hope its not too difficult to read. Had to reduce resolution and compress it to keep it within the limit:256kb)
- Attachments
-
- Screen Shot 2018-03-10 at 7.23.08 PM.zip
- (249.13 KiB) Downloaded 392 times
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
I don't see any meaningful information in the Eclipse screen shot that you've posted.
As I mentioned to you before in another thread, you need to use the command line to compile and run the code. So please try compiling it on command line and post the exact error message that you get.
If you are a beginner, you should simply avoid using any IDE at this stage.
As I mentioned to you before in another thread, you need to use the command line to compile and run the code. So please try compiling it on command line and post the exact error message that you get.
If you are a beginner, you should simply avoid using any IDE at this stage.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Yeah. I'll do that, although I have managed to run many other programs on eclipse. I couldn't figure as to why this wasn't giving any output
Thank you, for all the guidance though!

Thank you, for all the guidance though!

-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Well, from the code that you've posted the main method is empty. I don't see loopTest method being called. So no output.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Oh right. Thank you.. 

-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Why is that the compiler does not complain about the System.out.println(j); being unreachable?
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Please go through this thread from the beginning. It is explained in detail.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Dec 21, 2018 4:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
When x is 0, the statement continue loop; is executed. Note that loop: is for the outer loop.
Can anyone explain this to me:
loop: for (int i = 1; i < 5; i++){
I'm used to simply seeing nested for loops.
But this one have "loop:" before it. How does it work? thank you.
Can anyone explain this to me:
loop: for (int i = 1; i < 5; i++){
I'm used to simply seeing nested for loops.
But this one have "loop:" before it. How does it work? thank you.
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
You can apply a label to any statement. Since a for statement is just like any other statement, you can apply a label to a for statement as well. That is exactly what loop: is in this case.
You can read more about labels here: https://howtodoinjava.com/java/basics/l ... s-in-java/
Labels are use mostly in conjunction with break and continue statements. You can read more about it here: https://www.journaldev.com/980/java-bre ... ment-label
You can read more about labels here: https://howtodoinjava.com/java/basics/l ... s-in-java/
Labels are use mostly in conjunction with break and continue statements. You can read more about it here: https://www.journaldev.com/980/java-bre ... ment-label
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Tue Apr 16, 2019 5:40 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
So I tried the code by making x final. It still doesn't give unreachable code error.Below is my codeadmin wrote: ↑Mon Jul 04, 2016 1:25 amThink about it this way. How do "you" know that it is unreachable? You know it because you know that x is 0 and x==0 will be false. You have executed the code in your mind and made that determination.
But the compiler doesn't know that x will be 0 at runtime. It cannot execute code. It can only make use of the information that it is 100% sure about at compile time. This means if you have a final variable, then the compiler can make use of that fact because it knows that its value will be the same at run time also. But a normal variable's value can be different.
You may go through this interesting discussion: http://stackoverflow.com/questions/3795 ... iler-error
public class TestClass6{
final int x=0;
public void loopTest(){
loop: for (int i = 1; i < 5; i++){
for (int j = 1; j < 5; j++){
System.out.println(i);
if (x == 0) { continue loop; }
System.out.println(j);
}
}
}
public static void main(String args[]){
new TestClass6().loopTest();
}
}
-
- Site Admin
- Posts: 9791
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Please go through the SOF link given in the post that you've quoted. if statement is an exception. Java language allows an unreachable if block to support conditional compilation.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Tue Apr 16, 2019 5:40 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1280 :
Thank you . Yeah now I remember from other post of your's.if statement is an exception. I understand now. So for infinite loop case if there is statement after the loop ,it is unreachable . But for if condition it is exception. Is my understanding right?
Who is online
Users browsing this forum: No registered users and 3 guests