📑

On this page — Quick navigation

👇 Click any topic to scroll directly

Java Data Types

What are Data Types?

Data types specify the different sizes and values that can be stored in a variable. Java is a strongly typed language, meaning every variable must have a declared data type.


Types of Data Types in Java

Java has two categories of data types:

text
┌─────────────────────────────────────────────┐
│              JAVA DATA TYPES                 │
├──────────────────┬──────────────────────────┤
│                  │                          │
│   Primitive      │      Non-Primitive       │
│   Data Types     │      (Reference)         │
│                  │                          │
│ • byte           │ • String                 │
│ • short          │ • Arrays                 │
│ • int            │ • Classes                │
│ • long           │ • Interfaces             │
│ • float          │                          │
│ • double         │                          │
│ • char           │                          │
│ • boolean        │                          │
└──────────────────┴──────────────────────────┘

1. Primitive Data Types

Primitive types are the most basic data types in Java. They are predefined by the language and named by a reserved keyword.

Complete Primitive Data Types Table

Data Type Size Range Default Value Example
byte 1 byte -128 to 127 0 byte b = 100;
short 2 bytes -32,768 to 32,767 0 short s = 5000;
int 4 bytes -2,147,483,648 to 2,147,483,647 0 int i = 50000;
long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L long l = 100000L;
float 4 bytes ±3.4E-38 to ±3.4E+38 0.0f float f = 3.14f;
double 8 bytes ±1.7E-308 to ±1.7E+308 0.0d double d = 3.14159;
char 2 bytes 0 to 65,535 (Unicode) ‘\u0000’ char c = 'A';
boolean 1 bit true or false false boolean flag = true;

Detailed Examples with Output

1. Integer Types (byte, short, int, long)

Example:

java
public class IntegerTypes {
    public static void main(String[] args) {
        byte byteValue = 100;
        short shortValue = 5000;
        int intValue = 500000;
        long longValue = 15000000000L;  // 'L' is required for long
        
        System.out.println("byte value: " + byteValue);
        System.out.println("short value: " + shortValue);
        System.out.println("int value: " + intValue);
        System.out.println("long value: " + longValue);
    }
}

Output:

text
byte value: 100
short value: 5000
int value: 500000
long value: 15000000000

2. Floating-Point Types (float, double)

Example:

java
public class FloatTypes {
    public static void main(String[] args) {
        float floatValue = 3.14159f;  // 'f' is required for float
        double doubleValue = 3.141592653589793;
        
        System.out.println("float value: " + floatValue);
        System.out.println("double value: " + doubleValue);
        
        // Precision comparison
        float piFloat = 22/7f;
        double piDouble = 22/7d;
        
        System.out.println("Pi using float: " + piFloat);
        System.out.println("Pi using double: " + piDouble);
    }
}

Output:

text
float value: 3.14159
double value: 3.141592653589793
Pi using float: 3.142857
Pi using double: 3.142857142857143

3. Character Type (char)

Example:

java
public class CharExample {
    public static void main(String[] args) {
        char letterA = 'A';
        char digit = '9';
        char unicodeChar = '\u0041';  // Unicode for 'A'
        char specialChar = '@';
        
        System.out.println("Letter: " + letterA);
        System.out.println("Digit: " + digit);
        System.out.println("Unicode char: " + unicodeChar);
        System.out.println("Special char: " + specialChar);
        
        // char can be used as integer
        int asciiValue = 'A';
        System.out.println("ASCII value of A: " + asciiValue);
    }
}

Output:

text
Letter: A
Digit: 9
Unicode char: A
Special char: @
ASCII value of A: 65

4. Boolean Type

Example:

java
public class BooleanExample {
    public static void main(String[] args) {
        boolean isJavaFun = true;
        boolean isRaining = false;
        
        System.out.println("Is Java fun? " + isJavaFun);
        System.out.println("Is it raining? " + isRaining);
        
        // Using boolean in conditions
        int age = 18;
        boolean isEligible = age >= 18;
        
        if (isEligible) {
            System.out.println("You are eligible to vote!");
        }
    }
}

Output:

text
Is Java fun? true
Is it raining? false
You are eligible to vote!

2. Non-Primitive (Reference) Data Types

Non-primitive types are created by programmers and can be used to call methods.

String Example

Example:

