Java Primitive Types and Memory: What int x = 5 Really Does
int x reserves a 4-byte box, x = 5 fills it. The eight primitive types with their sizes, the defaults Java gives fields, and why 127 + 1 in a byte prints -128.
What you will learn
- Explain declaration, assignment and reassignment as reserving and filling a fixed-size box
- Pick the right primitive among byte, short, int, long, float, double, char and boolean
- Know the default values of fields and why local variables have none
- Spot overflow and truncation when a value doesn't fit
Before you start
- Java variables, keywords and naming
Start with one tiny line, int x = 5;, and look at what happens behind it. Memory is physical: it's the RAM in your computer, billions of tiny boxes. A variable of a primitive type is one of those boxes, with a fixed size, and the value sits right inside it.
Declare, assign, reassign
int x; is the declaration. int tells Java how much memory to reserve: 32 bits, or 4 bytes. The box exists, but nothing is in it. x = 5; is the assignment: 5 is stored in the box as bits. x = 10; is reassignment: the 5 is replaced, because a box holds one value at a time.
package org.studyeasy.s02l04;
public class Ex01_Assign {
public static void main(String[] args) {
int x;
x = 5;
System.out.println("x is " + x);
int y = 7;
x = 10;
System.out.println("x is now " + x + ", y is " + y);
}
}
x is 5
x is now 10, y is 7
int y = 7; merges the two steps, declare and assign together. Both styles are fine. For the reassignment you don't write int again.
The eight primitive types
| Type | Size | Holds |
|---|---|---|
byte | 1 byte | whole numbers, -128 to 127 |
short | 2 bytes | whole numbers, up to about 32 thousand |
int | 4 bytes | whole numbers, up to about 2 billion |
long | 8 bytes | whole numbers, a huge range |
float | 4 bytes | decimals, about 7 digits |
double | 8 bytes | decimals, about 15 digits |
char | 2 bytes | one character, like 'E' |
boolean | 1 bit of information | true or false |
A bigger box holds a bigger range. As a rule, reach for int for whole numbers and double for decimals. (There's also void, the keyword for "no value", used on methods.)
package org.studyeasy.s02l04;
public class Ex02_Types {
public static void main(String[] args) {
byte level = 100;
short year = 2026;
int views = 1_500_000;
long total = 3_000_000_000L;
float price = 3.14f;
double pi = 3.14159;
char initial = 'E';
boolean passed = true;
System.out.println("byte " + level + ", short " + year);
System.out.println("int " + views + ", long " + total);
System.out.println("float " + price + ", double " + pi);
System.out.println("char " + initial + ", boolean " + passed);
}
}
byte 100, short 2026
int 1500000, long 3000000000
float 3.14, double 3.14159
char E, boolean true
- The underscores in
1_500_000are only for reading; they don't print. - The
Lon3_000_000_000Lmatters. A plain number you type is anint, and three billion is too big for one. Remove theLandjavacsays "integer number too large". floatneeds anfat the end.charuses single quotes.
What an empty box holds
package org.studyeasy.s02l04;
public class Ex03_Defaults {
static int count;
static double rate;
static boolean done;
static char letter;
public static void main(String[] args) {
System.out.println("int " + count + ", double " + rate);
System.out.println("boolean " + done + ", char code " + (int) letter);
}
}
int 0, double 0.0
boolean false, char code 0
These four are fields (declared in the class, outside main), and Java fills fields with a default: 0, 0.0, false, and the character with code 0. That character is invisible, so the program prints its number.
A local variable inside a method gets no default. Delete x = 5; from the first program and print x, and javac says the variable "might not have been initialized".
A fixed box can overflow
package org.studyeasy.s02l04;
public class Ex04_Limits {
public static void main(String[] args) {
byte level = 127;
level++;
System.out.println("127 plus 1 in a byte: " + level);
byte big = (byte) 999;
System.out.println("999 squeezed into a byte: " + big);
int cut = (int) 1.9;
System.out.println("1.9 stored in an int: " + cut);
}
}
127 plus 1 in a byte: -128
999 squeezed into a byte: -25
1.9 stored in an int: 1
No error, just strange numbers:
- A byte tops out at 127. Add 1 and it wraps to -128, like an odometer rolling over.
- 999 doesn't fit in a byte. The cast keeps only the low 8 bits, which happen to mean -25.
- Casting 1.9 to
intcuts off the decimal part. It isn't rounded.
Without the cast, byte big = 999; doesn't compile: "possible lossy conversion from int to byte". The compiler protects you until you force it. Choose a type big enough for your values. Casting gets its own article in Java type casting and conversions.
Recap
| Idea | Remember |
|---|---|
| Primitive | A fixed-size box; the value sits inside it |
| Declare / assign / reassign | Reserve, fill, replace |
| Default choices | int for whole numbers, double for decimals |
| Suffixes | L for big longs, f for floats |
| Defaults | Fields get 0 / 0.0 / false; locals must be set first |
| Overflow | Too big for the box wraps or loses bits, silently |
Objects are different: their size varies, and you'll meet them in object-oriented programming.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.
Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.
Java Hello World with Maven: What the JDK Does and What Maven Adds
javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.
Java Variables, Keywords and Naming Rules
A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.
