DEV Community

Cover image for Java Data Types Explained — Simply, With Examples
DHANRAJ S
DHANRAJ S

Posted on

Java Data Types Explained — Simply, With Examples

Hey!

Before we start — let me ask you something.

You are filling out a form. One field asks for your name. Another asks for your age. Another asks for your phone number. Another asks — are you a student? Yes or No.

All four fields take different kinds of data. Name is text. Age is a number. Phone number is digits. Yes or No is just true or false.

Java works exactly the same way.

Every piece of data has a type. And Java makes you say that type clearly before you use it.

That is what data types are. And by the end of this blog — every Java data type will make complete sense.


1. What Is a Data Type?

A data type tells Java two things.

First — what kind of value this variable holds.
Second — how much memory to give it.

int age = 25;
Enter fullscreen mode Exit fullscreen mode

int — the type. Tells Java this is a whole number.
age — the name of the variable.
25 — the value.

Java reads this and thinks — "okay, age is a whole number. I will give it exactly enough memory for a whole number. Nothing more."

That is why data types matter. Java is efficient. It does not guess. You tell it exactly what you need.


2. Two Categories of Data Types

All data types in Java fall into two categories.

Primitive Data Types — basic, built-in types. They hold simple values directly.

Non-Primitive Data Types — complex types like String, Array, and Objects. They hold references to data.

Today we will focus on primitive types — because these are the foundation. Everything else builds on top of them.

Java has exactly 8 primitive data types. Let us go through each one.


3. The 8 Primitive Data Types — Overview

Type What it stores Size Example
byte Small whole number 1 byte 10
short Medium whole number 2 bytes 1500
int Whole number 4 bytes 50000
long Very large whole number 8 bytes 9999999999L
float Decimal number 4 bytes 3.14f
double Precise decimal number 8 bytes 3.14159265
char Single character 2 bytes 'A'
boolean True or false 1 bit true

Do not try to memorise all of this right now. Read through each one. It will click as you see examples.


4. Integer Types — Whole Numbers

These four types all store whole numbers. The difference is just how big the number can be.

byte

Stores numbers from -128 to 127.

byte temperature = 36;
byte smallNumber = -50;

System.out.println(temperature);  // 36
Enter fullscreen mode Exit fullscreen mode

Use byte when you know the number will be very small. Saves memory.

Quick question for you.

What do you think happens if you try to store 200 in a byte?

byte b = 200;  // Error!
Enter fullscreen mode Exit fullscreen mode

Java will give you a compile error. 200 is outside the range of -128 to 127. Java catches this mistake before your program even runs.


short

Stores numbers from -32,768 to 32,767.

short population = 25000;
short year = 2025;

System.out.println(year);  // 2025
Enter fullscreen mode Exit fullscreen mode

Bigger than byte. Still smaller than int. Use it when your number fits in this range and memory matters.


int

The most commonly used integer type. Stores numbers from about -2 billion to 2 billion.

int age = 25;
int salary = 75000;
int distance = 1000000;

System.out.println(salary);  // 75000
Enter fullscreen mode Exit fullscreen mode

When you are not sure which integer type to use — use int. It is the default choice for whole numbers in Java.


long

Stores very large numbers. When int is not big enough — use long.

long worldPopulation = 8000000000L;
long distanceToSun = 149600000000L;

System.out.println(worldPopulation);  // 8000000000
Enter fullscreen mode Exit fullscreen mode

Notice the L at the end of the number. That is required. Without it — Java treats the number as an int and it overflows.

Quick question for you.

Why do we need L at the end of a long value?

By default, Java reads any whole number as an int. If the number is too big for an int — Java gets confused before it even assigns it. The L tells Java — "this is a long, not an int." Now Java handles it correctly.


5. Decimal Types — Numbers With a Dot

float

Stores decimal numbers. Less precise. Uses 4 bytes.

float price = 99.99f;
float height = 5.8f;

System.out.println(price);  // 99.99
Enter fullscreen mode Exit fullscreen mode

Notice the f at the end. Same reason as L for long — Java reads decimal numbers as double by default. The f tells Java this is a float.

double

Stores decimal numbers. More precise than float. Uses 8 bytes.

double pi = 3.14159265358979;
double bankBalance = 150000.75;

System.out.println(pi);  // 3.14159265358979
Enter fullscreen mode Exit fullscreen mode

No letter needed at the end. double is Java's default for decimal numbers.

Quick question for you.

When should you use float over double?

When memory is tight — like in embedded systems or when storing millions of values. float uses half the memory of double. But for most everyday programs — just use double. The extra precision is worth it.


6. char — Single Character

char stores exactly one character. Always use single quotes.

char grade = 'A';
char initial = 'R';
char symbol = '@';

System.out.println(grade);   // A
System.out.println(symbol);  // @
Enter fullscreen mode Exit fullscreen mode

Always single quotes for char. Double quotes are for Strings.

