What are the different data types in Java?
In Java, every variable must have a declared data type. Data types specify the size and type of values that can be stored in a variable. They are primarily divided into two main categories:
- Primitive Data Types
- Non-Primitive Data Types (Reference Types)
1. Primitive Data Types
These are the most basic data types built into the Java language. They are not objects and hold raw values directly in the memory they are allocated. There are 8 primitive data types, which can be categorized into four groups:
- Integer Types (for whole numbers)
- Floating-Point Types (for decimal numbers)
- Character Type (for single characters)
- Boolean Type (for true/false)
Let's explore each one.
Integer Types (Signed whole numbers)
These can hold negative, zero, and positive values.
| Data Type | Size | Range | Example |
|---|---|---|---|
byte |
8 bits | -128 to 127 | byte age = 25; |
short |
16 bits | -32,768 to 32,767 | short year = 2024; |
int |
32 bits | -2^31 to 2^31-1 (approx. -2.1B to 2.1B) | int population = 1410000000; |
long |
64 bits | -2^63 to 2^63-1 (a very large number) | long distanceToStar = 9460730472580L; |
Key Points:
intis the most commonly used integer type.- For
long, you must suffix the value with an 'L' or 'l' (uppercase 'L' is recommended for readability).
byte floor = 10;
short seatsInStadium = 45000;
int annualSalary = 75000;
long nationalDebt = 3141592653589L; // Notice the 'L' at the end
Floating-Point Types (Decimal numbers)
These are used for numbers with fractional parts.
| Data Type | Size | Range/Significance | Example |
|---|---|---|---|
float |
32 bits | ~6-7 significant decimal digits | float price = 15.99f; |
double |
64 bits | ~15 significant decimal digits (more precise) | double scientificValue = 1.23456789012345; |
Key Points:
doubleis more precise and is the default choice for decimal values.- For
float, you must suffix the value with an 'f' or 'F'.
float temperature = 36.6f; // Notice the 'f' at the end
double pi = 3.141592653589793;
double average = 85.5; // double is the default
Character Type
Used to store a single character.
| Data Type | Size | Range | Example |
|---|---|---|---|
char |
16 bits | 0 to 65,535 (Unicode) | char grade = 'A'; |
Key Points:
- The
chartype uses single quotes''. - It can store letters, digits, symbols, and even Unicode characters.
char initial = 'J';
char percentSign = '%';
char unicodeChar = '\u03A9'; // Represents the Greek letter Omega (Ω)
Boolean Type
Represents only two possible values: true or false.
| Data Type | Size | Values | Example |
|---|---|---|---|
boolean |
Not precisely defined | true or false |
boolean isJavaFun = true; |
Key Points:
- It is used for flags and conditional checks (e.g., in
ifstatements and loops).
boolean isRaining = false;
boolean isLoggedIn = true;
boolean hasPassedExam = (score >= 40); // Evaluates to true or false
2. Non-Primitive Data Types (Reference Types)
These types don't store the actual value directly. Instead, they store a reference (or memory address) to the location where the object's data is stored.
Characteristics:
- They start with a capital letter (by convention).
- They can be assigned
null, meaning they reference nothing. - Their default value is
null. - They are created using the
newkeyword (with some exceptions like String literals).
The main non-primitive types are:
- String: A sequence of characters. It's not a primitive type, but it's so fundamental that it's often used like one.
- Arrays: A collection of elements of the same data type.
- Classes: User-defined blueprints for objects (e.g.,
Scanner,Person,Car). - Interfaces: A reference type similar to a class that can contain only constants, method signatures, default methods, static methods, and nested types.
Examples of Non-Primitive Types
1. String
String greeting = "Hello, World!"; // Created using a String literal
String name = new String("Alice"); // Created using the 'new' keyword
String emptyString = null;
2. Arrays
// Declaring and initializing an array of integers (primitive)
int[] numbers = {10, 20, 30, 40};
// Declaring and initializing an array of Strings (non-primitive)
String[] cities = new String[3];
cities[0] = "London";
cities[1] = "Paris";
cities[2] = "New York";
3. Classes (User-Defined)
// Suppose we have a class named 'Car'
class Car {
String color;
String model;
}
// Creating an object (instance) of the Car class
Car myCar = new Car(); // 'myCar' is a reference variable
myCar.color = "Red";
myCar.model = "SUV";
Summary Table & Key Differences
| Feature | Primitive Types | Non-Primitive Types |
|---|---|---|
| Nature | Predefined in Java. | Created by the programmer (except String). |
| Stores | Actual value. | Memory address (reference to the object). |
| Size | Fixed, depends on the data type (e.g., int=4B). | Varies, depends on the object. |
| Default Value | e.g., 0, 0.0, false, \u0000. |
null. |
| Assignment | int a = 10; (stores 10 in a). |
String s = "Hi"; (stores a reference in s). |
| Passed to Method | By value (a copy is made). | By reference (the address is copied). |
| Examples | byte, short, int, long, float, double, char, boolean |
String, Arrays, Classes, Interfaces. |
Memory Visualization
This is a crucial concept for understanding the difference.
// PRIMITIVE
int a = 50;
int b = a; // The VALUE 50 is copied from a to b.
b = 100; // Changing b does NOT affect a.
System.out.println(a); // Output: 50
System.out.println(b); // Output: 100
// NON-PRIMITIVE (Reference)
int[] arr1 = {1, 2, 3};
int[] arr2 = arr1; // The REFERENCE (address) is copied, not the data.
arr2[0] = 99; // Changing arr2 also changes arr1!
System.out.println(arr1[0]); // Output: 99
System.out.println(arr2[0]); // Output: 99
In the non-primitive example, both arr1 and arr2 are pointing to the same array object in memory. This is why a change through one reference is visible through the other.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.
