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

StackOverflow https://stackoverflow.com/questions/19524121

  •  01-07-2022
  •  | 
  •  

Вопрос

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?

Это было полезно?

Решение

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.

Другие советы

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.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top