← All lessons
Java basics · Lesson 2 of 10

Variables and types

A variable is a named box that holds a value, so you can use it again later. In Java you say what kind of value the box holds when you make it. This line makes a box called score for whole numbers and puts 10 in it:

Java
int score = 10;
System.out.println(score);
score = 15;
System.out.println(score);
score = score + 5;
System.out.println(score);
Prints
10
15
20

Read = as becomes, not "equals". Java works out the right side first and then stores the result under the name on the left. That's why score = score + 5 makes sense: take the old score (15), add 5, and store 20 back in the box. You only write the type (int) once, when the variable is made; after that you just use the name.

Types

The kind of value is its type. These five cover almost everything in this course:

  • int: a whole number, like 42 or -7.
  • double: a number with a decimal point, like 3.5 or 0.1.
  • boolean: true or false, nothing else. Used for yes-or-no questions.
  • char: one character, in single quotes: 'A', '7', '?'.
  • String: text of any length, in double quotes: "Ana". It has a capital S because it's a class, not one of the built-in simple types.
Java
String name = "Ana";
int age = 12;
double height = 1.52;
boolean likesCats = true;
char grade = 'A';
System.out.println(name + " is " + age);
System.out.println(height);
System.out.println(likesCats);
System.out.println(grade);
Prints
Ana is 12
1.52
true
A

The + between text and a value joins them into one piece of text, so name + " is " + age becomes Ana is 12. Notice the spaces inside the quotes: Java joins exactly what you give it.

Java checks types before it runs anything. Putting text in an int box, like int age = "twelve";, is a compile error: the program won't start until you fix it. That strictness feels fussy at first, but it catches a lot of mistakes early.

Names

Names can use letters, digits and _, but can't start with a digit or contain spaces. Capitals matter, so score and Score are different variables. Java's habit is camelCase: start lower case and put a capital at each new word, like totalPrice or likesCats. Pick names that say what the value means.

A variable marked final can't be changed after it's set. It's for values that should stay put, like the number of days in a week, and those are often written in capitals:

Java
final int DAYS_IN_WEEK = 7;
int weeks = 3;
System.out.println(weeks * DAYS_IN_WEEK);
Prints
21

var can stand in for the type when Java can see it from the value: var count = 3; makes an int. It's handy later; for now, writing the type helps you learn what each value is.

Practice

What does this print?

int x = 3;
x = x + 4;
System.out.println(x);
More info

Quick reference

int x = 5;Make a whole-number variable called x holding 5.
x = x + 1;Work out the right side, then store it in x.
int / doubleWhole numbers / numbers with a decimal point.
booleantrue or false.
charOne character in single quotes: 'A'.
StringText in double quotes: "hello".
"a" + bJoins text and a value into one string.
finalThe variable can't be changed after it's set.

Related courses