About enthuware.ocajp.i.v8.2.1279

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 » Mon Feb 26, 2018 3:48 am

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

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

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

Post by admin » Mon Feb 26, 2018 5:34 am

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 » Wed Aug 29, 2018 3:27 pm

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

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

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

Post by admin » Wed Aug 29, 2018 6:57 pm

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

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

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

Post by vilasa » Thu May 09, 2019 10:07 pm

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

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

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

Post by admin » Thu May 09, 2019 10:25 pm

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 » Fri May 10, 2019 5:13 pm

I understand now. Thank you .

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests