☕️ Boolean Expressions

5 min read > Topic Docs > Allison Vu > 4/2/22


In programming you often need to use data that can only have two values. Because of this, Java has something called a boolean data type which takes the values of true for false. It is the basis for all of Java’s comparisons and condition statements. A boolean type is called with a boolean keyword.

Example:
boolean youtubeIsFun = true;
boolean hwIsFun = false;
System.out.println(youtubeIsFun); //this will print out true
System.out.println(hwIsFun);//this will print out false

BOOLEAN EXPRESSION

A Boolean Expression is the Java expression that returns the boolean value with true or false. Mentioned previously, boolean is the basis for all of Java’s comparisons and conditions, so you can use > (greater than operator) to find out if the variable or expression is true.

int j = 9;
int t = 11;
System.out.println (t > j); //this will print out true
System.out.println (11 > 9); //this will print out true


You can use other symbols to call if the value is true or false. Such as: < (lesser than),==(equal to), <=(lesser than or equal to),>= (greater than or equal to)

Equals
int x = 7;
System.out.println (x==7); //Show true
System.out.println (6==7); //Show false

Less Than
int x = 7;
int y = 6;
System.out.println (y < x); //Show true
System.out.println (x < y);//Show false

Less Than or Equal to
int x = 7;
int y = 6;
System.out.println (y <= x); //Show true
System.out.println (x <= y);//Show false

Great Than or Equal to
int x = 7;
int y = 6;
System.out.println (x >= y);//Show true
System.out.println (y >= x);//Show false