Data Types in Java: Primitive & Non-Primitive Data Types
By Rohan Vats
Updated on May 26, 2025 | 13 min read | 19.92K+ views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on May 26, 2025 | 13 min read | 19.92K+ views
Share:
Table of Contents
Latest Update: Introduced in Java 24, JEP 450 reduces the size of object headers on 64-bit systems. This enhancement improves memory efficiency and supports better data locality, making it particularly useful for applications dealing with large data sets or high-throughput systems, such as AI workloads.
Data types in Java are the building blocks of any Java program, defining the kind of data a variable can hold. They are crucial because Java is a strongly typed language, meaning every variable must have a defined type, ensuring type safety, efficient memory usage, and fewer runtime errors.
In this blog, we’ll explore Java’s primitive data types and non-primitive data types, covering their features, memory use, value ranges, and best use cases.
Confused about which Java data type to use and when? Learn through upGrad’s Online Software Development Courses doing 5+ capstone projects with industry experts from top universities. Enroll today!
Here is a quick comparison of Java's primitive and non-primitive data types by type, size, and purpose.
Type |
Size |
Default Value |
Range / Description |
byte | 1 byte (8 bits) | 0 | -128 to 127 |
short | 2 bytes (16 bits) | 0 | -32,768 to 32,767 |
int | 4 bytes (32 bits) | 0 | -2,147,483,648 to 2,147,483,647 |
long | 8 bytes (64 bits) | 0L | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 bytes (32 bits) | 0.0f | ~±3.4 × 10^38 (7 digits precision) |
double | 8 bytes (64 bits) | 0.0d | ~±1.7 × 10^308 (15 digits precision) |
char | 2 bytes (16 bits) | '\u0000' | 0 to 65,535 (stores a single Unicode character) |
boolean | 1 bit (theoretically) | false | true or false |
String | Varies | null | Sequence of characters; predefined class in Java |
Array | Varies | null | Collection of elements of the same type |
Class | Varies | null | User-defined blueprint for objects |
Interface | Varies | null | Abstract type used to specify method signatures |
To meet the rising industry demand for skilled professionals, check out the following top courses that will help you sharpen your skills and get job-ready in 2025.
With an understanding of Java data types, it’s time to look at their importance in creating robust, efficient, and error-free programs.
Data types in Java determine the type of data that can be stored and the operations that can be performed on that data. Understanding data types is essential for writing efficient, error-free, and optimized Java programs.
Here’s a breakdown of their significance:
Choosing the right data types is just one part of writing efficient code. Learn how top developers structure memory and optimize performance in real-world projects—join upGrad’s Online AI-Drive Full Stack Development Bootcamp Program today.
Also Read: Exploring Java Architecture: A Guide to Java's Core, JVM and JDK Architecture
To better understand Java’s approach to memory management and type safety, let’s begin with an overview of its most fundamental category: the primitive data types.
In Java, you use primitive data types to store simple values like numbers, characters, and boolean logic directly in memory. These types are predefined by the language and offer efficient performance without the overhead of objects. You'll find eight primitives in total, each with a specific size and purpose.
Below are the details of primitive data types with their names, sizes, and typical use cases.
The byte data type is the smallest integer type in Java, occupying just 1 byte (8 bits) of memory. It stores whole numbers from -128 to 127, making it ideal for memory-constrained applications or when working with raw binary data such as file I/O or network streams.
byte n1 = 98;
System.out.println(n1); // Output: 98
Ready to learn Java from scratch? Enroll in upGrad’s free Core Java Basics course. Build a solid understanding of Java, gain practical coding experience, and master key development concepts. Perfect for beginners or those looking to level up their skills. Join today!
The short data type stores whole numbers from -32,768 to 32,767 using 2 bytes (16 bits). It is larger than byte but still smaller than int, offering a balance between memory efficiency and numeric range.
Note: Using values outside this range will trigger a compilation error.
short n1 = 9876;
System.out.println(n1); // Output: 9876
int is the default integer type in Java, storing values between -2,147,483,648 and 2,147,483,647 using 4 bytes (32 bits). It is widely used for integer operations due to its range and performance.
Common Use: Integer arithmetic, counters, indexing, etc.
int n1 = 987654;
System.out.println(n1); // Output: 987654
The long data type is used when an int is insufficient. It stores values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 using 8 bytes (64 bits).
Important: Always suffix the value with L or l to distinguish it from an int.
long n1 = 987654321098765L;
System.out.println(n1); // Output: 987654321098765
Also Read: Types of Variables in Java: Java Variables Explained
The float type is a single-precision 32-bit IEEE 754 floating-point type. It can store fractional numbers with up to 7 digits of precision, and supports values in the range ~3.4e−38 to 3.4e+38.
Note: Always suffix with f or F; otherwise, it’s treated as a double.
float n = 98.76f;
System.out.println(n); // Output: 98.76
double is a double-precision 64-bit IEEE 754 floating-point type. It supports about 15 decimal digits of precision and is the default for decimal numbers in Java.
Example Comparison:
public class Test {
public static void main(String[] args) {
float a1 = 1.f / 70;
double a2 = 1.d / 70;
float b1 = 0;
double b2 = 0;
float f1 = 1f;
double f2 = 1d;
for (int i = 0; i < 490; i++) {
b1 += a1;
b2 += a2;
if (i > 1 && i <= 50) {
f1 *= i;
f2 *= i;
}
}
System.out.println(b1); // Output: 6.9999795 (float)
System.out.println(b2); // Output: 6.999999999999978 (double)
System.out.println(f1); // Output: Infinity (float overflow)
System.out.println(f2); // Output: 3.0414093201713376E64 (double)
}
}
Why this matters: When precision or range is critical, always prefer double over float.
The boolean type holds only two values: true or false, using just 1 bit of memory conceptually (though usually a byte in practice). It’s typically used for:
Valid:
boolean x = true;
boolean y = 5 < 10;
Invalid (causes compilation error):
boolean x = "true"; // Invalid
boolean flag = true;
boolean b1 = false;
System.out.println(flag); // Output: true
System.out.println(b1); // Output: false
The char type stores a single 16-bit Unicode character, supporting values from '\u0000' (0) to '\uffff' (65,535). It can hold any character from the Unicode character set, including special characters, alphabets, and symbols.
Numeric Casting: A char can be cast to an int to get its ASCII/Unicode value, and vice versa.
char c1 = 'a'; // Output: a
char c2 = 66; // Output: B (ASCII of 66)
System.out.println((int)c1); // Output: 97
Because of these internal conversions and broader ranges, developers commonly use int for integers and double for decimals—ensuring compatibility, avoiding unexpected overflows, and minimizing type casting issues.
Also Read: 17 Interesting Java Project Ideas & Topics For Beginners
Next, let’s explore how Java handles more complex data structures through Non-Primitive Data Types like arrays, classes, and interfaces.
Non-primitive data types, also known as reference types, are a key feature in Java’s object-oriented architecture. Unlike primitive data types that store actual values in memory, non-primitive types store references (or memory addresses) that point to the actual objects in the heap memory. These types allow Java to represent complex data structures and behaviors beyond simple values.
1. Classes and Objects
Classes are blueprints from which objects (instances) are created. Each object stores its own state (fields) and behavior (methods).
class Bird {
String species;
Bird(String species) {
this.species = species;
}
}
Bird bird1 = new Bird("Pigeon");
System.out.println(bird1.species); // Output: Pigeon
Sharpen your skills in object oriented programming and machine learning with upGrad’s Executive Diploma in Machine Learning and AI. Gain practical expertise and prepare for high-demand roles like AI Engineer and Machine Learning Specialist. Start learning today!
2. String
String is a predefined non-primitive type in Java, and while technically a class, it’s often treated as a “special” data type due to its frequent use and built-in language support.
String greeting = "Hey There";
System.out.println(greeting); // Output: Hey There
Despite being non-primitive, many developers refer to it as Java’s “ninth primitive” due to its syntactic ease and special treatment by the JVM.
3. Arrays
Arrays in Java are objects that store multiple values of the same data type in contiguous memory. Whether it's an array of primitives or objects, arrays are always reference types.
int[] numbers = {1, 2, 3};
String[] names = {"Alice", "Bob"};
Arrays provide length information and can be multidimensional, allowing structured data storage.
Also Read: Array in Java: Types, Operations, Pros & Cons
4. Interfaces
Interfaces define abstract contracts that classes can implement. They do not store data themselves but define method signatures for implementing classes.
interface Animal {
void speak();
}
Interfaces are reference types and enable polymorphism, abstraction, and multiple inheritance.
Also Read: 20 Most Popular Programming Languages in 2025
Also Read: Learn 50 Java Projects With Source Code (2025 Edition)
The following table highlights the key differences between primitive and non-primitive data types in Java, helping you understand how they differ in structure, behavior, and usage.
Feature |
Primitive Types |
Non-Primitive Types |
Defined By | Java (predefined) | Programmer or Java library |
Value Stored | Actual value | Reference to object in memory |
Default Value | Type-specific (e.g., 0, false) | null |
Supports Methods | No | Yes |
Case Convention | Lowercase (e.g., int, boolean) | Uppercase (e.g., String, Bird) |
Memory Allocation | Stack (value-based) | Stack + Heap (reference + object) |
Size | Varies by type | Same size for reference; object size varies |
Advance your career in software development with upGrad’s Professional Certificate Program in Cloud Computing and DevOps! Gain practical experience with AWS, Azure, and Google Cloud, while mastering DevOps tools like Docker and Kubernetes. Work on real-world projects to get job-ready!
The range of an integer-based primitive type (like byte, short, int) is determined by its size in bits.
Java uses the IEEE 754 standard for floating-point arithmetic:
So:
With a solid understanding of data types in Java, explore other core Java concepts with upGrad and advance your career in software development.
Also Read: How to Code, Compile, and Run Java Projects in 2025
Data types in Java define the kind of data a variable can hold, ensuring proper memory usage and operations. Java offers two main categories: primitive types (like int, char, boolean) for basic values, and non-primitive types (such as String and Arrays) for objects and collections. Understanding these types helps you write efficient, type-safe code with proper memory management.
If you're looking to strengthen your Java skills further and bridge any gaps in your knowledge, upGrad’s Software Development courses offer hands-on experience, expert mentorship, and 1:1 guidance.
Here are some additional courses to further support your advancement in Java development.
Confused about your career in software development? Contact upGrad’s expert career counselors, who will guide you based on your goals. You can also visit a nearby upGrad offline center to explore course options, get hands-on experience, and speak directly with mentors!
Unlock your potential with our free Software Development courses and start building essential skills for a successful tech career!
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
References:
https://www.tech-channels.com/techchannels-blog-news/java-turns-30-with-enhanced-data-handling-capabilities
https://codedecodeacademy.com/understanding-java-23-features-in-2025-a-comprehensive-guide/
https://bulldogjob.com/readme/every-new-feature-in-java-24
408 articles published
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources