Question Details

No question body available.

Tags

java boolean

Answers (11)

April 3, 2026 Score: 2 Rep: 16,428 Quality: Low Completeness: 40%

Are you certain you ran the above code? If you accidentally used single "=" inside the "if" in place of "==" the value is assigned to false. A more natural way would be to use if(isLightOn) { to determine on / off and use boolean isLightOn = true; to enter the first branch.

April 3, 2026 Score: 1 Rep: 87,713 Quality: Medium Completeness: 60%

The difference between = and ==

The single = is the assignment operator. Inside a condition in an if statement too.

your right, in my original code I had if (isLightOn = false) {

and that gave the "the light is off" output

if (isLightOn = false) assigns false to isLightOn and then evaluates to the assigned value, that is, false. So in this case the else part of your if-else statement is executed and the light is off is printed.

BTW your IDE ought to warn you that the value of isLightOn = false is always false rendering the if-else statement rather pointless

The ==, double equal sign, is the equality operator. It compares the two values (without modifying any) and evaluates to true if they are equal.

The preferred way

All of this said and repeating what others have said: Prefer if (isLightOn) even if it means reversing the two parts of your if-else statement. Or if you don’t want that, use the not operator or exclamation mark: if (! isLightOn) (the space after ! is optional and most don’t type it; I personally find the negation easier to spot when separated from the variable name).

April 3, 2026 Score: 1 Rep: 108,123 Quality: Medium Completeness: 80%

Forget about booleans for a moment.

Consider 5 + 2, which consists of 3 things:

  • A left hand side (5 in this case) - which is itself an expression
  • A right hand side (2 in this case) - which is itself an expression
  • An operator (+ in this case)

and this whole construction (the entire 5 + 2) is, itself, a value. This is why we could also write 3 * (5 + 2), for example. The value of the whole thing (5 + 2) is itself a value of type int.

Now let's bring booleans back:

booleans are exactly like it. They aren't magical in any way. But, they support a few different operations (you can't write true + false, for example. Try it - it won't compile, the operation 'addition' is not defined for 2 booleans).

For example, the operator == is exactly like + is: In technical terms, it's a binary operation: It has a left hand side, a right hand side, an operator symbol (== instead of +), and the whole thing is itself a value. Exactly like 5 + 2 contains 2 ints and is itself an int value, true == false contains 2 booleans and is itself a boolean value. Let's try it:

int a = 5;
int b = 2;
int c = 5 + 2;
System.out.println(c);
System.out.println(a == b);

boolean d = false; boolean e = true; boolean f = d == e; System.out.println(f);

d == e is itself a boolean value, exactly like true is a boolean value. The result of d == e here is false: d == e boils down to false == true (because the value of d is false and the value of e is true), and false is of course not the same as true. Similarly:

boolean g = false == false;
System.out.println(g);

The above would print true. After all, false and false are identical, and == answers the question 'are the left hand thing and the right hand thing identical'. They are.

You might be confused as to how 2 'false' somehow makes a 'true'. They.. just do. If this mystifies you, consider the following code:

int x = 1 - 1;

We start with only 1 values and nevertheless, we end up with a 0. In the same way, with only 2 false values, we can make a true.

Finally, lets bring if into it: if takes the form of:

if (OPERAND) ACTION;

and means: if OPERAND is true, then do ACTION. If OPERAND is false, then don't do anything. If OPERAND is not a boolean value at all, refuse to compile this code.

Now, we have everything needed to explain your code

isLightOn is 'false', right? That's what boolean isLightOn = false; means.

So, if (isLightOn == false) boils down to if (false == false). We know that false == false boils down to true (just like 5 + 2 is the same as 7), so this reads: if (true).

Hence it prints 'the light is on'.

What you probably intended to write is simply:

if (isLightOn) {
  System.out.println("The light is on");
} else {
  System.out.println("The light is off");
}

There's no need for any == in an if block. The stuff in the parentheses simply needs to be a boolean value. a == b is an expression if type boolean, but so is the expression z, if z is a variable of type boolean. Or 5 < 10, that's also a boolean. You can put any boolean value in an if.

Code smells

Generally == true and == false, written literally like that, are essentially always wrong. The code works fine, but it's.. kinda useless and 'bad code style' - java programmers aren't accustomed to it, it makes code harder to understand, and gets in the way of clarity and having code easily testable and such.

Imagine someone wrote this code:

int x = askUserForValue();

if (x + 0 > 5) { System.out.println("You entered a value larger than 5!"); }

This code is.. kinda silly, no? What is that + 0 doing there? It doesn't do anything - if we made that code if (x > 5) { it would do exactly the same thing. The above snippet will compile and does what you think it does, it's just.. needlessly convoluted.

The same applies to x == true and x == false where x can be anything you like. That's like sticking + 0 at the end of a numeric expression: Useless, and only serves to confuse.

In java, the only thing you can even write for x in x == true is a boolean value. The expression x == true resolves to the value true of x is true, and resolves to the value false if x is false. So... if (x == true) is identical in every way to if (x). So.. just write that then.

if (x == false) is the same thing, except it flips the value of x: x == false resolves to true if x is false and vice versa. However, to 'flip' a boolean value, there's an operator for that: !. Which is shorter and more commonly used, so.. just do that then: never write x == false. Instead, write !x.

April 3, 2026 Score: 1 Rep: 4,254 Quality: Low Completeness: 0%

You seem to be inverting the reality of your code - is that deliberate?

April 3, 2026 Score: 1 Rep: 1 Quality: Low Completeness: 10%

your right, in my original code I had if (isLightOn = false) {

and that gave the "the light is off" output

but I don't understand why the == double equals sign makes so much of a difference.

if I say (isLightOn = false) does that not mean isLightOn is false. im still a but confused

April 3, 2026 Score: 0 Rep: 221,246 Quality: Low Completeness: 0%

The code shown does not produce the output described. You're mistaken.

April 3, 2026 Score: 0 Rep: 1 Quality: Low Completeness: 50%

The output of the code you have posted is "The light is on". As DuncG already mentioned, would this be the better way to evaluate a boolean against true

public class Main {
    public static void main(String[] args) {
      boolean isLightOn = true;

if (isLightOn) { System.out.println("The light is on."); } else { System.out.print ("The light is off"); } } }

The other way around would be

public class Main {
    public static void main(String[] args) {
      boolean isLightOn = false;

if (!isLightOn) { System.out.print ("The light is off"); } else { System.out.println("The light is on."); } } }

The "!" (exclamation mark) before the variable isLightOn stands for "not" (if not isLightOn)

April 3, 2026 Score: 0 Rep: 1 Quality: Low Completeness: 0%

thank you so much.

the code btw was just me being silly, I just wanted to see what would happen and didn't understand the output, but whis was really helpful.

April 3, 2026 Score: 0 Rep: 1 Quality: Low Completeness: 50%

To say it another words: if (isLightOn = false) is nearly the same as if (isLightOn) - the only difference in the first statement the value false will be assigned to the variable isLightOn, the second statement will use the value which is already assigned to the variable isLightOn for the evaluation. The first statement means - if you break it down - if (false). And evaluating this if statement is always false and will always execute the else part (if there is one) of the if-statement.

April 3, 2026 Score: 0 Rep: 1 Quality: Low Completeness: 0%

I've never learned so much information all at once, thank you kind stranger

April 3, 2026 Score: 0 Rep: 19,341 Quality: Low Completeness: 40%

And when reading the code in your head, read !x as not x to put you in the right mindset: if (!isLightOn) would be pronounced as if not is light on. This isn't grammatically correct English, but it should be an understandable translation from Java to English (or C to English, or C# to English, or JavaScript to English...)