java · level 1 · unit 2 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A ="button" class="pl-term" data-term="0" aria-expanded="false">variable is a box with a name on it.
A variable is a box your program keeps things in. You write a name on the label and put a inside. From then on, you say the name to get the value back.
In Java the label also says what kind of thing fits in the box. That is its type. String boxes hold text. boxes hold whole numbers, like 7 or 100.
So String name = "Sam"; means: make a String box called name and put "Sam" in it. To use the box, write its name with no quotes.
public class Main {
public static void main(String[] args) {
String name = "Sam";
int age = 9;
System.out.println(name);
System.out.println(age);
}
} Read the = as put into, not equals.
guess first
Quotes or no quotes? 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 pet = "Biscuit";
System.out.println("pet");
System.out.println(pet);
}
}
your turn
Your turn. Put yourself in the boxes.
Type your name between the quotes on line 3 and your age, as a whole number, on line 4. Then press run.
output
press run to see what happens
fix it
This one is broken. Can you fix it?
output
press run to see what happens
- Java says
cannot find symbol. Look at the name inside the brackets on line 4. - Is it spelled the same as the box made on line 3?
- The box is called snack. Add the missing c on line 4, then press run.
nailed it
you made the computer remember. :D
- A variable is a box with a name and a type.
Stringboxes hold text.intboxes hold whole numbers.- Spell the name the same way every time.
new words in your collection
- variable
- value
- type
- int
This lesson isn’t saved. A free account remembers every lesson you finish.