Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top