Java Lesson 6 - Data Types | Dataplexa

Data Types in Java

In real-world applications, programs work with different kinds of information. Some data represents numbers, some represents text, and some represents true or false conditions.

Java needs to know exactly what kind of data it is handling. That is why Java uses data types. They tell the program how much memory to allocate and how the data should behave.


Why Data Types Matter

Java is a strongly typed language. This means every variable must have a defined data type before it can store any value.

This strictness helps Java catch errors early, improves performance, and makes programs more reliable in large systems.

In enterprise applications like banking or healthcare systems, this reliability becomes extremely important.


Two Main Categories of Data Types

Java data types are divided into two major categories. Understanding this division helps you choose the right type for the right situation.

  • Primitive Data Types – store simple values
  • Non-Primitive Data Types – store complex objects and references

Primitive Data Types

Primitive data types store simple, single values. They are fast, memory-efficient, and widely used in core logic.

Java provides eight primitive data types. The most commonly used ones are listed below.

  • int – stores whole numbers
  • double – stores decimal values
  • char – stores a single character
  • boolean – stores true or false

Example: Using Primitive Data Types

Let’s look at a simple example that stores different types of information about a student.

public class DataTypesExample {

    public static void main(String[] args) {

        int age = 21;
        double marks = 89.5;
        char grade = 'A';
        boolean passed = true;

        System.out.println(age);
        System.out.println(marks);
        System.out.println(grade);
        System.out.println(passed);
    }
}

Each variable stores a different kind of value, and Java clearly understands how to handle each one.


Non-Primitive Data Types

Non-primitive data types are used to store more complex data. Instead of holding values directly, they usually store references to objects.

Some commonly used non-primitive types include:

  • String – stores text
  • Arrays – store multiple values
  • Classes – represent real-world entities

You will explore these in detail in upcoming lessons.


How Java Chooses Memory

Each data type occupies a specific amount of memory. For example, an int uses less memory than a double.

Choosing the correct data type improves performance and avoids unnecessary memory usage, especially in large-scale applications.


What You Learned in This Lesson

  • Why Java requires data types
  • The difference between primitive and non-primitive types
  • How data types affect memory and performance
  • How to use basic data types in a real program

In the next lesson, you will learn how to store and manipulate data using variables, which build directly on these concepts.