• Post Reply Bookmark Topic Watch Topic
  • New Topic

When Do Conditional Statements Cause Compilation Failures???  RSS feed

 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So one thing that has plagued me in my test practice is when conditional statements actually cause a failure to compile?
For example:


So hopefully you guys can see where I'm having some logic problems trying to figure out how the compiler works. I don't understand why the "instanceof" line causes a compilation error, yet putting dead code in (essentially the exact same thing right???) doesn't?

Much appreciation for anyone that can kind of give me a rundown of things that cause conditional statements to fail to compile???
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
' a instanceof b' check whether a is instance of b. Means, a inherit/extends b.

in your case Bird and Bat are 2 different class and are not related by any mean. That's why it is giving error.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This fails to compile because the compiler sees that the type of the variable b is Bat, which is a type which is not related to Bird, so the compiler knows that this instanceof check would always be false.


This works because this is a special case. You'd say that the body of this if-statement is unreachable code, so the compiler should reject it. But this is allowed to support conditional compilation - so that you can quickly "comment out" a block of code by surrounding it with if (false) { ... }. See section 13.4.9 of the JLS for details.


Works because the values of x and y are not constants, so the compiler cannot assume they have specific values at any point in the code.

See also section 14.21 of the JLS which describes what is and is not regarded as unreachable code.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(false) is a special case that the compiler allows. This is so you can do debugging with this kind of code:

 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys! I wasn't aware that putting "false" in was a special case, but that makes all the other issues I had clear now. So basically, if a condition will be false (aside from using the word "false") with no possible way of ever being true (such as a variable being reassigned to a different value that would make it true), then it throws an error. Got it!
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. As a general rule you are allowed anything however daft it is. There are but few exceptions.
You can write if (false) ... but not while (false) ...
You cannot write instanceof or casts when it is obvious to the compiler the result will be false or the cast will fail.

I am afraid you are going to have to go through the Java® Language Specification (=JLS) (as link provided earlier and two links in this post) to see what is permissible and what isn't. And you will find the JLS difficult to read.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!