Data Types in Java

Data Types in Java

May 11th, 2026
1541
15:00 Minutes

Have you ever thought about how your computer acknowledges the difference between numbers and words when writing Java codes? To understand it you need to know about data types in Java programming language. These types tell the program what kind of value a variable can store like a number, a letter or true and false. They also help the system decide how much space is needed to save that value.

Understanding them seems quite complicated first, but trust me it is not. You just have to start from the very basics and this guide here will help you do that. It explains what data types are in Java, their classification, importance, and best practices when you deal with them. Let’s begin!

What are Data Types in Java?

Data types in Java are classifications that specify the type of value a variable can store. It acts as a blueprint that tells the compiler or interpreter what type of value it can store in a particular variable. This ensures that the operations performed on the data type are safe and efficient.

Master Java Programming with Java Training

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

Why Understanding Data Types is Important?

It is very important for you to understand data types in Java programming. They are at the core of everything you do as a developer. Understanding them helps you know which data types should be used when. Choosing the right data type gives you various benefits, including:

1. Memory Allocation: Different data types need different amounts of memory. For example, a small number needs less memory than a big number or a long text. Choosing the correct data type helps use memory wisely.

2. Code Optimization: When we use the right data type, the program runs faster and more smoothly. It avoids wasting system resources.

3. Avoiding Runtime Errors: If we use the wrong data type, the program may crash or show errors while running. Understanding data types helps prevent these problems.

4. Readability and Maintainability: Proper data types make the code clear and easy to understand. It also becomes easier to update or fix the code later.

5. Data Integrity: Using the correct data type keeps the data accurate and safe. It prevents storing wrong or invalid values.

Read Also: Java Tutorial for Beginners

Classification of Data Types in Java

There are primarily two categories of data types in Java. Let explore them in-depth with examples:

1) Primitive Data Types

Primitive data types are those data types provided by Java. They store basic values directly in memory. There are various primitive data types in Java based on their size and uses. Let’s explore them one by one:

A) Integer Types (Whole Numbers)

Data Type Size Example
byte 1 byte -128 to 127
short 2 byte Small integers
int 4 byte Most commonly used
long 8 byte Large integers

Example Code:

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

        byte b = 10;
        short s = 2000;
        int i = 50000;
        long l = 10000000000L;  // L is required

        System.out.println("byte value: " + b);
        System.out.println("short value: " + s);
        System.out.println("int value: " + i);
        System.out.println("long value: " + l);
    }
}

Output:

integer data types in java

B) Floating-Point Types (Decimal Numbers)

Data Type Size Precision
float 4 bytes 6–7 digits
double 8 bytes 15 digits

Example Code:

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

        float f = 5.75f;   // f is required
        double d = 19.99;

        System.out.println("float value: " + f);
        System.out.println("double value: " + d);
    }
}

Output:

floating point data types in java

C) Character Type

It only stores a single character.

Data Type Size Example
char 2 bytes 'A', '1', '@'

Example Code:

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

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

        System.out.println("Grade: " + grade);
        System.out.println("Symbol: " + symbol);
    }
}

Output:

character data type in java

D) Boolean Type

It stores true or false.

Data Type Size Example
boolean 1 bit true / false

Example Code:

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

        boolean isJavaFun = true;
        boolean isFishFlying = false;

        System.out.println("Is Java fun? " + isJavaFun);
        System.out.println("Is fish flying? " + isFishFlying);
    }
}

Output:

boolean data type in java

2) Non-Primitive (Reference) Data Types

Non-Primitive Data Types in Java store references (memory addresses) to objects rather than storing actual values directly. These are also categorised into different types based on their characteristics. Here are some of the common ones:

A) String

String is a class in Java used to store text.

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

        String name = "Nehal";
        System.out.println("Name: " + name);
    }
}

Output:

string data type in java

B) Array

Array stores multiple values of the same type.

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

        int[] numbers = {10, 20, 30, 40};

        for(int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }
    }
}

Output:

array data type in java

C) Class (User-Defined Type)

class Student {
    int roll;
    String name;
}

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

        Student s1 = new Student();
        s1.roll = 45;
        s1.name = "Nehal";

        System.out.println("Roll: " + s1.roll);
        System.out.println("Name: " + s1.name);
    }
}

Output:

class data type in java

Read Also: What are Classes and Objects in Java

Primitive vs Non-Primitive Data Types: Key Differences

Primitive and non-primitive data types are the two main categories used to store data in Java programming. Understanding their difference helps you choose the correct type and write better code. Let me explain you this in brief:

Parameters Primitive Data Types Non-Primitive
Meaning Primitive data types store simple values like numbers, characters or true/false. Non-primitive data types store more complex data like words, arrays or objects.
Examples Examples include int, double, char and boolean. Examples include String, arrays, classes and interfaces.
What is Stored They store the actual value directly in memory. They store the reference (address) of the object, not the actual value.
Size Their size is fixed and depends on the type. Their size is not fixed and can change.
Speed They are generally faster because they store simple values They are slightly slower because they store references to objects.
Default Value They have default values such as 0, false or '\u0000'. Their default value is null if not assigned.
Creation They are already built into Java. They are created by the programmer or provided by Java libraries.

Type Casting in Java

Type Casting in Java means changing a variable from one data type to another data type. It is mainly used to convert numbers from one type to another.

There are two types of type casting in Java:

1. Implicit Casting (Widening Casting)

It happens automatically when we convert a smaller data type into a larger data type.

For example:

int number = 10;
double result = number;   // int is automatically converted to double
System.out.println(result);

2. Explicit Casting (Narrowing Casting)

It is done manually by the programmer when converting a larger data type into a smaller data type.

