Variables and Types

Basic Java
≈ 4 min read

A variable is a name for a value

A program constantly needs to store data: a user's age, the number of attempts, a temperature, or a yes-or-no answer. Java uses variables for this. Every variable has a name and a type, and the type determines which values can be stored in it.

int age = 25;
double temperature = 21.5;
boolean online = true;
char grade = 'A';

In the first line, int is the type, age is the variable name, and 25 is its initial value. After the variable is created, its value can change, but its type stays the same.

int score = 10;
score = 15;
A local variable must be given a value before it is read. Java will not let you use a local variable if the compiler cannot be sure that a value has already been assigned to it by that point.

Primitive types

Java has eight primitive types for simple values. It is convenient to remember them by purpose:

  • byte, short, int, long — whole numbers;
  • float, double — numbers with a fractional part;
  • char — one 16-bit UTF-16 code unit;
  • boolean — the logical value true or false.

In ordinary code, int is used most often for whole numbers, and double for fractional numbers. If you need a long or float, you can show that explicitly with a suffix on the value.

int lessons = 12;
long views = 3_000_000_000L;
double progress = 72.5;
float ratio = 0.75F;
char firstLetter = 'J';
boolean completed = false;

Reference types

Not all data is primitive. Classes, interfaces, and arrays are reference types. A variable of a reference type stores a reference to an object or the special value null, which means that there is no reference.

String language = "Java";
String secondLanguage = null;

String looks almost as simple here as int, but it is a reference type. We will cover how strings, arrays, and objects work in their own topics; for now, the important thing is to distinguish the two large groups: primitive and reference types.

The type determines which values are allowed

Java checks types during compilation. If a variable is declared as a number, you cannot suddenly put text into it. This lets many mistakes be found before the program even starts.

int count = 10;
count = 12;
// count = "twelve"; // compilation error

Some numeric types are compatible with each other. A value of a narrower type can be converted automatically into a wider type when no data is lost. The reverse conversion must be written explicitly.

int count = 10;
long total = count;

double price = 19.95;
int wholePrice = (int) price;

After the last line, wholePrice will be 19: the fractional part is discarded by this conversion.

An explicit conversion does not guarantee that data will be preserved. When a wider type is converted to a narrower one, some information may be lost. That is why you should make such a conversion only when you understand the possible result.

var — a type without repeating the type name

For local variables, Java lets you write var when the type can be determined unambiguously from the initial value. This does not make Java dynamically typed: the compiler still determines one specific type, and that type does not change afterward.

var users = 12;
var course = "Java";

// users = "twelve"; // compilation error

A var declaration must have an initial value from which the compiler can infer the type. It is best used where the type remains obvious from the code even without repeating it.

Names should make code easier to read

A variable name should describe the meaning of the data rather than its type or position in the program. For ordinary variables, Java convention uses lowerCamelCase: the first word starts with a lowercase letter, and each following word starts with an uppercase letter.

int completedLessons = 7;
double averageScore = 91.5;
boolean premiumEnabled = true;

Now declarations such as int completedLessons = 7; can be read literally: “create a variable named completedLessons of type int and put the value 7 into it.” Operators, conditions, and loops will work with exactly these kinds of values and variables next.