Search

Variables and Data Types

Notice
The Java posts on this page are based on what I learned from the book Jump to Java, interpreted and organized in my own way. If you find any mistakes or inaccuracies, please feel free to let me know, I’ll review and correct them as soon as possible.
In this chapter, we’ll explore variables and data types in Java, step by step. Every program begins with handling data.
At the heart of storing, changing, and retrieving data lies the concept of a variable, and the data type defines what kind of value that variable can hold.
Understanding these two is not just about syntax, it’s about knowing how your program actually behaves inside memory.
Once you grasp this, you’ll begin to see how Java manages data behind the scenes.

Understanding Variables

What Is a Variable?

A variable is a named storage location in memory that holds a value.
Think of it as a labeled box where you can temporarily store data during program execution.
int age = 25;
Java
복사
Here, age is the name of the variable, and it stores the integer value 25.
Behind the scenes, Java allocates 4 bytes of memory to hold this number, and age acts as a human-readable name for that memory location.

Declaration and Initialization

In Java, defining a variable usually happens in two steps!
1.
Declaration – Telling the compiler that a variable exists.
int score;
Java
복사
2.
Initialization – Assigning an initial value to it.
score = 90;
Java
복사
You can also combine both steps like below
int score = 90;
Java
복사
If you try to use a variable before giving it a value, the compiler will throw an error, Java requires explicit initialization to ensure safety.

Variable Scope

A variable’s scope determines where in the program it can be accessed.
public class Main { public static void main(String[] args) { int a = 10; // local variable if (true) { int b = 20; // valid only inside this block System.out.println(a + b); // ✅ works } // System.out.println(b); // ❌ Error: b no longer exists here } }
Java
복사
Variables declared inside a block {} only exist while the block is running.
Once the block ends, those variables are automatically removed from memory.
Understanding scope helps you write clean, memory-efficient, and predictable code.

Understanding Data Types

Java’s Type System

Java is a statically typed language, meaning every variable must have a declared type.
Once a variable’s type is defined, it cannot hold a value of another type.
int number = 10; number = "Hello"; // ❌ Error : incompatible types
Java
복사
This strict type checking allows the compiler to catch errors early,
making Java programs more stable and less error-prone at runtime.

Two Main Categories of Data Types

Type
Description
Examples
Primitive Types
Store the actual value directly in memory
int, double, char, boolean
Reference Types
Store the memory address of an object
String, Array, Class, Interface
For example like this
int score = 90; // Primitive : stores the value 90 directly String name = "Hyunwoo"; // Reference : stores the address of the string object
Java
복사
A reference type variable doesn’t hold the value itself, it points to the location of an object in memory.
When copied, both variables can refer to the same object.

Primitive Data Types in Detail

Type
Size
Range
Example
byte
1 byte
-128 ~ 127
Small numerical data (e.g., sensor values)
short
2 bytes
about -32,000 ~ 32,000
Memory-efficient integer
int
4 bytes
about -2.1 billion ~ 2.1 billion
Most common integer type
long
8 bytes
very large integers
long population = 8000000000L;
float
4 bytes
~6 decimal digits
float pi = 3.14f;
double
8 bytes
~15 decimal digits
double pi = 3.14159265358979;
char
2 bytes
single Unicode character
char grade = 'A';
boolean
1 byte
true or false
boolean isLogin = true;

Assigning Values to Variables

In Java, the = operator doesn’t mean “equals.”
It means “assign the value on the right to the variable on the left.”
int x = 10; x = 20;
Java
복사
This statement updates the value stored in memory, x still refers to the same location, but now it holds 20.

Value Copy vs Reference Copy

int a = 10; int b = a; b = 20; System.out.println(a); // 10 — separate copy String s1 = "Hello"; String s2 = s1; s2 = "Hi"; System.out.println(s1); // Hello — reference updated
Java
복사
Primitive types copy the value itself, while reference types copy the address of the object.
This means if two reference variables point to the same object, changing that object’s contents through one variable will affect the other.

Constants (final)

To make a variable’s value unchangeable, use the final keyword!
final double PI = 3.14159;
Java
복사
Once declared as final, the value of PI can’t be reassigned.
Constants are typically written in uppercase letters for readability.

Commonly Used Data Types

In real-world development, a few types appear far more often than others
Type
Purpose
Example
int
Integer values, counters, loop indices
int count = 10;
double
Decimal values, measurements, calculations
double avg = 3.75;
boolean
Logical values for control flow
boolean isActive = true;
char
Single characters
char c = 'J';
Among reference types, the most frequently used one is String.
A String in Java is not just text, it’s a sequence of characters stored as an object.
String greeting = "Hello, Java!"; System.out.println(greeting.length()); // prints string length
Java
복사

User-Defined Data Types

Primitive types are useful, but they can’t represent complex real-world structures.
That’s where classes (user-defined data types) come in. A class lets you create your own data structure, defining both the data it holds and the behavior it can perform.
class Person { String name; int age; } public class Main { public static void main(String[] args) { Person p1 = new Person(); p1.name = "Hyunwoo"; p1.age = 29; System.out.println(p1.name + " is " + p1.age + " years old."); } }
Java
복사
Here, Person is a custom data type we defined ourselves.
It groups related properties together and can be extended with methods later.
In other words, a class is more than a container of data, it’s the foundation of Object-Oriented Programming (OOP), where real-world concepts are modeled in code.