java · level 1 · unit 3 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Quotes turn a number into text.
5 is a number. "5" in quotes is a string that happens to look like one, and Java treats them very differently. 5 + 3 adds up to 8, but "5" + "3" joins into 53.
Join a string and a number and the number becomes text too, so "Apples: " + 5 gives Apples: 5. Whole numbers go in int boxes. Numbers with a decimal point, like 0.75, go in boxes.
Each box only takes its own type. Put text in an int box and Java stops with , which means the value does not fit the label.
public class Main {
public static void main(String[] args) {
int apples = 5;
double price = 0.75;
System.out.println(5 + 3);
System.out.println("5" + "3");
System.out.println("Apples: " + apples);
System.out.println(price);
}
} Put 2 in a double box and it shows as 2.0, with its decimal point.
guess first
Goals as text. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
public class Main {
public static void main(String[] args) {
String homeGoals = "2";
String awayGoals = "1";
System.out.println(homeGoals + awayGoals);
}
}
your turn
Your turn. How old will you be next year?
Type your age as a whole number, with no quotes, then press run.
output
press run to see what happens
fix it
These cookies will not add up. Can you fix it?
output
press run to see what happens
- Java says
incompatible typeson line 3. What is different about the value 4? - The quotes make
"4"a string, and a string does not fit in anintbox. - Remove the quotes around 4 on line 3, then press run.
nailed it
you can tell text and numbers apart. :D
- Quotes make a value text, even if it looks like a number.
intholds whole numbers.doubleholds numbers with decimals.- A string plus a number joins them into new text.
new words in your collection
- double
- incompatible types
This lesson isn’t saved. A free account remembers every lesson you finish.