Data Types in Java: Each Types Explained with Examples

Author: Ritika
0

 

If you are learning Java in your first or second year of B.Tech, this is one topic you cannot skip — Data Types in Java.

 

I still remember when I first started coding in Java in my 1st semester. I thought data types were just theory for exams. But when I started building projects and solving coding questions on platforms like HackerRank, I realized… data types actually decide how your program behaves.

 

So in this blog, I’ll explain data types in Java in a very simple and practical way — just like I would explain to my juniors in college.

 

Data Types in Java: Each Types Explained with Examples
(toc) #title=(Table of Content)

 

Introduction

In simple words, data types tell Java what kind of data we are storing in a variable.

For example:

  • Your age → number → int
  • Your percentage → decimal number → float or double
  • Your name → text → String
  • True/False value → boolean

When we write:

int age = 20;

 

We are telling Java:

“Bro, I’m storing a number here, so treat it like an integer.”

Without data types, Java wouldn’t know how much memory to use or how to handle that value.

In Java, data types are mainly divided into two categories:

  • Primitive Data Types
  • Non-Primitive Data Types

Let’s understand both properly.

 

Primitive Data Types

Primitive data types are the most basic data types in Java.

They store simple values directly in memory. There are 8 primitive data types in Java.

 

List of Primitive Data Types

 

Data Type Size Example Used For
byte 1 byte byte b = 10; Small numbers
short 2 bytes short s = 200; Medium numbers
int 4 bytes int a = 100; Whole numbers
long 8 bytes long l = 10000L; Large numbers
float 4 bytes float f = 10.5f; Decimal numbers
double 8 bytes double d = 10.555; Large decimal numbers
char 2 bytes char c = 'A'; Single character
boolean 1 bit boolean flag = true; True/False

 

Now let’s understand them in a practical way.

 

Integer Types (byte, short, int, long)

These are used to store whole numbers (no decimal).

In most of our college programs, we mostly use:

  • int → For marks, roll number, age
  • long → For very large numbers

Example:

int marks = 85;
long population = 1400000000L;

Honestly speaking, in most projects and assignments, int is enough. I have rarely used byte or short in real programs. They are mostly for memory optimization or interviews.

 

Decimal Types (float and double)

Used to store decimal values like percentages or CGPA.

 

float percentage = 89.5f;
double cgpa = 8.10;

 

Many students get confused here.

 

👉 Why do we write f after float?

Because by default, Java treats decimal numbers as double. So if you want float, you must add f.

In real projects, I mostly use double because it is more precise.

 

Character Type (char)

Used to store a single character.

char grade = 'A';

Important:

  • Single quotes → 'A'
  • Double quotes → "A" (This is String, not char)

This mistake is very common in practical exams.

 

Boolean Type

Stores only two values:

  • true
  • false
boolean isPassed = true;

This is heavily used in:

  • Conditions
  • Login systems
  • Form validation
  • Loops

In my mini project, I used boolean to check if a user is logged in or not.

 

Non-Primitive Data Types

Now this is where things become interesting.

Non-primitive data types don’t store simple values directly. They store reference (address) of the object.

These are also called Reference Types.

Main non-primitive types:

  • String
  • Array
  • Class
  • Object
  • Interface

 

String

Technically, String is not primitive. It is a class in Java.

String name = "Ankit";

Even though we use it like a normal data type, internally it works differently.

In simple words:

  • int stores value directly.
  • String stores reference to an object.

When I first learned this, I was honestly confused. But later during OOPS concepts, it became clear.

 

Array

Array stores multiple values of the same type.

int[] marks = {80, 85, 90};

In college, arrays are very important for:

  • DSA
  • Practical exams
  • Lab tests
  • Competitive coding

Arrays are non-primitive because they are objects in Java.

 

Class and Object

Everything in Java revolves around classes and objects.

class Student {
    int roll;
}

Here, Student is a class (non-primitive type).

Student s1 = new Student();

s1 stores reference to the object.

 

Difference Between Primitive and Non-Primitive

 

Feature Primitive Non-Primitive
Stores Actual value Reference (address)
Memory Less More
Size Fixed Not fixed
Example int, double String, Array
Can be null? No Yes

 

Many students get confused in exams when asked this difference. Just remember:

Primitive → Direct value
Non-Primitive → Reference to object

 

Type Conversion in Java

Now comes an important concept — Type Conversion.

Sometimes we need to convert one data type into another.

There are two types:

  • Implicit Type Casting (Widening)
  • Explicit Type Casting (Narrowing)

 

Implicit Type Casting (Widening)

This happens automatically.

When converting:

smaller type → larger type

int a = 10;
double b = a;
  • int → double
  • No data loss
  • Java does automatically

This is safe.

 

Explicit Type Casting (Narrowing)

When converting:

larger type → smaller type


double d = 10.5;
int a = (int) d;

 

Output will be:

10

Decimal part is removed.

Here we must use:

(int)

Otherwise compiler error.

When I first saw this, I was shocked that 10.5 becomes 10. But yes, Java removes decimal part.

 

Practical Examples (Like in College Assignments)

Example 1: Student Marks Program


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

        String name = "Rahul";
        int roll = 101;
        double percentage = 87.5;
        boolean isPassed = true;

        System.out.println("Name: " + name);
        System.out.println("Roll: " + roll);
        System.out.println("Percentage: " + percentage);
        System.out.println("Passed: " + isPassed);
    }
}

 

Here we used:

  • String
  • int
  • double
  • boolean

This is exactly the kind of program asked in lab exams.

 

Example 2: Type Casting Example

int a = 5;
double b = a;   // widening

double x = 9.8;
int y = (int) x;  // narrowing

 

Understanding this is important for:

  • Calculations
  • CGPA conversion
  • Percentage logic

 

Some Common Mistakes Students Make

  • Writing float f = 10.5; (Missing f)
  • Using double quotes in char
  • Forgetting type casting
  • Mixing int and double without understanding result

These small mistakes cause compile errors. And during practical exam, panic starts 😅

So practice is important.

 

Why Data Types Matter in Real Projects

  • Wrong data type → memory waste
  • Wrong data type → data loss
  • Wrong data type → calculation errors

For example: If you store money in int instead of double, decimals will be lost.

In backend projects (like when I worked with Spring Boot), choosing correct data type for database fields is very important.

 

Conclusion

So let’s quickly revise.

Data Types in Java are divided into:

1️⃣ Primitive Data Types

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

They store simple values directly.

2️⃣ Non-Primitive Data Types

  • String
  • Array
  • Class
  • Object
  • Interface

They store references.

And then we learned about:

  • Type casting (Implicit & Explicit)

If you’re a beginner, don’t just memorize definitions for exams. Try small programs. Change values. See what happens.

When I started practicing small codes daily, data types became very clear.

Trust me, once this topic is strong, Java becomes much easier.

 

Frequently Asked Questions (FAQs)

What is the most commonly used data type in Java?

int, double, and String are most commonly used in beginner and real-world programs.

Is String a primitive data type?

No. String is a non-primitive data type. It is actually a class in Java.

What is the default data type for decimal numbers?

The default data type for decimal numbers in Java is double.

What is type casting in Java?

Type casting means converting one data type into another, either automatically (widening) or manually (narrowing).

Why do we use boolean data type?

Boolean is used to store true or false values, mainly for conditions and decision-making in programs.

 

If you are preparing for semester exams or building your first Java project, master this topic properly. It’s small, but very powerful.

 

Happy coding 🚀

 

Tags

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !)#days=(20)

We use cookies to personalize content and ads, provide social media features, and analyze our traffic. By clicking "Accept", you consent to our use of cookies. Privacy Policy
Accept !
Breaking News | News |