Task 1 of 8 · 0 solved · 8 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

1.2 Variables — declaring a variable

Lesson

Variables and assignment

To compute anything, a program needs to store values. A variable is named storage for a value of a given type. This notebook is about declaring one.


Declaring a variable

A declaration gives a type, a name, and a starting value: int score = 10;. The = means assign — put the value into the variable — not equals.

int score = 10;

Worked example

int score = 10;
System.out.println(score);  // 10

The name score now stands for the value 10.


▶ Try it: run this, then change something and run it again.



Assemble a statement that declares score with the value 10.


Fill the blank so score holds the whole number 10.


What does this print?

int age = 30;
System.out.println(age);

Type exactly what this prints:

int x = 7;
System.out.println(x);

Click the operator that puts the value into the variable.