Learn Java · lesson 3 of 61 · about 6 minutes
Text and True/False
Store the show's name in a String, a row letter in a char, and yes-or-no facts in a boolean.
What you'll learn
- Use String for text
- Use char for a single character
- Use boolean for true and false
Name the show
Numbers aren't enough. A booking needs the show's name, a row letter, and yes-or-no facts, like whether the box office is open. The type of a variable decides what it can hold:
| Type | Holds | Example |
|---|---|---|
| int | whole numbers | 42 |
| double | decimals | 12.35 |
| char | one character | 'C' |
| boolean | true or false | true |
| String | text | "Space Pirates" |
String starts with a capital S: it's a class, not one of the basic types. Text goes in double quotes, and a single char in single quotes.
A booking in five types
public class Main {
public static void main(String[] args) {
String show = "Space Pirates";
int seat = 7;
double price = 12.35;
char row = 'C';
boolean open = true;
System.out.println(show + ": row " + row + ", seat " + seat);
System.out.println("Price: " + price);
System.out.println("Open: " + open);
}
}Output
Space Pirates: row C, seat 7
Price: 12.35
Open: trueStrict on purpose
Java checks every value against its variable's type before the program runs. Text in an int, or a whole title in a char, and it won't compile.
That's why Java is trusted with big systems: a whole family of mistakes is caught before a single customer sees them.
Now try it yourself
This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.