I cannot understand booleans


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?


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

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


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;


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


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