G Batra wrote:Is the usability of the statement drives the validity of it? I mean, if the statement is useful in any way, then the statement becomes a valid statement. If it isn't then it is NOT a valid statement.
I assume one (or more) sections in the JLS will specify all conditions a statement must fulfill to be valid.
This and all subsequent sections describe all valid statements. And I think we can agree that the empty statement is pretty useless, but it's allowed (and thus will compile)
G Batra wrote:Just wanted to know, why Java doesn't enforce the method return values to be assigned to appropriate variable in the calling method? Does it not make the return values as "useless".?
What if you are not interested in the return value. For example, the
add() method of
ArrayList returns a boolean. I have used this method many, many, many times without using the return value, because in these use cases it didn't matter. So if the Java compiler enforces return values to be assigned, your code would be a mess because you would need to create a bunch of local variables which are not used at all. So you would need to type
boolean result =, just to store the return value of the
add() method and make the compiler happy. But you won't do anything with this variable, because you are not interested in this return value. And that's for every method invocation which returns a value
Hope it helps!
Kind regards,
Roel