Java Data Types – Complete Guide to Primitive and Non-Primitive Types with Examples

java data types

If you are a beginner in Java Programming, one of the first fundamental concepts you will learn about is Java Data Types. The data types define the kind of value a variable can hold, the memory size it occupies, and the operations you can perform on it. To avoid the constant errors and confusions that will arise throughout your coding journey in Java, you need a good knowledge of primitive data types in Java, as well as primitive types.

This guide will teach you all the fundamental facts about Java data types. It will provide examples, tables, and real-world examples to help you not only understand Java data types but also master them!

What are Data Types in Java?

  • A data type in Java describes:
  • What type of value a variable can store
  • The memory size it requires to hold that value
  • What operations you can perform on the data

In simplify descriptive words:

👉 If variables are seen as containers, data types are the labels on the container that tells Java what type of data it can hold

Java data types can be classified into two broad groups:

  • Primitive Data Types in Java (Basic building blocks)
  • Non-Primitive (Reference) Data Types in Java (Complex/Custom types)
java data types
Data Types in Java

Primitive Data Types in Java

Primitive types are the most basic, pre defined types in the Java language. They are called “primitive” because they represent the simplest values: numbers, characters, and booleans.

There are 8 primitive data types in Java:

Data Type

Size (bits) Range Default Value Example

byte

8 -128 to 127 0 byte b = 100;

short

16 -32,768 to 32,767 0 short s = 5000;

int

32 -2^31 to 2^31-1 0 int age = 25;

long

64 -2^63 to 2^63-1 0L

long phone = 9876543210L;

float 32 ~7 decimal digits 0.0f

float pi = 3.14f;

double 64 ~15 decimal digits 0.0d

double salary = 50000.99;

char

16 (Unicode) ‘\u0000’ to ‘\uffff’ ‘\u0000’

char grade = ‘A’;

boolean 1 (JVM-dependent) true/false false

boolean isJavaFun = true;

Example Code:

public class PrimitiveExample {
    public static void main(String[] args) {
        int age = 30;
        double salary = 55000.75;
        char grade = 'A';
        boolean isJavaFun = true;

        System.out.println("Age: " + age);
        System.out.println("Salary: " + salary);
        System.out.println("Grade: " + grade);
        System.out.println("Is Java Fun? " + isJavaFun);
    }
}

Non-Primitive (Reference) Data Types in Java

Non-primitive data types are different from primitive types, as non-primitive types reference an object which references the actual location in memory. Non-primitive types are values which consist of a variety of complex values, and obviously have methods to manipulate values.

Examples:

  • String → “Hello Java”
  • Arrays → collection of elements
  • Classes & Objects → custom blueprints
  • Interfaces → abstract types

Example:

public class NonPrimitiveExample {
    public static void main(String[] args) {
        String name = "Java Developer";
        int[] marks = {90, 85, 88};

        System.out.println("Name: " + name);
        System.out.println("First Mark: " + marks[0]);
    }
}

Difference Between Primitive and Non-Primitive Data Types

Feature Primitive Non-Primitive
Storage Directly store values Store references (addresses)
Size Fixed (depends on type) Not fixed (depends on object)
Examples int, float, boolean String, Arrays, Objects
Default Values 0, false, null-char null
Methods No built-in methods Have methods (e.g., String.length())

Memory Allocation in Java

  • Primitive data types in Java → stored on stack memory for quick access.
  • Non-primitive types → stored on heap memory with references on stack.

👉 Understanding this helps in performance optimization and avoiding NullPointerExceptions.

Wrapper Classes in Java (Autoboxing & Unboxing)

java data types
Wrapper Class in java

Java provides wrapper classes that convert primitive types into objects. For example:

  • int → Integer
  • double → Double
  • char → Character

Example:

public class WrapperDemo {
    public static void main(String[] args) {
        int num = 10;
        Integer obj = num; // Autoboxing
        int val = obj;     // Unboxing

        System.out.println("Wrapped: " + obj);
        System.out.println("Unwrapped: " + val);
    }
}

Real-World Examples of Java Data Types

java data types
Real-World Examples of Java Data Types
  • Banking Application → double balance, long accountNumber
  • E-commerce → int productId, String productName, float price
  • Gaming → boolean isGameOver, char playerSymbol

👉 Without choosing correct Java data types, you risk memory waste or data loss.

✅ Final Thoughts

If mastering Java data types is important to you, you aren’t going to be a good Java developer. Every Java program you write will depend on both primitive data types in Java, such as int and boolean, and classes, such as String and Arrays. As a result, it is important to know how data is stored and manipulated.

To recap:

  • Primitive data types in Java = simple values (fast, memory-efficient)
  • Non-primitive data types in Java = objects with methods and flexibility.
  • After choosing the proper data type, performance can improve, bugs decrease, and scalability increases.

While this is a good start for going from a novice to a professional developer, this is only the beginning, and If done properly, a good Java course will encourage you to not only become more aware of Java data types but to learn more advanced topics, such as collections, generics, frameworks, and much more. When you’re done reading this article, it would be helpful to remember, if you want to go from novice to expert more time and hands-on experience, a good Java course, and thoughts like these can help you make that move.

So when you declare your next variable, remember the data type is not just the type; it is the basis upon which you’ll build everything in the code! 🚀

Related Links

Previous Article

What is Linear Search and Binary Search (2025 Guide): Search Algorithms Explained, Code in Python & Java, and More

Next Article

What is Div Tag in HTML: Master the Meaning, Practical Examples & Smart Centering Tricks 🎯 [2025]

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