Booleans
Let's explore a new type of data, booleans. Booleans can hold one of two values: true
or false
. Let's open up the Java REPL and look at some operators that return booleans.
> "1" == "1";
java.lang.Boolean res0 = true
> "1" == "2";
java.lang.Boolean res1 = false
> 1 == 1;
java.lang.Boolean res2 = true
> Boolean isMathWrong = 1 > 2;
java.lang.Boolean falseStatement = false
> Boolean isMathRight = 1 < 2;
java.lang.Boolean trueStatement = true
This is a pretty straight forward concept. The equality operator ==
is asking if something is equal to something else. If it is then the Boolean will be true
.
In the statement "1" == "1"
we are checking to make sure that "1"
is equal to "1"
. They are, so the Java REPL creates a Boolean variable java.lang.Boolean res0 = true
.
Likewise, in the second statement "1"
is not equal to "2"
, so we create a Boolean variable that isfalse
: java.lang.Boolean res1 = false
We can also save Boolean variables to use again later like in Boolean isMathWrong = 1 > 2;
and Boolean isMathRight = 1 < 2;
. Here we are creating our Booleans using the relational operators > and < to check if an Integer is greater than or less than another operator. Once the result is processed into a Boolean statement, we save it into a Boolean variable.
Using Booleans, we can add branching logic in the applications we write. Let's write anif/else
statement to see if somebody can see a rated R movie.
if-else-then/RatedR.java
import java.io.Console;
public class RatedR {
public static void main(String[] args) {
Console console = System.console();
System.out.println("How old are you?");
String stringUserAge = console.readLine();
Integer integerUserAge = Integer.parseInt(stringUserAge);
if ( integerUserAge >= 17 ) {
System.out.println("You can see the movie!");
} else {
System.out.println("I'm sorry, you are too young to see that movie.");
}
}
}
We took the user's age, converted it to an integer and then used the relational operator >=
to check and see if their age was greater than or equal to17
. When the code after if
, ( integerUserAge >= 17 )
, evaluates to true
, Java will run all the code between the first set of curly braces. When it evaluates to false
, Java will run all the code between the set of curly braces that follows else
.
There are also a lot of methods that will return a Boolean. Let's create a statement using one of these methods.
if-else-then/StartsWithZ.java
import java.io.Console;
public class StartsWithZ {
public static void main(String[] args) {
Console console = System.console();
System.out.println("What's your name?");
String userName = console.readLine();
if ( userName.startsWith("Z") ) {
System.out.println("Your name starts with a Z!");
} else {
System.out.println("Your name doesn't start with a Z :(");
}
}
}
The parentheses after the if
statement is where we store our statement that returns a boolean. We used userName.startsWith("Z")
as the statement here. The method startsWith("Z")
is called on the userName
variable. It will take the string argument passed to it, "Z"
and compare it to the first letter of userName
. It will return true
if it is equal to the argument, and false
if not.
Here is a list of operators that you can reference for your exercises:
Equality
- ==: equal to. 1 == 1 is true. 1 == 2 is false.
- !=: not equal to. 1 != 2 is true. `1 != 1 is false.
Relational
: greater than. 2 > 1 is true. 1 > 2 is false.
=: greater than or equal to. 2 >= 2 and 2 >= 1 are both true. 2 >= 3 is false.
- <: less than. 1 < 2 is true. 2 < 1 is false.
- <=: less than or equal to. 1 <= 1 and 1 <= 2 are both true. 2 <= 1 is false.