I cannot understand booleans


#1

Good morning,

I'm "learning" (being honest i'm remembering the basics of Java) and i've got some problems to understand the type BOOLEAN.

So basically Boolean can be true or false, OK, but when can you use it? Like

boolean morning = "true";
boolean tuesday = "false";

System.out.Println(tuesday + morning);

screen would show = False

System.out.Println(tuesday != morning);

Screen would show = True

Ok not the best example, but what use can Booleans give me?


#2

They're particularly useful for deciding whether to execute some piece of code:

if today == TUESDAY:
    print("it's tuesday")
else:
    print("it's not tuesday.")

#3

Booleans are often used in 'if' or 'while' loops. For example:

if (value1==value2) {
sameValue = true;
} else {
sameValue = false;
}

while(!gotPizza) { //while gotPizza is false
boolean purchasedPizza = getPizza(); //calls getPizza() method
gotPizza = purchasedPizza;
}


#4

A lot of thanks both for the help! I could understand it very easily!


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.