Home Java Java Data Types

Java Data Types

Beginner ⏱ 8 min read Updated: Jul 2026
  • Java data types define the type of data that a variable can store.
  • Data types specify the memory size and range of values a variable can contain.
  • Java provides two categories of data types: primitive and non-primitive data types.
  • Primitive data types store simple values directly.
  • Non-primitive data types store references to objects.

What are Data Types in Java?

Data types in Java define what type of value a variable can store. Every variable in Java must have a declared data type before it can be used.

Java is a strongly typed programming language, which means the compiler checks the data type of variables during compilation. This helps prevent errors and improves program reliability.

💡
Important: Java requires every variable to have a specific data type. The data type determines the type of value and amount of memory required.

Why are Data Types Important in Java?

Data types are important because they help Java manage memory efficiently and provide better control over the values stored in variables.

Memory Management

Data types allocate the required amount of memory for storing values.

Type Safety

Data types prevent invalid values from being assigned to variables.

Performance

Using the correct data type improves application performance.

Readability

Data types make programs easier to understand and maintain.

Types of Data Types in Java

Java data types are divided into two main categories:

  • Primitive Data Types: Built-in data types that store simple values.
  • Non-Primitive Data Types: Reference data types that store objects and complex data.

Primitive Data Types in Java

Primitive data types are predefined by Java and store basic values directly. Java provides eight primitive data types.

Data Type Size Description
byte 1 byte Stores small integer values
short 2 bytes Stores short integer values
int 4 bytes Stores normal integer values
long 8 bytes Stores large integer values
float 4 bytes Stores decimal values
double 8 bytes Stores large decimal values
char 2 bytes Stores single characters
boolean JVM dependent Stores true or false values

Example of Primitive Data Types in Java

The following example demonstrates the use of different primitive data types in a Java program.

Example

class Main{ 
    public static void main(String[] args) {
        byte number =  100;
        short value =  20000;
        int age =25;
        long population =  1000000L;
        float price =  99.5f;
        double salary =  50000.75;
        char grade =  'A';
        boolean status = true;
        System.out.println(number);
        System.out.println(value);
         System.out.println(age);
        System.out.println(population); 
         System.out.println(price);
         System.out.println(salary);
         System.out.println(grade);
         System.out.println(status);
    }
    }
        
Result
100
20000
25
1000000
99.5
50000.75
A
true

Non-Primitive Data Types in Java

Non-primitive data types are reference types that store the memory address of objects instead of storing actual values directly.

These data types are created by programmers and provide additional functionality through methods and objects.

String

Used to store a collection of characters or text values.

Array

Used to store multiple values of the same data type.

Class

Defines the structure and behavior of objects.

Object

Represents an instance of a class containing data and methods.

String Data Type in Java

String is one of the most commonly used non-primitive data types in Java. It is used to store text values inside double quotes.

Example of String in Java

Example

class Main{/span>
    public static void main (String[] args) {
          String language = "Java";
        System.out.println(language);
    }
  }
        
Result
Java

Array Data Type in Java

An array is used to store multiple values of the same data type in a single variable. Each element in an array is accessed using an index number.

Example of Array in Java

Example

class Main{ 
    public static void main(String[] args) {
        int [] numbers = {10, 20,30};
        System.out.println(numbers[1]);
    }
}
        
Result
20

Class Data Type in Java

A class is a user-defined data type that acts as a blueprint for creating objects. It contains variables and methods that define object behavior.

Example of Class in Java

Example

class Student{ 
    int String name; age; 
}
        

Object Data Type in Java

An object is an instance of a class. Objects allow developers to access variables and methods defined inside a class.

Example of Object in Java

Example

class Student{
    String name;
}
class Main{ 
    public static void main(String[] args) {
         Student s1 = new Student(); 
    }
    }
        

Type Conversion in Java

Type conversion in Java is the process of converting a value from one data type into another data type. Java supports automatic and manual type conversion.

Type conversion is useful when performing operations between different data types.

Widening Type Conversion

Widening conversion converts a smaller data type into a larger data type automatically. It is also known as implicit conversion because Java performs it without manual code.

Example

class Main{ 
    public static void main(String[] args) {
        int number =50;
        double value = number; 
        System.out.println(value);
    }
    }
        
Result
50.0

Narrowing Type Conversion

Narrowing conversion converts a larger data type into a smaller data type. It requires manual casting by the programmer.

Example

class Main{
    public static void main (String[] args) {
        double price =99.99;
        int value = ( int ) price;
        System.out.println(value);
    }
    }
        
Result
99

Difference Between Primitive and Non-Primitive Data Types

Primitive Data Types Non-Primitive Data Types
Built-in data types provided by Java Created by programmers
Store actual values directly Store references to objects
Fixed memory size Memory size depends on objects
Example: int, char, boolean Example: String, Array, Class

Best Practices for Using Java Data Types

Choosing the correct data type improves memory management, performance, and readability of Java programs.

  • Always choose a data type according to the required value range.
  • Use int for normal integer calculations.
  • Use long when storing very large numbers.
  • Use double for accurate decimal calculations.
  • Use boolean for conditions that have only true or false values.
  • Use meaningful variable names with appropriate data types.
💡 Note: Java is a strongly typed language. A variable cannot store a value of another data type without proper conversion.

Java Data Types Summary

Java data types define the kind of information that variables can store. They help the compiler understand memory requirements and allowed operations.

Java provides eight primitive data types for storing simple values and several non-primitive data types for handling objects and complex structures.

Key Points to Remember

  • Every Java variable must have a declared data type.
  • Java has two types of data types: primitive and non-primitive.
  • Primitive data types store direct values.
  • Non-primitive data types store object references.
  • String is a non-primitive data type in Java.
  • Type conversion allows changing one data type into another.

Frequently Asked Questions

What are Java data types?

Java data types define the type of value that can be stored inside a variable.

How many primitive data types are available in Java?

Java provides eight primitive data types: byte, short, int, long, float, double, char, and boolean.

Is String a primitive data type in Java?

No, String is a non-primitive data type because it is a class in Java.

What is type conversion in Java?

Type conversion is the process of converting one data type into another data type.