Java Data Types

Introduction

Java is a statically typed programming language, meaning every variable must be declared with a specific data type before it can be used. Data types help the Java compiler allocate the correct amount of memory, perform type checking, and prevent invalid operations.

Java provides two main categories of data types:

  • Primitive Data Types
  • Non-Primitive (Reference) Data Types

Primitive data types are built into the Java language and store simple values such as numbers, characters, and boolean values.

Non-primitive data types store references to objects and include classes, arrays, strings, interfaces, and collections.

What are Java Data Types?

A Java data type defines the type of value a variable can store. It tells the compiler how much memory to allocate for the variable and what operations are allowed on it.

Example:


int age = 25;

In this example:

  • int is the data type.
  • age is the variable.
  • 25 is the value.

Since age is declared as an int, it can only store whole numbers.

Types of Java Data Types

Java data types are divided into two main categories:


Java Data Types
│
├── Primitive Data Types
│     ├── byte
│     ├── short
│     ├── int
│     ├── long
│     ├── float
│     ├── double
│     ├── char
│     └── boolean
│
└── Non-Primitive (Reference) Data Types
      ├── String
      ├── Array
      ├── Class
      ├── Object
      ├── Interface
      ├── Enum
      └── Collection

Primitive Data Types

Primitive data types are predefined by Java and store simple values directly.

Java provides eight primitive data types.

Data Type Size Default Value Example
byte 1byte 0 byte age = 25;
short 2 bytes 0 short year = 2025;
int 4 bytes 0 int salary = 50000;
long 8 bytes 0L long population = 8000000000L;
float 4 bytes 0.0f float price = 99.99f;
double 8 bytes 0.0 double pi = 3.14159;
char 2 bytes ‘\u0000’ char grade = ‘A’;
boolean JVM dependent false</td boolean active = true;

1. byte

The byte data type stores small integer values.

Range:


-128 to 127

Example:


byte age = 25;
System.out.println(age);

Use byte when memory optimization is important and the value range is small.

2. short

The short data type stores larger integer values than byte.

Range:


-32,768 to 32,767

Example:


short year = 2026;

3. int

The int data type is the most commonly used integer type.

Range:


-2,147,483,648 to 2,147,483,647

Example:


int marks = 95;

Use int for most whole-number calculations.

4. long

The long data type stores very large integer values.

Example:


long population = 8100000000L;

Notice the L suffix.

5. float

The float data type stores decimal numbers with single precision.

Example:


float price = 299.99f;

Notice the f suffix.

6. double

The double data type stores decimal numbers with higher precision than float.

Example:


double salary = 65000.75;

double is the preferred choice for decimal values in most applications.

7. char

The char data type stores a single Unicode character.

Example:


char symbol = '#';

Another example:


char symbol = '#';

Remember that char values are enclosed in single quotes.

8. boolean

The boolean data type stores only two values:

  • true
  • false

Example:


boolean passed = true;

Boolean variables are commonly used in conditions and loops.

Non-Primitive Data Types

Non-primitive (reference) data types store references to objects rather than the actual data.

Common examples include:

  • String
  • Array
  • Class
  • Object
  • Interface
  • Enum
  • Collection

Example:


String name = "Alice";

Unlike primitive data types, non-primitive types can call methods and contain multiple values or behaviors.

Example Program


public class DataTypesExample {
    public static void main(String[] args) {
        byte age = 25;
        short year = 2026;
        int marks = 95;
        long population = 8100000000L;
        float price = 499.99f;
        double salary = 65000.75;
        char grade = 'A';
        boolean passed = true;
        String name = "John";
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Year: " + year);
        System.out.println("Marks: " + marks);
        System.out.println("Population: " + population);
        System.out.println("Price: " + price);
        System.out.println("Salary: " + salary);
        System.out.println("Grade: " + grade);
        System.out.println("Passed: " + passed);
    }
}

Output

Name: John
Age: 25
Year: 2026
Marks: 95
Population: 8100000000
Price: 499.99
Salary: 65000.75
Grade: A
Passed: true

Primitive vs Non-Primitive Data Types

Feature Primitive Non-Primitive
Stores Actual value Reference (memory address)
Memory Fixed size Depends on the object
Methods Not available Available
Null Value No Yes
Performance Faster Slightly slower
Examples int, double, char String, Array, Object

Real-Life Example

Imagine you’re developing a Student Management System.


String studentName = "Rahul";
int rollNumber = 101;
double percentage = 88.5;
char grade = 'A';
boolean passed = true;

Each variable uses the most appropriate data type:

  • String → Student name
  • int → Roll number
  • double → Percentage
  • char → Grade
  • boolean → Pass or fail status

Choosing the correct data type improves readability, memory usage, and performance.

Best Practices

📖
When working with Java data types:
  • Use int for most whole numbers.
  • Use long only when values exceed the int range.
  • Prefer double over float for decimal calculations.
  • Use boolean for true/false conditions.
  • Use meaningful variable names.
  • Choose the smallest suitable data type when memory optimization is important.
  • Avoid unnecessary type conversions.
  • Follow Java naming conventions.

Common Mistakes

Beginners frequently make the following mistakes.

1. Forgetting the L Suffix

Incorrect:


long population = 8100000000;

Correct:


long population = 8100000000L;

2. Forgetting the f Suffix

Incorrect:


float price = 99.99;

Correct:


float price = 99.99f;

3. Using Double Quotes for char

Incorrect:


char grade = "A";

Correct:


char grade = 'A';

4. Using Single Quotes for Strings

Incorrect:


String name = 'John';

Correct:


String name = "John";

5. Choosing the Wrong Data Type

Incorrect:


int salary = 55000.75;

Correct:


double salary = 55000.75;

6. Assigning an Invalid Boolean Value

Incorrect:


boolean active = "true";

Correct:


boolean active = true;

Conclusion

Java data types are the foundation of every Java program. They define the kind of values variables can store, determine memory usage, and ensure type safety during compilation. Java offers eight primitive data types for storing simple values and a wide range of non-primitive data types for working with objects, strings, arrays, collections, and custom classes.

Choosing the correct data type helps improve application performance, readability, and maintainability.

Java Data Type – Interview Questions

Q 1: What is a data type in Java?
Ans: A data type specifies the type of value a variable can store in Java.
Q 2: How many types of data types are there in Java?
Ans: Two types: Primitive and Non-Primitive.
Q 3: Name primitive data types in Java.
Ans: byte, short, int, long, float, double, char, boolean.
Q 4: What is the default data type for decimal values?
Ans: double is the default decimal data type.
Q 5: What is the size of int data type?
Ans: The size of int is 4 bytes.

Java Data Type – Objective Questions (MCQs)

Q1. Which of the following is a primitive data type in Java?






Q2. What is the size of a long data type in Java?






Q3. What is the default value of a char data type in Java?






Q4. Which of the following data types can store decimal values in Java?






Q5. Which of the following is the correct range for the byte data type in Java?






Related Java Tutorials