• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Valentin's mock exam no1

 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Valentin

public class Question38 {
public static void main(String[] args){
while(false);//line 1
if(false);//line 2
do{}while(false);//line 3
for(;false ;//line 4
}
}

You stated about the above question that line1 and line 4 won't compile because of unreachable statements problem. I would agree with you if there was another statement other than the empty one. The code above compiles fine but not the following.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on which compiler you use to compile the code, but as per the specification (JLS 14.20 Unreachable Statements), such code SHOULD NOT compile.
An excerpt of that section is given here:
-----JLS 14.20 quote begin-----
[...]
the following statement results in a compile-time error:
while (false) { x=3; }
because the statement x=3; is not reachable; but the superficially similar
case:
if (false) { x=3; }
does not result in a compile-time error. An optimizing compiler may realize
that the statement x=3; will never be executed and may choose to omit the
code for that statement from the generated class file, but the statement
x=3; is not regarded as "unreachable" in the technical sense specified here.
-----JLS 14.20 quote end-----
If you read that section entirely, it will become clear to you that only
lines 2 and 3 compile correctly. Bottom line,
when in doubt, trust the specification and not the compilers which are not
always specification-compliant...
The empty statement is a statement like any other except that certain optimizing compilers may decide to disregard it.
[ October 14, 2002: Message edited by: Valentin Crettaz ]
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right Valentin but when using
while(false); there's no statement to be reached anyway.
P.S i'm using jdk 1.4.1
Did you try to compile it. ?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the statement to execute is the empty statement denoted by the semicolon ;
And yes, I have compiled that code and it produces.

PS1: I never release code that has not been compiled
PS2: Both 1.4 and 1.4.1 yield the same compilation output
[ October 14, 2002: Message edited by: Valentin Crettaz ]
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Weird I compiled it using j.d.k 1.4.1 and 1.3.1 and it doesn't produce any errors.
Anyway thx and sorry about that
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw do you have any other mock exams. I can only see one ?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am preparing a second mock exam but I have been under lots of pressure lately and had to postpone the release of my new mock. It should be out by the end of this year.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic