Page 1 of 2

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

Posted: Wed Sep 26, 2012 12:27 pm
by Michailangelo
Shouldn't this print 5?

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

Posted: Wed Sep 26, 2012 12:29 pm
by Michailangelo
I see now. The while is a never ending loop.

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

Posted: Tue Jan 29, 2013 2:08 pm
by akassem
I understand that the while loop is an infinite loop. But since it calls getX() each time, shouldn't this lead to a StackOverflowError eventually?

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

Posted: Tue Jan 29, 2013 2:38 pm
by admin
akassem wrote:I understand that the while loop is an infinite loop. But since it calls getX() each time, shouldn't this lead to a StackOverflowError eventually?
No, stack overflow occurs in recursion. i.e. when a method calls itself. That is not happening here.

HTH,
Paul.

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

Posted: Wed Mar 05, 2014 7:19 pm
by crux terminatus
To check my understanding:

getX() will return 5 each time the condition in the while loop is checked. So, the condition 5 != 0 will be checked each time the while loop is executed, and will always be false - thus the infinite loop.

If int x = 0 is removed, x will eventually become 0, upon which the while loop terminates.

Correct?

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

Posted: Wed Mar 05, 2014 8:52 pm
by admin
Yes, that is correct.

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

Posted: Sat May 24, 2014 10:48 am
by UmairAhmed
Hi,

Can you please illustrate what actually the local variable 'x' in method looper() do and its effect on while loop. Why while loop just stops if we remove int x = 0 from the lopper method(). I guess it is something related with shadowing of instance variable with local variable but couldn't get this point.

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

Posted: Sat May 24, 2014 8:50 pm
by admin
Did you read the explanation? It explains exactly what you are asking.
Note that looper() declares an automatic variable x, which shadows the instance variable x. So when x = m; is executed, it is the local variable x that is changed not the instance field x. So getX() never returns 0. If you remove int x = 0; from looper(), it will print 0 and end.
If you remove the automatic variable x, then there will be no shadowing of the instance field x and x = m will affect the instance field x.

Also, please read the post by "crux terminatus" above.

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

Posted: Sun Jun 19, 2016 4:40 am
by wuqing1450
admin wrote:Did you read the explanation? It explains exactly what you are asking.
Note that looper() declares an automatic variable x, which shadows the instance variable x. So when x = m; is executed, it is the local variable x that is changed not the instance field x. So getX() never returns 0. If you remove int x = 0; from looper(), it will print 0 and end.
If you remove the automatic variable x, then there will be no shadowing of the instance field x and x = m will affect the instance field x.

Also, please read the post by "crux terminatus" above.
If local x to be changed to this.x, the code would print 0. Correct?

Code: Select all

public class TestClass{
        int x = 5;
        int getX(){ return x; }

        public static void main(String args[]) throws Exception{
            TestClass tc = new TestClass();
            tc.looper();
            System.out.println(tc.x);
        }
        public void looper(){
            int x = 0;
            while( (this.x = getX()) != 0 ){
                for(int m = 10; m>=0; m--){
                    [b]this.[/b]x = m;
                }
            }
       }     
}

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

Posted: Sun Jun 19, 2016 10:04 am
by admin
What happened when you tried it out?

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

Posted: Thu Jun 23, 2016 9:13 am
by wuqing1450
admin wrote:What happened when you tried it out?
It prints 0 :)

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

Posted: Sun Jun 26, 2016 12:38 pm
by Sai Divya sree
What will the following code print?

Code: Select all

public class TestClass{
        int x = 5;
        int getX(){ return x; }

        public static void main(String args[]) throws Exception{
            TestClass tc = new TestClass();
            tc.looper();
            System.out.println(tc.x);
        }
        
        public void looper(){
            int x = 0;
            while( (x = getX()) != 0 ){
                for(int m = 10; m>=0; m--){
                    x = m;
                }
            }
            
       }     
}

Hi,
i couldn't understand how if int x=0 is removed from looper(); method would result in printing 0 and end?Though i have seen the earlier posts i couldnt understand the replies.Please if anyone could help me.I am little new to JAVA.Thank you!

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

Posted: Sun Jun 26, 2016 1:53 pm
by admin
If you remove int x=0 from looper, there will be only one variable named x in the whole code and that is the instance variable x. This x will be modified in look by the statement x = m; At the end of the loop, x will be assigned 0.

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

Posted: Thu Jun 30, 2016 5:06 pm
by Sai Divya sree
Wow!Thank you .I understood it very well!

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

Posted: Fri Aug 12, 2016 9:03 pm
by sherryyuan
I just wonder how this code will compile since x is an instance variable in the class. and the loop declare it with the same name? Is it work?

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

Posted: Fri Aug 12, 2016 9:24 pm
by admin
What happened when you tried it out?

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

Posted: Fri Mar 17, 2017 1:31 pm
by seemavishwakarma
Why this code compiles and do not complain about unreachable System.out.println statement due to infinite loop.

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

Posted: Fri Mar 17, 2017 9:49 pm
by admin
Compiler does not execute code. It does not know the value of the variables (unless they are compile time constants). So it does not know that it is an infinite loop.

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

Posted: Tue Mar 28, 2017 1:43 pm
by seemavishwakarma
Thanks a lot Enthuware. Cleared my certification 1Z0-803 :joy: :thumbup: . Tests helped me a lot. definitely a must have before actual exams!!!

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

Posted: Sat Sep 23, 2017 1:15 pm
by Javier
Hi!!

I would like to know if an infinite loop generate some kind of exception or error, is there no problem with the memory or the hard disk?

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

Posted: Sat Sep 23, 2017 8:27 pm
by admin
Javier wrote:Hi!!

I would like to know if an infinite loop generate some kind of exception or error, is there no problem with the memory or the hard disk?
A simple for loop that does not consume any memory can run forever -
for(;;);

If the loop consumes memory, of course, it will eventually cause OutOfMemoryError or StackOverflowError (if there is recursion involved).

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

Posted: Sun Sep 24, 2017 1:28 pm
by Javier
Thank you so much Admin for your answer!!
Enthuware is great, I learnt a lot of with your explanations, I feel very confident to try to pass the OCA certificate.
Thank you guys!!

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

Posted: Wed May 02, 2018 5:48 am
by SeoaneR
Hi There
I'm finding the !=(Not equals) quite confusing .
The interpretation of the != operator is that , if values are not equal then the condition evaluates to true,
or evaluating to true if the values are different.
The while loop has the following condition : while(x = getX() !=0) .This implies 5 !=0 .So if I use the definition
of the != that says if values are not equal the condition evaluates to true ...why does the while loop evaluate
to false.

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

Posted: Wed May 02, 2018 12:00 pm
by admin
You understanding is correct. while loop does not evaluate to false. It evaluates to true. Why do you think it evaluates to false here?

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

Posted: Tue Dec 11, 2018 2:35 pm
by OCAJO1
So the fact that the correct answer is that it will run forever and not throw an exception (OutOfMemeoryException) at run time, mean that neither the for loop nor the assignment within it (x=m) consume any memory?