About enthuware.ocajp.i.v8.2.1279
Moderator: admin
-
- Posts: 4
- Joined: Mon Feb 26, 2018 3:41 am
- Contact:
About enthuware.ocajp.i.v8.2.1279
How come we know that JLS explicitly defines this as an exception to the rule for if(false) and not for while(false).....?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About enthuware.ocajp.i.v8.2.1279
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.
Scroll to the bottom of section 14.21.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Wed Aug 29, 2018 3:20 pm
- Contact:
Re: About enthuware.ocajp.i.v8.2.1279
Hi admin!
I don't get it. You wrote: is also a compile-time error, isn't it?
Thus only two of them will compile without any errors:
Cheers,
Jérôme
I don't get it. You wrote:
SoSimilarly, for( int i = 0; false; i++) x = 3; is also a compile time error because x= 3 is unreachable.
Code: Select all
for( int i = 0; i< 0; i++) x = 3;
Thus only two of them will compile without any errors:
Code: Select all
if (false) { x=3; }
do{ x = 3; } while(false);
Jérôme
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About enthuware.ocajp.i.v8.2.1279
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.
-
- Posts: 2
- Joined: Wed Aug 29, 2018 3:20 pm
- Contact:
Re: About enthuware.ocajp.i.v8.2.1279
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
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
-
- Posts: 7
- Joined: Tue Apr 16, 2019 5:40 pm
- Contact:
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About enthuware.ocajp.i.v8.2.1279
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.
-
- Posts: 7
- Joined: Tue Apr 16, 2019 5:40 pm
- Contact:
Re: About enthuware.ocajp.i.v8.2.1279
I understand now. Thank you .
-
- Posts: 5
- Joined: Mon Jan 17, 2022 12:32 pm
- Contact:
Re: About enthuware.ocajp.i.v8.2.1279
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.
Who is online
Users browsing this forum: Bing [Bot] and 105 guests