Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Basics

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Basics

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 reserves a 32-bit, 4-byte box that is empty. x = 5 stores 5 in it as bits ending in 0101. x = 10 replaces the 5 with 10 in the same box. int x; x = 5; x = 10; (empty) 5 ...0000 0101 10 declaration: reserve 32 bits = 4 bytes assignment: fill reassignment: 5 replaced
A primitive is a fixed-size box. Declaring reserves it, assigning fills it, reassigning swaps the value.

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

TypeSizeHolds
byte1 bytewhole numbers, -128 to 127
short2 byteswhole numbers, up to about 32 thousand
int4 byteswhole numbers, up to about 2 billion
long8 byteswhole numbers, a huge range
float4 bytesdecimals, about 7 digits
double8 bytesdecimals, about 15 digits
char2 bytesone character, like 'E'
boolean1 bit of informationtrue 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_000 are only for reading; they don't print.
  • The L on 3_000_000_000L matters. A plain number you type is an int, and three billion is too big for one. Remove the L and javac says "integer number too large".
  • float needs an f at the end. char uses 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 int cuts 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

IdeaRemember
PrimitiveA fixed-size box; the value sits inside it
Declare / assign / reassignReserve, fill, replace
Default choicesint for whole numbers, double for decimals
SuffixesL for big longs, f for floats
DefaultsFields get 0 / 0.0 / false; locals must be set first
OverflowToo big for the box wraps or loses bits, silently

Objects are different: their size varies, and you'll meet them in object-oriented programming.

Keep going in Java · 6 of 79

Previous
Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
Next post
Java Strings and Text Blocks: Quotes, Concatenation and parseInt
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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.

On this page

  • Declare, assign, reassign
  • The eight primitive types
  • What an empty box holds
  • A fixed box can overflow
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.