Bookmark Topic Watch Topic
  • New Topic

dead code and Unreachable code diff

 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Report post to moderator






please tell the difference.

Thanks
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
Veena ,

The first code sample you provided is Dead Code and it will not compile at all ! The second code sample does compile but a warning is issued by your IDE . This is the major difference . Apart from that , the reason why the code in the second code compiles is that you are using varibales and the java compiler 'thinks' that maybe these variables will change on runtime. However, when you use the same code and replace variables with actual values , the compiler knows already at compile time that this code will not be reached ever.

Another thing I would like to point out is that if you mark your variables as final in the second code sample, you will again get compile time error ( instead of warning ) because of the "final" nature of the variable.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Report post to moderator
veena - as saif's reply refers to you using variables in the second code sample, when in fact there aren't any, I'm assuming you have edited the code. Please don't do this - it makes the rest of the thread meaningless.
 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Report post to moderator
hello,
asif

thanks for the reply, i had made editing mistake in the code so i have edited the code, based on that you answer,please.

the 1st sample code compiles n run successfully. No doubt. statement after else not considered as not reachable code, where as in while loop, it is.

that is the Question.

why it behaves different in if and while loop
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Likes 2
  • Mark post as helpful
  • send pies
  • Report post to moderator
veena bijur wrote: . . . i had made editing mistake in the code so i have edited the code, based on that you answer,please. . . .
No, that is the wrong thing to do. As Joanne Neal has already said, it makes the thread incomprehensible.

To avoid further confusion, I am closing this thread, and suggest you post a new thread with what you are asking about, without “editing mistake”s. Then we can all see what we are really dealing with.
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic