Skip to content
C

Data Types

A data type tells Java what kind of value a variable will store — whether it's a whole number, a decimal number, a single character, or true/false.


1. What are Data Types?

A data type tells Java what kind of value a variable will store — whether it's a whole number, a decimal number, a single character, or true/false. Java has two categories: primitive types (like int, double, char, boolean) and reference types (like String, arrays, and objects).

2. Why is it used?

Data types help Java know exactly how much memory to reserve and what operations are valid on a value. For example, you can perform mathematical operations on an int, but not on a String, because their data types behave differently.

3. Real-Life Example

Think of different containers in a kitchen — a jar for sugar, a bottle for oil, a box for spices. Each container is designed for one kind of item. Similarly, each data type is designed to correctly store one kind of value.

4. Syntax

java
int wholeNumber = 10; double decimalNumber = 10.5; char letter = 'A'; boolean isActive = true; String text = "Hello";

5. Example Program

java
public class DataTypeDemo { public static void main(String[] args) { int marks = 90; double percentage = 90.5; char grade = 'A'; boolean passed = true; System.out.println(marks + " " + percentage + " " + grade + " " + passed); } }

Output:

90 90.5 A true

6. Key Points to Remember

  • 8 primitive types exist in Java: byte, short, int, long, float, double, char, boolean.
  • String is not a primitive type — it is a class (reference type).
  • Choosing the right data type avoids wasted memory and unexpected errors.

Mock Test

  • Data Types - Quick Test

    10 questions on Java's primitive types, String, and how they behave.

    10 questions · 10 min · Easy
    Start Mock Test

Coding Problems