Chapter 07 of 57

Data Types in Java

Whenever we create a variable in Java, we need to tell Java what type of data that variable is going to store. This is where data types come in.

For example, if we want to store someone's age, we need a number. If we want to store their name, we need text. Java needs to know what kind of value we are storing so it can handle that data correctly.

That's why we write:

int age = 20;
String name = "John";

Here, int tells Java that age will store an integer, while String tells us that name stores text.

Why Do We Need Data Types?

Imagine you have several boxes. One box is meant for storing books, another for storing clothes, and another for storing shoes. You wouldn't randomly put everything into every box.

Variables work in a similar way. A data type tells Java what kind of value a variable is designed to store.

For example:

int age = 20;
double price = 99.99;
char grade = 'A';
boolean passed = true;

Each variable stores a different type of data.

Two Main Categories of Data Types

Java data types are broadly divided into two categories:

Data Types
├── Primitive Data Types
└── Non-Primitive Data Types

Let's understand both.

Primitive Data Types

Java has 8 primitive data types. These are the basic data types provided by the language.

They are:

  • byte

  • short

  • int

  • long

  • float

  • double

  • char

  • boolean

These types are used to store simple values such as numbers, characters, and true/false values.

Integer Types

The first four types are used for storing whole numbers:

byte, short, int, and long.

For example:

byte age = 25;
short year = 2026;
int population = 1000000;
long distance = 9000000000L;

The main difference between them is the amount of memory they use and the range of values they can store.

Among these, int is the most commonly used type for normal whole numbers.

Floating-Point Types

float and double are used for numbers that contain decimal values.

For example:

float temperature = 36.5f;
double price = 999.99;

In most situations, double is preferred when you need decimal values because it provides greater precision.

Character Type

The char data type stores a single character.

For example:

char grade = 'A';

Notice that the character is written inside single quotes.

A char can contain only one character:

char letter = 'A';

But this is not a char:

char name = "John"; // Error

because "John" contains multiple characters.

Boolean Type

The boolean data type stores only two possible values:

true
false

For example:

boolean isLoggedIn = true;
boolean isPassed = false;

This is useful whenever a program needs to represent something that has a yes/no or true/false condition.

Non-Primitive Data Types

The other category is non-primitive data types.

These include things such as:

  • String

  • Arrays

  • Classes

  • Interfaces

  • Enums

For example:

String name = "John";

Here, String is used to store text.

You will notice that String starts with a capital S, while primitive types such as int, double, and boolean are written in lowercase.

We will study String, arrays, classes, and other non-primitive types separately, so you don't need to memorize all their details right now.

Commonly Used Data Types

As a beginner, you will mostly work with these:

int age = 20;
double price = 499.99;
char grade = 'A';
boolean isStudent = true;
String name = "John";

If you understand what these five are used for, you already have a good starting point.

A Simple Example

Let's use different data types in one program:

class Main {
    public static void main(String[] args) {

        String name = "John";
        int age = 22;
        double height = 5.8;
        char grade = 'A';
        boolean isStudent = true;

        System.out.println(name);
        System.out.println(age);
        System.out.println(height);
        System.out.println(grade);
        System.out.println(isStudent);
    }
}

Output:

John
22
5.8
A
true

Notice how each variable has a different data type depending on the kind of information it stores.