java
public class StringExample {
    public static void main(String[] args) {
        String firstName = "John";
        String lastName = "Doe";
        String fullName = firstName + " " + lastName;
        
        System.out.println("First Name: " + firstName);
        System.out.println("Last Name: " + lastName);
        System.out.println("Full Name: " + fullName);
        
        // String methods
        System.out.println("Length: " + fullName.length());
        System.out.println("Uppercase: " + fullName.toUpperCase());
    }
}

Output:

text
First Name: John
Last Name: Doe
Full Name: John Doe
Length: 8
Uppercase: JOHN DOE

Array Example

Example:

java
public class ArrayExample {
    public static void main(String[] args) {
        // Integer array
        int[] numbers = {10, 20, 30, 40, 50};
        
        // String array
        String[] fruits = {"Apple", "Banana", "Mango", "Orange"};
        
        System.out.println("Numbers array:");
        for (int i = 0; i < numbers.length; i++) {
            System.out.println("Index " + i + ": " + numbers[i]);
        }
        
        System.out.println("\nFruits array:");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}

Output:

text
Numbers array:
Index 0: 10
Index 1: 20
Index 2: 30
Index 3: 40
Index 4: 50

Fruits array:
Apple
Banana
Mango
Orange

Type Conversion (Casting)

Implicit Casting (Widening) – Automatic

java
public class ImplicitCasting {
    public static void main(String[] args) {
        int intValue = 100;
        long longValue = intValue;      // Automatic: int → long
        float floatValue = longValue;    // Automatic: long → float
        double doubleValue = floatValue; // Automatic: float → double
        
        System.out.println("int: " + intValue);
        System.out.println("long: " + longValue);
        System.out.println("float: " + floatValue);
        System.out.println("double: " + doubleValue);
    }
}

Output:

text
int: 100
long: 100
float: 100.0
double: 100.0

Explicit Casting (Narrowing) – Manual Required

java
public class ExplicitCasting {
    public static void main(String[] args) {
        double doubleValue = 9.78;
        int intValue = (int) doubleValue;  // Manual casting required
        
        System.out.println("Original double: " + doubleValue);
        System.out.println("After casting to int: " + intValue);
        
        // Warning: Data loss possible
        int largeInt = 130;
        byte smallByte = (byte) largeInt;  // Data loss happens here
        System.out.println("int 130 → byte: " + smallByte);
    }
}

Output:

text
Original double: 9.78
After casting to int: 9
int 130 → byte: -126

Memory Visualization Diagram

text
Memory Allocation for Data Types:

┌──────────────────────────────────────────────────────────┐
│                     STACK MEMORY                         │
├──────────────────────────────────────────────────────────┤
│  byte b = 100;        →    [100]  (1 byte)              │
│  short s = 5000;      →    [5000] (2 bytes)             │
│  int i = 50000;       →    [50000] (4 bytes)            │
│  long l = 100000L;    →    [100000] (8 bytes)           │
│  float f = 3.14f;     →    [3.14] (4 bytes)             │
│  double d = 3.14159;  →    [3.14159] (8 bytes)          │
│  char c = 'A';        →    ['A'] (2 bytes)              │
│  boolean flag = true; →    [true] (1 bit)               │
└──────────────────────────────────────────────────────────┘

┌──────────────────────────────────────────────────────────┐
│                      HEAP MEMORY                         │
├──────────────────────────────────────────────────────────┤
│  String name = "Java";  →    [Reference] → "Java"       │
│  int[] arr = {1,2,3};   →    [Reference] → [1][2][3]    │
│  Object obj = new...;   →    [Reference] → Object Data  │
└──────────────────────────────────────────────────────────┘

Best Practices Summary

Best Practice Explanation
Use int for integers Most common and efficient for whole numbers
Use double for decimals Default choice for floating-point numbers
Add L for long values long value = 100000L;
Add F for float values float value = 3.14f;
Use String for text Most flexible for character sequences
Consider memory Use smaller types (byte, short) for large arrays
Be careful with casting Explicit casting can cause data loss

Quick Reference Card

java
// Variable Declaration Examples
int age = 25;
double salary = 55000.50;
char grade = 'A';
boolean isActive = true;
String name = "John Doe";

// Multiple variables
int x = 10, y = 20, z = 30;

// Constant (final)
final double PI = 3.14159;

// Type conversion
int num = (int) 3.14;  // 3

// Check maximum values
System.out.println("Max int: " + Integer.MAX_VALUE);
System.out.println("Min int: " + Integer.MIN_VALUE);