Why 'condition && statement()' is not popular in Java world?

They are not interchangeable:

isValid && statement(); is an expression and not a statement. Also it would only work when your statement() method is of type boolean .

So you could only use this where an (predicate) expression is expected by the Java-language syntax.

opinion: If you would use it when meeting the above requirements, then i would indeed find that it "Does decrease readability". (as it can only be used in certain situations, plus by your own words ("doing Java development for an almost decade now and I've never seen") nobody is expecting it)


if( isValid && statement() ) only works if statement() returns a boolean otherwise the condition wouldn't be compilable. Doing that might not be considered good design (depends on what statement actually does) and might reduce readability or your ability to use the return value of statement().

Besides that just isValid && statement(); isn't supported by the language - and don't make the mistake to compare a language like Java to a language like JavaScript, they follow different paradigms.

You could do something like boolean b = isValid && statement(); but you'd normally only do that if you want to use the value of b somehow.

Edit: As requested by KRyan I'll also add a paragraph on the meaning of a && b and what it is used for in Java as well as JavaScript (although I'm no expert here).

Java

In general you can combine two boolean expressions using & (and) or | (or). Thus true & true will result in true as does true | false, but true & false will result in falseetc.

The problem with the single character operators is that every argument is evaluated even if you already know the result. As an example when you see false & ... you already know that the result is false when looking at the first argument and any other arguments won't make a difference.

Now if you have an expression that calls some methods which return a boolean value you might not want to execute those methods if they aren't needed, e.g. when the methods themselves are quite expensive. As an example consider needsCheckWebsite & isValueOnWebsite( "test" ). isValueOnWebsite( "test" ) might take a while and if needsCheckWebsite was false you'd not want to execute that method.

To fix that you can use the double-character operators && and ||, aka short-circuit operators. That means that if you write needsCheckWebsite && isValueOnWebsite( "test" ) and if needsCheckWebsite is false, the expensive execution of isValueOnWebsite( "test" ) won't happen since we already know that the result doesn't change a thing.

Hence isValid && statement() would mean that statement() should only be executed if isValid.

JavaScript

AFAIK in JavaScript the short-circuit operators work in a similar way in that it evaluates the first argument and if the result is still unknown the second argument is evaluated and returned. However, since JavaScript is a dynamically typed language (in constrast to Java which is statically typed) you can mix different types, e.g. var value = null || "hello" which would result in value = "hello".

This allows the developer to use && and || for things other than boolean logic, like supplying a default value, which is not possible in Java due to the static typing (in Java you could use the ternary operator ? for that, e.g. String s = input != null ? input : defaultValue but as you can see that doesn't look as elegant as JavaScript's var s = input || defaultValue).