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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Jokumo♫
Posts: 6
Joined: Wed Apr 20, 2016 9:56 am
Contact:

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

Post by Jokumo♫ »

isn't the while part unreachable code?
do { break ; } while (true) ;
I tried it out and it compiles without error. So I tried it out with some code in the while part and managed to crash the compiler :o (reported it on http://bugreport.java.com as suggested by the compiler)

Code: Select all

public class DoWhileBug {
	public static void main(String... args){
		Integer i = 100;
		do{ break; } while(i++ < 10);
	}

}
however, on using int instead of an Integer it compiles without error, but wouldn't it be unreachable code?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Jokumo♫ wrote:isn't the while part unreachable code?

Code: Select all

do { break ; } while (true) ;
The compiler doesn't execute the code so it doesn't know that the code in the body will always cause the loop to break. That is why it cannot flag the unreachable error.
The compiler can only look at the compile time constant values to determine unreachable code.

Here is a good discussion about this: http://stackoverflow.com/questions/3795 ... iler-error
If you like our products and services, please help us by posting your review here.

Jokumo♫
Posts: 6
Joined: Wed Apr 20, 2016 9:56 am
Contact:

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

Post by Jokumo♫ »

but if I add a System.out.println(); after the break the compiler complains about it as unreachable code.

good discussion by the way, but I have no problem to just accept that java doesn't accept unreachable code, exept of if(false){ ... }. And in real life I would never ever write such a silly peace of code as mentioned in my first post. But since we have to know every tiny silliness and exception for the exam, I am wondering if this is another exception of the rule, a bug in the compiler or something completely different I hadn't thought about it until now.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

Even I have the doubt : if I add a System.out.println(); after the break the compiler complains about it as unreachable code.
so in that case, the compiler knows about the break causing an unreachable statement. then why will the while() in the question not become unreachable ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

did you go through the stackoverflow link mentioned above?
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

its a little confusing. can you explain please

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Attribute it to inconsistency in the language :) As you found out in the discussion here: https://coderanch.com/t/683311/certific ... piled-Code
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

oh okay. is it really an inconsistency but ?

tcroots19
Posts: 6
Joined: Tue Nov 19, 2019 2:01 pm
Contact:

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

Post by tcroots19 »

the answer the third says, "You cannot have break or continue in an 'if' or 'else' block without being inside a loop." but then goes on to give an if/else with a label before it and a break inside...which does compile. So it seems you can do it outside a loop you just need a label that is in scope, i.e. a label for the if statement. Is that correct?

Interesting discussion on it here, https://stackoverflow.com/questions/415 ... va-what-if enjoyed the "herd theory" comment

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Correct. The first statement of the explanation is talking about just the break statement (which is applicable only in a loop). The exception to this rule is the "labeled break" statement, which is valid in an if statement.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests