Java Arena
Step 2 of 4

String variables

A variable is a named place that holds a value. String message = scanner.nextLine(); in the last step created one. Variables can also get their value straight from the code:

String food = "pizza";
System.out.println(food);
System.out.println("food");
Output
pizza
food

The line String food = "pizza"; has three parts:

  • String is the variable's type: what kind of value it holds. A String holds text.
  • food is the variable's name. You choose it.
  • = "pizza" gives it its value.

After that, the name stands for the value. That's why System.out.println(food) prints pizza, while System.out.println("food"), with quotes, prints the word food itself. Quotes mean "this exact text"; no quotes mean "the value of this variable".

A variable has to be created before it's used, and Java checks the name's spelling and capitals: Food and food are two different names.

Your task

Challenge 1 of 3

Create a String variable called planet that holds the text Mars, and print the variable (not the text in quotes).

Challenge 2

This program should print the values of its two variables, but it prints their names instead. Fix it so it prints:

Helsinki
Finland

Challenge 3

Create three String variables: breakfast holding oatmeal, lunch holding soup and dinner holding pasta. Then print the variables in this order: dinner, breakfast, lunch.

pasta
oatmeal
soup