[java] if (boolean condition) in Java

This is always very confusing to me. Can someone please explain it? The confusion I have is - boolean default to false. So in the below example, does it enter the if loop when state is not turned on, i.e., it is TURNED OFF if (condition is false) OR does it enter the if loop when state is TURNED ON, in other words if (condition is true)?

boolean turnedOn;
if (turnedOn) {
    //do stuff when the condition is false or true?
} else {
    //do else of if
}

I know this is a very basic question, but if you could explain the answer in very basic language, that would be great. :) Feel free to point me to duplicate posts that have a very good explanation (I did not find one where I could clearly get it). Also feel free to change the subject of the post if you'd like to make it more generic.

This question is related to java if-statement

The answer is


boolean state = "TURNED ON";

is not a Java valid code. boolean can receive only boolean values (true or false) and "TURNED ON"is a String.

EDIT:

now you are talking about a loop and your code does not contain any. your var state is false because the boolean default value and you execute the else clause.


Booleans default value is false only for classes' fields. If within a method, you have to initialize your variable by true or false. Thus for example in your case, you'll have a compilation error.

Moreover, I don't really get the point, but the only way to enter within a if is to evaluate the condition to true.


The syntax of if block is as below,

if(condition){
// Executes when condition evaluates to true.
}
else{
// Executes when condition evaluates to false.
}

In your case you are directly passing a boolean value so no evaluation is required.


if (turnedOn) {
    //do stuff when the condition is false or true?
}
else {
    //do else of if
}

It can be written like:

if (turnedOn == true) {
        //do stuff when the condition is false or true?
    }
else { // turnedOn == false or !turnedOn
        //do else of if
}

So if your turnedOn variable is true, if will be called, if is assigned to false, else will be called. boolean values are implicitly assigned to false if you won't assign them explicitly e.q. turnedOn = true


In your example, the IF statement will run when it is state = true meaning the else part will run when state = false.

if(turnedOn == true) is the same as if(turnedOn)

if(turnedOn == false) is the same as if(!turnedOn)

If you have:

boolean turnedOn = false;

Or

boolean turnedOn;

Then

if(turnedOn)
{

}
else
{
    // This would run!
}

This is how the if behaves.

if(turnedOn) // This turnedOn should be a boolean or you could have a condition here which would give a boolean result.
{
// It will come here if turnedOn is true (i.e) the condition in the "if" evaluates to true
}
else
{
// It will come here if turnedOn is false (i.e) the condition in the "if" evaluates to false
}

boolean turnedOn;
    if(turnedOn)
    {
    //do stuff when the condition is true - i.e, turnedOn is true
    }
    else
    {
    //do stuff when the condition is false - i.e, turnedOn is false
    }

Assuming state is having a valid boolean value set in your actual code, then the following condition will succeed

if(state)

when state is boolean value is TRUE

If condition checks for the expression whether it is evaluated to TRUE/FALSE. If the expression is simple true then the condition will succeed.


Suppose you want to check a boolean. If true, do something. Else, do something else. You can write:

if(condition==true){

}
else{   //else means this checks for the opposite of what you checked at if

}

instead of that, you can do it simply like:

if(condition){  //this will check if condition is true 

}
else{ 

}

Inversely. If you were to do something if condition was false and do something else if condition was true. Then you would write:

if(condition!=true){   //if(condition=false)

}
else{

}

But following the simple path. We do:

if(!condition){  //it reads out as: if condition is not true. Which means if condition is false right?

}
else{

}

Think about it. You'll get it in no time.


ABoolean (with a uppercase 'B') is a Boolean object, which if not assigned a value, will default to null. boolean (with a lowercase 'b') is a boolean primitive, which if not assigned a value, will default to false.

Boolean objectBoolean;
boolean primitiveBoolean;

System.out.println(objectBoolean); // will print 'null'
System.out.println(primitiveBoolean); // will print 'false'

Citation

so in your code because boolean with small 'b' is declared it will set to false hence

boolean turnedOn;
    if(turnedOn) **meaning true**
    {
    //do stuff when the condition is false or true?
    }
    else
    {
    //do else of if ** itwill do this part bechae it is false
    }

the if(turnedon) tests a value if true, you didnt assign a value for turned on making it false, making it do the else statement :)