Understanding Java Primitive Types with Examples
Learn about Java primitive data types in detail. Explore the list of Java primitive types, their size, range, and examples with explanations.
When learning Java programming, one of the first things you encounter is Java data types. Data types define the kind of values a variable can hold. In simple terms, they act as blueprints that tell the compiler and programmer what type of data is stored in a variable.
In Java, data types are mainly classified into two categories:
Primitive types in Java
Reference types in Java
This blog focuses on Java primitive data types, their characteristics, examples, size, range, and the difference between primitive and reference types.
Primitive data types in Java are the most basic data types built into the Java language. They represent simple values and are not objects. Java provides eight primitive types.
These are:
byte
short
int
long
float
double
char
boolean
So, if you are wondering, how many primitive data types does Java have? The answer is 8.
They are fast and efficient, directly stored in memory.
They help define the foundation of Java programs.
They form the basis for variables, expressions, and calculations.
Let’s break down the Java data types list and understand the Java basic data types one by one.
1. byte
Size: 1 byte (8 bits)
Range: -128 to 127
Default Value: 0
Use Case: Efficient for saving memory in large arrays.
Example:
byte age = 25;
System.out.println(age);
2. short
Size: 2 bytes (16 bits)
Range: -32,768 to 32,767
Default Value: 0
Use Case: Saves memory compared to int when values are small.
Example:
short distance = 32000;
System.out.println(distance);
3. int
Size: 4 bytes (32 bits)
Range: -2,147,483,648 to 2,147,483,647
Default Value: 0
Use Case: Commonly used for integer values.
Example:
int salary = 50000;
System.out.println(salary);
4. long
Size: 8 bytes (64 bits)
Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Default Value: 0L
Use Case: For very large integer values.
Example:
long population = 7800000000L;
System.out.println(population);
5. float
Size: 4 bytes (32 bits)
Range: Approx ±3.40282347E+38F
Default Value: 0.0f
Use Case: For decimal values with less precision.
Example:
float price = 99.99f;
System.out.println(price);
6. double
Size: 8 bytes (64 bits)
Range: Approx ±1.79769313486231570E+308
Default Value: 0.0d
Use Case: For precise decimal values.
Example:
double pi = 3.141592653589793;
System.out.println(pi);
7. char
Size: 2 bytes (16 bits)
Range: 0 to 65,535 (Unicode)
Default Value: '\u0000'
Use Case: Represents single characters.
Example:
char grade = 'A';
System.out.println(grade);
8. boolean
Size: 1 bit (depends on JVM implementation)
Values: true or false
Default Value: false
Use Case: Logical decisions.
Example:
boolean isJavaFun = true;
System.out.println(isJavaFun);
In Java, variables are containers that hold data. When you declare a variable, you must also declare its data type. This ensures Java variable data types are strictly checked at compile time, reducing errors.
Example:
int age = 21;
float marks = 85.6f;
char grade = 'A';
boolean isPassed = true;
Java data types are classified into:
Primitive types in Java - Simple, predefined types like int, float, boolean, etc.
Reference types in Java - Objects and arrays that refer to memory addresses.
Difference between primitive and reference types in Java:
| Aspect | Primitive Types | Reference Types |
|---|---|---|
| Memory | Stored directly | Store memory address |
| Examples | int, float, boolean | Arrays, Strings, Objects |
| Default Value | 0, false | null |
| Performance | Fast | Comparatively slower |
This shows the core distinction between primitive and reference types Java programmers must understand.
Here’s a quick Java primitive data types size range table:
| Data Type | Size | Range |
| byte | 1 byte | -128 to 127 |
| short | 2 bytes | -32,768 to 32,767 |
| int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
| long | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| float | 4 bytes | ±3.40282347E+38F |
| double | 8 bytes | ±1.79769313486231570E+308 |
| char | 2 bytes | 0 to 65,535 |
| boolean | 1 bit | true / false |
Example: Bank Transaction Simulation
public class BankAccount {
public static void main(String[] args) {
long accountNumber = 1234567890L;
double balance = 1500.75;
boolean isActive = true;
char accountType = 'S'; // S for Savings
System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: $" + balance);
System.out.println("Active: " + isActive);
System.out.println("Account Type: " + accountType);
}
}
This Java datatype guide is designed for those who are starting out. If you are searching for a Java data types tutorial or Java types tutorial, keep in mind:
Java has primitive types and reference types.
Always use the right type for efficiency.
Understand Java data type examples before applying them in projects.
Choose the right data type - Use int instead of long if the value fits.
Use float/double carefully - Floating-point numbers can lead to precision errors.
Use boolean for conditions - Instead of using int for true/false.
Prefer primitives for performance - Avoid unnecessary wrappers unless required.
Understanding Java primitive data types is a fundamental step in mastering Java programming. With 8 built-in primitive types, Java provides a solid foundation for handling numbers, characters, and logical values.
We covered:
The list of Java primitive data types with examples
Size and range of primitive types
The difference between primitive and reference types in Java
Practical code examples
This Java datatype guide will help you strengthen your basics and write efficient Java programs. Whether you are exploring Java types, Java variable data types, or want to differentiate between primitive and reference types Java, this tutorial gives you clarity.
Whether you are a beginner learning from a Java data types tutorial or an advanced programmer revisiting the Java primitive data types size range, mastering these basics ensures you build a strong foundation in Java.
If you want to learn more about Java programming, explore advanced tutorials, or need expert IT training, feel free to get in touch with us:
📞 Call us: +91‑8587000904, 8587000906, 9643424141
🌐 Visit: www.cyberinfomines.com
📧 Email: vidhya.chandel@cyberinfomines.com
Your journey to becoming a skilled Java developer starts with the right guidance - Cyberinfomines is here to help you every step of the way.