This code does not compile due to an unreachable statement

I tried my best to reproduce it in a different way, but it seems that only this line gives an error (please excuse the comment):

return foo.containsKey(MARKET_DATA) && !foo.get(MARKET_DATA).isMissing();/*mapping code can inject a Missing type*/;

However, I am typing this, it gives me an error: "unreachable statement". Why is this?

+4
source share
3 answers

This is clear if you delete the comment:

return foo.containsKey(MARKET_DATA) && !foo.get(MARKET_DATA).isMissing();;

Note that there are two semicolons at the end: empty statements are allowed in Java, but this particular empty statement is unreachable since the previous statement is always returns.

(For the avoidance of doubt, a comment should not end with a symbol ;).

+10
source

; : /*mapping code can inject a Missing type*/;

, return . ; , ( ).

+2

By Specification, Java ; is an empty statement. So after return

0
source

Source: https://habr.com/ru/post/1654532/


All Articles