• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assert Question

 
Ruben Lunda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code:



What cause compilation to fail?

A) Line 13
B) Line 14
C) Line 18
D) Line 20.


The correct answer is (D) . Why???
 
Donny Nadolny
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My compiler produces an error on all 4 of the options.
 
Keith Tingle
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you inform the java compiler that you want 1.4 source compatabilty (-source 1.4) it will not consider 'assert' a keyword.

The only error I get on this code is 'unreachable code' for the the final assert.

IBM JDK 1.4.2.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code


Please always quote where you found the code. That is what book, author, or mock exam.

Did you actually complie the code? What happened?

Thankyou
-Barry
 
Ruben Lunda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Keith,
I did not see the error!
 
Swati Udas
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why does'nt line 14 give the same error..
after assert false in line 13..line 14 will never be reached!!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Then why does'nt line 14 give the same error..
after assert false in line 13..line 14 will never be reached!!



Because assertions can be disabled at runtime (probably).
 
K Gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is with while loop because the error given is
unreachable statement.But why
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by K Gupta:
I think the problem is with while loop because the error given is
unreachable statement.But why


It's because the while(true) loop cannot be exited. There is no break statement to get out of that loop. So any instruction after that loop cannot be reached. The compiler can detect that problem.
[ September 01, 2004: Message edited by: Barry Gaunt ]
 
K Gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if i compile following code
class A{
public void f(){
while(true){
}
}
it compiles!!
Then what is the problem with assert code
 
K Gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if i compile following code
class A{
public void f(){
while(true){
}
}
}
it compiles!!
Then what is the problem with assert code
 
Arun Yadav
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by K Gupta

But if i compile following code
1.class A{
2. public void f(){
3. while(true){
4. }
5. }
6.}
it compiles!!
Then what is the problem with assert code



The above code compiles because there is nothing which compiler can see as unreachable, however if we put any statement between line 4 and line 5, wheather its assert or any statement, you will find compiler complaning unreachable statement. hope this explain why its compiling..

Arun
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic