java · level 1 · unit 2 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Put something new in a box and the old thing is gone.
A box holds one value at a time. Put a new value in and the old one is thrown away. We say you it. Write the type only once, when you first make the box.
You can copy one box into another, like String order = drink;. You can also strings with +, which sticks them end to end, so mind the spaces.
A box can even use its own value. score = score + 1; takes what is in score, adds one and puts the answer back in.
public class Main {
public static void main(String[] args) {
String drink = "tea";
drink = "coffee";
System.out.println("I would like " + drink);
int score = 5;
score = score + 1;
System.out.println(score);
}
} When you join strings, put any spaces you need inside the quotes.
guess first
Copy, then change. 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 drink = "tea";
String order = drink;
drink = "coffee";
System.out.println(order);
}
}
your turn
Your turn. Change the weather.
Type tomorrow's weather between the quotes on line 5, then press run to see the box change.
output
press run to see what happens
fix it
Losing a life crashed the game. Can you fix it?
output
press run to see what happens
- Java says
livesis already defined. Which two lines both start withint? - Line 5 starts with a type, so it tries to make a second box. You only want a new value in the old one.
- Delete
intat the start of line 5, so it sayslives = 2;, then press run.
nailed it
your boxes can change now. :D
- A new value overwrites the old one.
- Write the type only once, when you make the box.
+joins strings. Put spaces inside the quotes.
new words in your collection
- overwrite
- join
This lesson isn’t saved. A free account remembers every lesson you finish.