Why does `instanceof` error rather than return `false` when used for 2 incompatible classes?

前端 未结 4 1419
迷失自我
迷失自我 2020-12-11 17:58

I\'m reading this:
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2

They say:

Consider the example program

4条回答
  •  悲&欢浪女
    2020-12-11 18:11

    instanceof check is a runtime check. The compiler is able to discover that this condition is incorrect at compile time (much earlier), so it tells you that it is wrong. Always remember, that failing fast is a good practice, it will save you a lot of time and nerves.

提交回复
热议问题