An variable is a name associated with a value of a specific type that the program can query or modify.
Once you know how to compile and run, the next logical step is to learn how to store data.
If you come from languages like JavaScript or Python, here you’ll notice the first major shift in mindset. Java is a statically and strongly typed language.
What does this mean? It means that in Java, a variable is a box designed to hold only one type of thing.
- If you define a box for shoes (
int), you cannot try to put a shirt (String) into it. The compiler will yell at you before you even try to run the program.
This, which at first seems like an annoying limitation, is actually one of Java’s greatest advantages: the compiler protects you from making silly type errors.
What is a variable?
A variable is simply a space reserved in RAM to store a value that can change during program execution.
In Java, to use a variable, we must first declare it (tell the system: “reserve space for an integer”) and initialize it (give it a value).
// Declaration
int edad;
// Initialization
edad = 35;
// All in one line (most common)
int puntuacion = 100;The Primitive Types (The Big 8)
In Java, everything is an Object… except primitive types.
For performance reasons, Java maintains 8 basic data types that are not objects. They are pure values stored directly on the Stack memory, making them extremely fast to process.
They are divided into four groups: Integers, Floating-point numbers, Characters, and Booleans.
Integer Types
They are used to count things that do not have decimals. There are 4 types, depending on the size of the number you want to store.
| Type | Size | Approximate Range | Use |
|---|---|---|---|
byte | 8 bits | -128 to 127 | Raw binary data / Massive arrays. |
short | 16 bits | -32,768 to 32,767 | Rarely used nowadays. |
int | 32 bits | -2 billion to 2 billion | The standard. Use it by default. |
long | 64 bits | Enormous (9 quintillion…) | For database IDs, timestamps, etc. |
The default type is int. In Java, when you write a number 10 in your code, the compiler assumes it’s an int.
If you want to use a long, you must put an ‘L’ at the end of the number. Otherwise, Java will try to treat it as an int, and if it’s too large, it will cause an error.
int habitantes = 45000000;
long distanciaEstrella = 9460730472580800L; // Note the final 'L'Floating-Point Types
For numbers with a fractional part, the IEEE 754 standard is used.
| Type | Size | Precision | Use |
|---|---|---|---|
float | 32 bits | 6-7 decimal digits | 3D graphics, scientific data arrays. |
double | 64 bits | 15-16 decimal digits | The standard. Use for general math. |
As with integers, the king here is double. Java treats any decimal (3.14) as a double by default. If you want to force a float, you must use the ‘f’.
double pi = 3.1415926535;
float gravedad = 9.8f; // The 'f' is necessary; otherwise, a compilation error occursThe Precision Problem
Never, under any circumstances, use double or float for money. Computers store decimals in binary, and some numbers cannot be represented exactly (just like 1/3 in decimal is 0.33333…).
Try printing 0.1 + 0.2 in Java. It will give you 0.30000000000000004.
For exact financial calculations, a special class called BigDecimal is used.
Characters (char)
The char type is used to store a single character.
Unlike C++, where a char is 1 byte (ASCII), in Java a char occupies 2 bytes (16 bits) because Java uses Unicode (UTF-16) internally. This allows storing Chinese characters, emojis, etc.
They are always defined with single quotes ' '.
char letra = 'A';
char simbolo = '@';
char unicode = '\u0041'; // This is the letter 'A' in Unicode codeBe careful! 'A' (char) is not the same as "A" (String). Double quotes indicate a String of characters, which is an Object, not a primitive.
Booleans (boolean)
The simplest type. It only has two possible values: true or false.
It is the foundation of all control logic (conditionals and loops) that we will see later.
boolean esJavaDivertido = true;
boolean meGustaPHP = false;Interestingly, the memory size of a boolean is not strictly defined in the Java specification and depends on the JVM implementation, although logically it represents 1 bit of information.
Overflow
What happens if you try to store a number larger than what fits in the box?
Imagine a byte (max 127).
byte numero = 127;
numero++; // Add 1
System.out.println(numero); // Result?The result is not 128. The result is -128. When you add 1, the bits wrap around (like the odometer on an old car) and we go to the lowest negative value. This is called Integer Overflow and is a common source of bugs.