Page 1 of 2

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sat Oct 13, 2012 1:58 pm
by Prachi
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

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sat Oct 13, 2012 4:52 pm
by admin
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);

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Tue Jan 27, 2015 7:00 am
by gparLondon
Why is that compiler will not complain about unreachable code?

System.out.println(j);//for this line

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Tue Jan 27, 2015 10:35 am
by admin
Because compiler doesn't know that it is unreachable. Compiler doesn't compute values of variables. JVM does.

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Fri Nov 06, 2015 4:00 pm
by Roibeard
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

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Fri Nov 06, 2015 8:00 pm
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sun Nov 08, 2015 6:23 am
by Roibeard
Ok, that's actually quite helpful information. Thank you.

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Mon Jul 04, 2016 12:53 am
by Sai Divya sree

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..

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Mon Jul 04, 2016 1:25 am
by admin
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

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sat Oct 15, 2016 1:45 pm
by 6EczPZZNmQ3p
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");
}

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sat Oct 15, 2016 8:46 pm
by admin
Yes, sorry, I meant x==0 is true.

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sat Mar 10, 2018 2:37 am
by Meghana
I tried to run it by adding package and main method like this:

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 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?

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sat Mar 10, 2018 3:57 am
by admin
Please post exact error message that you get. Screenshot would be even better.

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sat Mar 10, 2018 9:02 am
by Meghana
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)

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sat Mar 10, 2018 10:03 pm
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sun Mar 11, 2018 12:09 am
by Meghana
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! :)

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sun Mar 11, 2018 1:15 am
by admin
Well, from the code that you've posted the main method is empty. I don't see loopTest method being called. So no output.

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Tue Mar 13, 2018 2:57 am
by Meghana
Oh right. Thank you.. :)

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Tue Nov 27, 2018 7:56 pm
by OCAJO1
Why is that the compiler does not complain about the System.out.println(j); being unreachable?

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Tue Nov 27, 2018 9:44 pm
by admin
OCAJO1 wrote:
Tue Nov 27, 2018 7:56 pm
Why is that the compiler does not complain about the System.out.println(j); being unreachable?
Please go through this thread from the beginning. It is explained in detail.

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Fri Dec 21, 2018 4:03 am
by juliusacar
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.

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Fri Dec 21, 2018 8:05 am
by admin
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

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Fri Jul 05, 2019 5:03 pm
by vilasa
admin wrote:
Mon Jul 04, 2016 1:25 am
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
So I tried the code by making x final. It still doesn't give unreachable code error.Below is my code

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();
}

}

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Fri Jul 05, 2019 8:58 pm
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.1280 :

Posted: Sat Jul 06, 2019 5:03 pm
by vilasa
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?