About enthuware.ocajp.i.v8.2.1279

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

Moderator: admin

Post Reply
Lunarkiran
Posts: 4
Joined: Mon Feb 26, 2018 3:41 am
Contact:

About enthuware.ocajp.i.v8.2.1279

Post by Lunarkiran »

How come we know that JLS explicitly defines this as an exception to the rule for if(false) and not for while(false).....? :roll:

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

Re: About enthuware.ocajp.i.v8.2.1279

Post by admin »

We know because it is written in the specification: https://docs.oracle.com/javase/specs/jl ... #jls-14.21
Scroll to the bottom of section 14.21.
If you like our products and services, please help us by posting your review here.

Jerome
Posts: 2
Joined: Wed Aug 29, 2018 3:20 pm
Contact:

Re: About enthuware.ocajp.i.v8.2.1279

Post by Jerome »

Hi admin!

I don't get it. You wrote:
Similarly, for( int i = 0; false; i++) x = 3; is also a compile time error because x= 3 is unreachable.
So

Code: Select all

for( int i = 0; i< 0; i++) x = 3;
is also a compile-time error, isn't it?

Thus only two of them will compile without any errors:

Code: Select all

if (false) { x=3; }
do{ x = 3; } while(false);
Cheers,
Jérôme

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

Re: About enthuware.ocajp.i.v8.2.1279

Post by admin »

What happened when you tried compiling for( int i = 0; i< 0; i++) x = 3;. ?
If you like our products and services, please help us by posting your review here.

Jerome
Posts: 2
Joined: Wed Aug 29, 2018 3:20 pm
Contact:

Re: About enthuware.ocajp.i.v8.2.1279

Post by Jerome »

Hi,

ok I got it:
for( int i = 0; i< 0; i++) x = 3; => x is reachable with i<0
for( int i = 0; false; i++) x = 3; => x is never reachable, no matter the value of i

It was easy ;o)

Thanks for your support
Jérôme

vilasa
Posts: 7
Joined: Tue Apr 16, 2019 5:40 pm
Contact:

Re: About enthuware.ocajp.i.v8.2.1279

Post by vilasa »

admin wrote:
Wed Aug 29, 2018 6:57 pm
What happened when you tried compiling for( int i = 0; i< 0; i++) x = 3;. ?
compiles fine.But x is unreachable .Because when I tried to print x ,it was 0 . As per logic explained in JLS this should give compile error

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

Re: About enthuware.ocajp.i.v8.2.1279

Post by admin »

No, which part of JLS are your referring to? Please go through the replies above.
If you like our products and services, please help us by posting your review here.

vilasa
Posts: 7
Joined: Tue Apr 16, 2019 5:40 pm
Contact:

Re: About enthuware.ocajp.i.v8.2.1279

Post by vilasa »

I understand now. Thank you .

tangjm
Posts: 5
Joined: Mon Jan 17, 2022 12:32 pm
Contact:

Re: About enthuware.ocajp.i.v8.2.1279

Post by tangjm »

Jerome wrote:
Fri Aug 31, 2018 1:50 am
Hi,

ok I got it:
for( int i = 0; i< 0; i++) x = 3; => x is reachable with i<0
for( int i = 0; false; i++) x = 3; => x is never reachable, no matter the value of i

It was easy ;o)

Thanks for your support
Jérôme
Yeah, the key thing to note is the use of the term 'constant expression' in section 14.21 of the JLS.

'The contained statement is reachable iff the for statement is reachable and the condition expression is not a constant expression whose value is false.'

To clarify, 'i < 0' is not a constant expression whereas 'false' is a constant expression. Hence, the former 'for' loop is reachable and compiles but the latter is unreachable and fails to compile.

Post Reply

Who is online

Users browsing this forum: No registered users and 50 guests