For example:

double number = 10.75;
int result = (int) number;   // double is converted to int
System.out.println(result);

Read Also: What are Identifiers in Java (Variable Names)

Want to Learn Everything About Java Programming?

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

What are Wrapper Classes in Java?

Wrapper classes in Java are special classes that convert primitive data types into objects. In Java, primitive types like int, double, and char are not objects. But sometimes, Java programs (especially Collections like ArrayList) require objects instead of primitive values. Wrapper classes solve this problem by “wrapping” a primitive value inside an object.

For example:

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

Let me explain you with the help of a code example:

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

        // Primitive value
        int num = 10;

        // Converting primitive to object (Autoboxing)
        Integer obj = num;

        System.out.println("Object value: " + obj);

        // Converting object back to primitive (Unboxing)
        int newNum = obj;

        System.out.println("Primitive value: " + newNum);
    }
}

Output:

wrapper classes in java

Read Also: A Comprehensive Guide to Collections in Java

What is Autoboxing in Java?

Autoboxing in Java is the automatic conversion of a primitive data type into its corresponding wrapper class object.

This feature was introduced in Java 5. It allows Java to automatically convert primitives like int, double etc, into objects like Integer, Double, without writing extra code.

For example:

  • int → Integer
  • double → Double

Let me explain you with the help of a code example:

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

        // Primitive type
        int num = 20;

        // Autoboxing (int to Integer)
        Integer obj = num;

        System.out.println("Wrapper Object: " + obj);
    }
}

Output:

autoboxing in java

What is Unboxing in Java?

Unboxing in Java is the automatic conversion of a wrapper class object into its corresponding primitive data type.

It is the opposite of autoboxing. Java will automatically extract the primitive value from the wrapper object.

For example:

  • Integer → int
  • Double → double

Let me explain you with the help of a code example:

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

        // Wrapper object
        Integer obj = 100;

        // Unboxing (Integer to int)
        int num = obj;

        System.out.println("Primitive Value: " + num);
    }
}

Output:

unboxing in java

Common Mistakes When Working with Data Types in Java

As a beginner, it is common to make mistakes, but when you understand them then it will help you avoid errors, improve programs and write clean, correct Java code. Let me tell you some of them:

1. Using the Wrong Data Type

Sometimes we choose the wrong type for a value. For example:

WRONG

int price = 99.99;   // Error: int cannot store decimals

RIGHT

double price = 99.99;

2. Forgetting Type Casting

If we don’t cast properly then Java may give an error or lose data.

WRONG

double num = 10.5;
int x = num;   // Error

RIGHT

double num = 10.5;
int x = (int) num;   // Now it works, but decimal part is removed

3. Integer Division Problem

When we divide two integers, Java gives an integer answer.

WRONG

int a = 5;
int b = 2;
System.out.println(a / b);   // Output: 2

We expect 2.5, but Java gives 2.

RIGHT

System.out.println((double)a / b);   // Output: 2.5

4. Not Initializing Variables

This error happens when we use a variable without giving it a value.

WRONG

int num;
System.out.println(num);   // Error

RIGHT

int num = 10;
System.out.println(num);

5. Overflow Problem

Every data type has a limit. If we go beyond that limit, overflow happens.

Example:

int num = 2147483647;  // Maximum int value
num = num + 1;
System.out.println(num);   // Output: -2147483648

Solution:

long num = 2147483647L;

Best Practices of Data Types in Java

The following are some best practices like choosing primitives,using constants, and leveraging wrapper classes helps write clean, efficient and error free programs:

1) Use Primitive Types for Performance

Primitive types like int, double, boolean are faster and use less memory.

public class PrimitiveExample {
    public static void main(String[] args) {
        int sum = 0;   // primitive type

        for (int i = 0; i < 1000000; i++) {
            sum += i;
        }

        System.out.println("Sum: " + sum);
    }
}

2) Use Constants for Fixed Values

Use final for values that should not change.

public class ConstantExample {

    final static int MAX_SIZE = 100;  // constant value

    public static void main(String[] args) {
        System.out.println("Maximum size is: " + MAX_SIZE);
    }
}

3) Leverage Wrapper Classes When Needed

Use Integer, Double, etc., when working with collections.

import java.util.ArrayList;

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

        ArrayList numbers = new ArrayList<>();

        numbers.add(10);   // int is automatically converted to Integer
        numbers.add(20);

        System.out.println(numbers);
    }
}

Start Your Web Development Journey Today

Build real websites and master front-end and back-end technologies step by step.

Explore Now

Wrapping up

This guide has explained everything about data types in java along from basics to advanced. It explained how they help manage memory, improve performance and prevent errors. You should now practice this knowledge on real-world projects, starting from writing simple programs and applying the correct data types to strengthen your understanding.

Explore Our Trending Articles-

FAQs of Data Types in Java

1. What is the difference between Widening and Narrowing Casting?

Widening casting converts a smaller data type to a larger data type automatically and narrowing casting converts a larger data type to a smaller data type manually using explicit casting.

2. Is String primitive or non-primitive?

String is a non-primitive data type in Java. It is actually a class used to store text.

3. What is the default value of data types?

Primitive data types have default values like 0 for numbers and false for boolean. Reference types like String have default values of null.

4. What is the difference between int and Integer?

int is a primitive type and stores only numbers. Integer is a wrapper class and can be used with collections like ArrayList.

About the Author
Author Nehal Sharma
About the Author

Nehal Sharma is a skilled content writer with expertise in Java, mobile development, and data analytics. She transforms complex data into actionable insights and has experience in business intelligence, data science, and Salesforce. She also simplifies technical concepts into clear, engaging content for learners and professionals.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.