- A Boolean type variable is one that can only be either
true
orfalse
. - In Java the Boolean type is denoted by the keyword
boolean
. To create a Boolean variable, just as with other type variables, we declare it:
boolean isThisASushiCard;
Here, the type of the variable comes first, then the name of the variable, which is
isThisASushiCard
. The name is followed by a semi-colon (;
) to show that the statement stops here.To make use of this variable we can assign a value to it using the equals symbol (
=
).boolean isThisASushiCard; isThisASushiCard = true;
It is possible to combine the declaration and assignment into a single statement, called a definition, like so:
boolean isThisASushiCard = true;
Add the following code to a new file called
FirstBoolean.java
public class FirstBoolean{ public static void main(String[] args){ boolean isThisASushiCard = true; System.out.println("It is " + isThisASushiCard + " that this is a sushi card"); } }
Compile
FirstBoolean.java
and then runFirstBoolean
to see the output.- Try changing the value of the Boolean and the text to make a funny sentence and then save it and compile it and run it again.