JoJo
JoJo

Reputation: 1447

what is the meaning of the <= (less than or equal) operator for boolean operations?

I saw a piece of java code like this:

int y = 100;
boolean x = y <= 0;
System.out.println(x);

<= is strange for me due to this using way, Could anyone explain the <= here, how can I use it?

Upvotes: 0

Views: 253

Answers (2)

David Brossard
David Brossard

Reputation: 13834

Read it as:

boolean x = (y <= 0);

This means that:

  • x will be true if y is equal to or less than zero.
  • x will be false if y is greater strictly than zero.

Upvotes: 5

rgettman
rgettman

Reputation: 178263

The assignment operator = is of lower precedence in Java than <=, so <= is performed first. The boolean result of y <= 0 is assigned to x. It could have more clearly written:

boolean x = (y <= 0);

But the effect is the same.

Upvotes: 9

Related Questions