Page 1 of 2

About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Tue Jun 02, 2015 8:40 am
by FrodoBaggins
When I put the code in question into my Eclipse compiler I get the following compilation errors:

Type mismatch: cannot convert from ArrayList to List

Code: Select all

import java.awt.List;
import java.util.ArrayList;

public class O21465 {
public static void main(String[] args) {
	List s1 = new ArrayList();
	try{while(true){s1.add("sdfa");}
	}catch(RuntimeException e){
e.printStackTrace();
}
	System.out.println(s1.size());
}
}
What is wrong?

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Tue Jun 02, 2015 10:18 am
by admin
We don't recommend using IDEs while studying for the exam. Please use command line and post the output from there.

thank you,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Tue Jun 02, 2015 11:56 am
by Ad9999
FrodoBaggins wrote:When I put the code in question into my Eclipse compiler I get the following compilation errors:

Type mismatch: cannot convert from ArrayList to List

Code: Select all

import java.awt.List;
import java.util.ArrayList;

public class O21465 {
public static void main(String[] args) {
	List s1 = new ArrayList();
	try{while(true){s1.add("sdfa");}
	}catch(RuntimeException e){
e.printStackTrace();
}
	System.out.println(s1.size());
}
}
What is wrong?
You are importing java.awt.List which is not the java.util.List collection/date structure. Also, infinite loop and out of memory Error. Not a run time exception.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Thu Jun 04, 2015 7:40 am
by FrodoBaggins
Thanks. Why not use the compiler? In a real work environment I would use it all the time.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Thu Jun 04, 2015 10:32 am
by admin
FrodoBaggins wrote:Thanks. Why not use the compiler? In a real work environment I would use it all the time.
You have to use a compiler. What you mean is IDE. You should not use an IDE like Eclipse or NetBeans while preparing for the exam. See Chris Barrett's posts here for why: http://www.coderanch.com/t/639884/ocajp ... iled-OCAJP

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Sun Nov 22, 2015 1:18 am
by prasanna_ls
I was expecting the System.out.println(s1.size()) to cause the compiler to complain about unreachable code. It IS an unreachable code isn't it? What's going on here?

(Yes, I did execute the code. And yes, there was no compilation errors. But it doesn't quite make sense to me.)

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Sun Nov 22, 2015 9:51 am
by admin
Why do you think it is unreachable code?
-Paul.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Sun Nov 22, 2015 1:35 pm
by prasanna_ls
Because there is an infinite loop i.e. while(true) and it doesn't contain any break statements in it. So, everything that comes after that is unreachable isn't it? So, I was expecting the code to not compile. That DOES seem to be the case for the statements in the same code block that come after while(true){}. But not so for the ones that aren't in the same code block.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Sun Nov 22, 2015 8:49 pm
by admin
What if the code within the loop throws an exception?

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Thu Feb 25, 2016 1:46 pm
by smily.sharma2009
even if it throws an exception, it will still not execute the last system.out.println.Can you please explain the reason for not throwing unreachable code.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Thu Feb 25, 2016 8:27 pm
by admin
smily.sharma2009 wrote:even if it throws an exception, it will still not execute the last system.out.println.
Why do you think so? You can try replacing the code in the while loop with throw new RuntimeException() and see what happens.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Tue Dec 06, 2016 9:52 am
by roosdebie
Why is it a OutOfMemory error and not a StackOverflow error? Do we have to know for the exam the distinction?
Thanks

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Tue Dec 06, 2016 11:17 am
by admin
OutOfMemory is when heap space runs out and StackOverflow is when stack space runs out. When you call a method from another method, the call details are placed on the stack. If you make a chain of such calls ( m1 calls m2, m2 calls m3, m3 calls m4 and so on), the stack (which is very small as compared to heap) will get filled up and you will get StackOverflow. This is not happening in this code.

Here is a good link: http://stackoverflow.com/questions/1143 ... emoryerror

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Wed Feb 15, 2017 9:01 am
by rp.reshma
please help me to understand how thos code will throw java.lang.OutOfMemoryError .

Code: Select all

List s1 = new ArrayList( );    
  
   try{             
       while(true){          
       s1.add("sdfa"); 
       }   
   }catch(RuntimeException e) {
             e.printStackTrace();
   }  
  System.out.println(s1.size());

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Wed Feb 15, 2017 9:01 pm
by admin
If you keep adding elements to an ArrayList forever, it will after some time run out of memory and the JVM will throw OutOfMemoryError.
You should try running the code.
Paul.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Sat Jul 07, 2018 10:38 am
by __JJ__
Why is this not unreachable? What is it in the JLS that distinguishes between this:

Code: Select all

        try{
            while(true){}
        }catch(RuntimeException e){}
        System.out.println();
wherein the SOP is not considered unreachable and this:

Code: Select all

        while(true){}
        System.out.println();
wherein the SOP IS considered unreachable?

I am quite miffed because it seems to be yet another corner case of a corner case.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Sat Jul 07, 2018 10:46 am
by admin
Well, a JVM could always throw an Error while it is executing the while loop and it will be caught by the catch clause, in which case the println will be executed. So, it doesn't look unreachable to me. Not a corner case at all!

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Tue Dec 04, 2018 2:58 pm
by OCAJO1
taking one of the previous posts one step further,

try{
while(true){}
}catch(RuntimeException e){}
System.out.println();

will the above code produce a run time OutOfMemoryError (which will not be caught by this catch anyway)?
the reason I ask is that this while loop does not do anything except churn, so is it really using up the memory to produce the OutOfMemoryError?

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Tue Dec 04, 2018 3:40 pm
by admin
what happened when you tried it out?

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Tue Dec 04, 2018 6:31 pm
by OCAJO1
It kept on running for about 7 or 8 minutes, so I stopped it. That's why I wondered if I did not get any OutOfMemeoryError because the while loop doesn't do anything.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Tue Dec 04, 2018 9:34 pm
by admin
That's right. OOMError is thrown when the JVM runs out of memory. In this case, the loop is not consuming any memory. Try creating some objects in the loop and you should get the error.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Wed Dec 05, 2018 3:21 pm
by OCAJO1
Will this exam have trick questions like this?

What type of error/exception the following piece of code will throw,

try{
while(true){}
} catch(RuntimeException e){}

1. OOMError
2. StackOverflowError
3. RuntimeException
4. no error

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Wed Dec 05, 2018 5:34 pm
by admin
It is possible. Also, why do you think it is a trick question? It is actually quite straightforward if you know the purpose of these exception classes.

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Wed Dec 05, 2018 5:43 pm
by OCAJO1
well the reason I would consider it a trick question is that usually endless events would either produce choice 1 or 2. And if one is not up on exceptions and errors, one may think of choice 3. However there it is, unpretentious answer, choice 4 :shock:

Re: About Question enthuware.ocajp.i.v8.2.1465 :

Posted: Wed Dec 05, 2018 10:44 pm
by admin
1. OOMError : Is the code consuming any memory? creating objects? No.
2. StackOverflowError: Is the code putting anything on the stack that is not getting removed (usually happens in the case of recursion)? No.
3. RuntimeException: Is the code calling any method or doing any operation? No.

Option 4 it is :)