char letter = 'A';    // correct
char letter = "A";    // wrong — this is a String, not a char
Enter fullscreen mode Exit fullscreen mode

Something interesting about char — every character has a number behind it called a Unicode value.

char letter = 'A';
System.out.println((int) letter);  // 65
Enter fullscreen mode Exit fullscreen mode

'A' is actually stored as the number 65. 'B' is 66. 'a' is 97.

You can even do arithmetic with chars.

char letter = 'A';
System.out.println((char)(letter + 1));  // B
Enter fullscreen mode Exit fullscreen mode

7. boolean — True or False

boolean stores only two values — true or false. Nothing else.

boolean isLoggedIn = true;
boolean hasDiscount = false;
boolean isAdult = true;

System.out.println(isLoggedIn);   // true
System.out.println(hasDiscount);  // false
Enter fullscreen mode Exit fullscreen mode

This is used everywhere — conditions, flags, checking states.

boolean isEven = (10 % 2 == 0);
System.out.println(isEven);  // true
Enter fullscreen mode Exit fullscreen mode

Quick question for you.

What do you think boolean isEven = (7 % 2 == 0) gives?

7 % 2 gives 1 — the remainder when 7 is divided by 2. 1 == 0 is false. So isEven is false. 7 is not even.


8. Putting It All Together — One Example

Let us use all 8 types in one program.

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

        byte age = 25;
        short year = 2025;
        int salary = 75000;
        long worldPopulation = 8000000000L;

        float height = 5.9f;
        double pi = 3.14159265358979;

        char grade = 'A';
        boolean isStudent = false;

        System.out.println("Age: " + age);
        System.out.println("Year: " + year);
        System.out.println("Salary: " + salary);
        System.out.println("World Population: " + worldPopulation);
        System.out.println("Height: " + height);
        System.out.println("Pi: " + pi);
        System.out.println("Grade: " + grade);
        System.out.println("Is Student: " + isStudent);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Age: 25
Year: 2025
Salary: 75000
World Population: 8000000000
Height: 5.9
Pi: 3.14159265358979
Grade: A
Is Student: false
Enter fullscreen mode Exit fullscreen mode

Eight types. One program. All working together.


9. Type Casting — Converting One Type to Another

Sometimes you need to convert one type to another.

There are two kinds.

Widening — automatic, safe

Going from a smaller type to a bigger type. Java does this automatically.

int num = 100;
long bigNum = num;       // int to long — automatic
double decimal = num;    // int to double — automatic

System.out.println(bigNum);    // 100
System.out.println(decimal);   // 100.0
Enter fullscreen mode Exit fullscreen mode

No data is lost. The smaller type fits perfectly inside the bigger one.

Narrowing — manual, you do it yourself

Going from a bigger type to a smaller type. You have to do this manually because data might be lost.

double price = 99.99;
int roundedPrice = (int) price;

System.out.println(roundedPrice);  // 99
Enter fullscreen mode Exit fullscreen mode

(int) tells Java — "I know what I am doing. Convert this double to an int."

The .99 is cut off. Not rounded. Just cut. That is narrowing casting — you lose the decimal part.

Quick question for you.

What do you think (int) 9.9 gives? 9 or 10?

It gives 9. Java does not round — it truncates. Just drops everything after the decimal point. Keep that in mind.


10. Common Mistakes Beginners Make

Forgetting L for long

long big = 9999999999;   // Error — Java reads this as int, too big
long big = 9999999999L;  // Correct
Enter fullscreen mode Exit fullscreen mode

Forgetting f for float

float price = 9.99;    // Error — Java reads this as double
float price = 9.99f;   // Correct
Enter fullscreen mode Exit fullscreen mode

Using double quotes for char

char letter = "A";  // Error — this is a String
char letter = 'A';  // Correct
Enter fullscreen mode Exit fullscreen mode

Storing a value outside the type's range

byte b = 200;   // Error — byte only goes up to 127
int i = 200;    // Correct
Enter fullscreen mode Exit fullscreen mode

Quick Summary — 8 Types in Plain English

  1. byte — tiny whole numbers. -128 to 127. Rare use.
  2. short — small whole numbers. -32,768 to 32,767. Rare use.
  3. int — your go-to whole number type. Use this by default.
  4. long — very large whole numbers. Add L at the end.
  5. float — decimal numbers, less precise. Add f at the end.
  6. double — decimal numbers, more precise. Your go-to decimal type.
  7. char — single character. Always use single quotes.
  8. boolean — only true or false. Used in conditions everywhere.

Data types are the very foundation of Java. Every variable you ever create will have one.

The good news is — in practice, you use int, double, boolean, and char for 90% of everything. The others exist for specific situations.

Try writing the full program from section 8 yourself. Change the values. Try storing a number outside the range. See the error Java gives you. That hands-on experience will make all of this stick.

If you have a question — drop it in the comments below.


Thanks for reading. Keep building.

Top comments (0)