I'm trying to evaluate expression (1 <= month <= 12)
in a if condition.
This statement seems valid in javascript, but not in Java.
In Java,
int month = 0;
boolean flag = (1 <= month <= 12);
It throws following error:
The operator <= is undefined for the argument type(s) boolean, int
In Javascript,
var month = 0;
console.log('Expression evaluates to: ', (1 <= month <= 12));
It always returns
true
no matter what the value of month is.
Can someone please explain:
true
in javascript?Also I know I can get it to work it this way (1 <= month && month <= 12)
. So, not looking for a solution but an explanation.
Thanks. Also let me know if my questions are not clear.
I'm trying to evaluate expression (1 <= month <= 12)
in a if condition.
This statement seems valid in javascript, but not in Java.
In Java,
int month = 0;
boolean flag = (1 <= month <= 12);
It throws following error:
The operator <= is undefined for the argument type(s) boolean, int
In Javascript,
var month = 0;
console.log('Expression evaluates to: ', (1 <= month <= 12));
It always returns
true
no matter what the value of month is.
Can someone please explain:
true
in javascript?Also I know I can get it to work it this way (1 <= month && month <= 12)
. So, not looking for a solution but an explanation.
Thanks. Also let me know if my questions are not clear.
(1 <= month) <= 12
).
– Pointy
Commented
Apr 2, 2015 at 14:27
<=
is non-associative, so you can't use it by repetition. You can specify it with:
1 <= month && month <= 12
The reason is that the JavaScript parser parses 1 <= month <= 12
as:
(1 <= month) <= 12
It's a consequence of the grammar of JavaScript, they could have defined it otherwise, but it would plicate the matter a bit. Most grammars define the expressions as:
expr -> [0-9]+
expr -> identifier
expr -> expr '<=' expr
(with an LALR) parser.
And Java uses the following (approximate) grammar:
expr -> numExpr '<=' numExpr
expr -> numExpr
numExpr -> identifier
numExpr -> [0-9]+
(...and so on...)
In Java it is thus even impossible to parse such expression (unless you perform a cast which makes it a numExp
again).
For the JavaScript part, why does it always return true
?
Now (1 <= month)
is a boolean (true/1
or false/0
), and that value cannot be pared (reasonable) with 12
(0
and 1
are always less than or equal to 12
). Only very limited programming languages support such feature.
Regarding the subquestion
Why does java consider it as an invalid expression?
It's because Java evaluates it the following way:
(1 <= month) <= 12
boolean <= int
Booleans and ints cannot be pared because of the type safety.
The reason is due to short-circuit evaluation (which is the method used by most programming languages to evaluate logical expressions). Essentially what happens is the expression is evaluated left-to-right and transformed along the way so...
1 <= month <= 12
Gets evaluated as:
(1 <= month) <= 12
Which either gives you:
true <= 12
/* or */
false <= 12
As you can see, in Java (since it is type-safe) you get a type error. Because you cant use the <=
operator on a boolean. In JS, booleans are always <=
to a number (you can test this in your console).
Hope that answers your question!