java - Evaluating the expression 1 <= month <= 12 - Stack Overflow

admin2025-04-20  0

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:

  • If it is a valid expression or not?
  • Why does it always yield to true in javascript?
  • Why does java consider it as an invalid expression?

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:

  • If it is a valid expression or not?
  • Why does it always yield to true in javascript?
  • Why does java consider it as an invalid expression?

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.

Share Improve this question asked Apr 2, 2015 at 14:25 iaLiaL 38610 silver badges19 bronze badges 2
  • 1 That's a valid expression (in JavaScript, not Java), but it doesn't do what you think it does. Java won't let you perform a parison like that between a number and a boolean, but it parses the expression the same way ((1 <= month) <= 12). – Pointy Commented Apr 2, 2015 at 14:27
  • 2 Dont pare Javascript and Java. There are not made for the same purpose. Java is strongly typed, javascript is not and will allow more funky style programming – ortis Commented Apr 2, 2015 at 14:30
Add a ment  | 

3 Answers 3

Reset to default 10

<= 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!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745105982a285292.html

最新回复(0)