DEV Community

Cover image for Data Types in Java
ABISHEK M
ABISHEK M

Posted on

Data Types in Java

Introduction

Data types are one of the fundamental concepts in Java programming.

Whenever we write a program, we need to store different kinds of information, such as numbers, names, decimal values, and true or false conditions.

Java provides different data types to store these values.

A data type specifies what kind of value a variable can store.

For example:

  • Age – Integer
  • Name – Text
  • Salary – Decimal number
  • Grade – Character
  • Passed – True or False

Let's understand data types in Java in detail.

1. Types of Data Types in Java

Java data types are divided into two major categories:

  1. Primitive Data Types
  2. Non-Primitive (Reference) Data Types

2. Primitive Data Types

Primitive data types are the basic built-in data types provided by Java.

Java has 8 primitive data types:

  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. char
  8. boolean

Let's understand each data type.

1. byte

The byte data type is used to store small whole numbers.

  • Memory: 1 byte
  • Range: -128 to 127

Example:

byte age = 21;
System.out.println(age);
Enter fullscreen mode Exit fullscreen mode

Output:

21
Enter fullscreen mode Exit fullscreen mode

2. short

The short data type is used to store whole numbers within a larger range than byte.

  • Memory: 2 bytes
  • Range: -32,768 to 32,767

Example:

short number = 25000;
System.out.println(number);
Enter fullscreen mode Exit fullscreen mode

Output:

25000
Enter fullscreen mode Exit fullscreen mode

3. int

The int data type is commonly used to store integer values.

  • Memory: 4 bytes
  • Range: -2,147,483,648 to 2,147,483,647

Example:

int mark = 90;
System.out.println(mark);
Enter fullscreen mode Exit fullscreen mode

Output:

90
Enter fullscreen mode Exit fullscreen mode

4. long

The long data type is used to store very large whole numbers.

  • Memory: 8 bytes
  • Range: -2⁶³ to 2⁶³ - 1

Example:

long population = 8000000000L;
System.out.println(population);
Enter fullscreen mode Exit fullscreen mode

Output:

8000000000
Enter fullscreen mode Exit fullscreen mode

Important: We use the L suffix when writing a long literal that needs it.

5. float

The float data type is used to store decimal numbers.

  • Memory: 4 bytes
  • Precision: Approximately 6–7 decimal digits.

Example:

float price = 99.5f;
System.out.println(price);
Enter fullscreen mode Exit fullscreen mode

Output:

99.5
Enter fullscreen mode Exit fullscreen mode

Important: We must use the f suffix for a float literal.

6. double

The double data type is used to store decimal numbers with greater precision than float.

  • Memory: 8 bytes
  • Precision: Approximately 15–16 decimal digits.

Example:

double salary = 25000.75;
System.out.println(salary);
Enter fullscreen mode Exit fullscreen mode

Output:

25000.75
Enter fullscreen mode Exit fullscreen mode

Important: Double is the default type for decimal literals in Java.

7. char

The char data type is used to store a single character.

  • Memory: 2 bytes
  • Uses single quotation marks.

Example:

char grade = 'A';
System.out.println(grade);
Enter fullscreen mode Exit fullscreen mode

Output:

A
Enter fullscreen mode Exit fullscreen mode

Important: A char uses single quotes, whereas String uses double quotes.

8. boolean

The boolean data type is used to store only two values:

  • true
  • false

It is mainly used for decision-making and condition checking.

Example:

boolean isPassed = true;
System.out.println(isPassed);
Enter fullscreen mode Exit fullscreen mode

Output:

true
Enter fullscreen mode Exit fullscreen mode

Java does not specify a fixed memory size for boolean.

3. Primitive Data Types Summary

Data Type Memory Purpose
byte 1 byte Small whole numbers
short 2 bytes Whole numbers
int 4 bytes Integer values
long 8 bytes Large whole numbers
float 4 bytes Decimal numbers
double 8 bytes More precise decimal numbers
char 2 bytes Single character
boolean Not specified True or false

Primitive Data Types Structure

Primitive Data Types
│
├── Integer Types
│   ├── byte
│   ├── short
│   ├── int
│   └── long
│
├── Floating-Point Types
│   ├── float
│   └── double
│
├── Character Type
│   └── char
│
└── Boolean Type
    └── boolean
Enter fullscreen mode Exit fullscreen mode

4. Non-Primitive Data Types

Non-primitive data types are also called reference types.

They are used to work with objects and more complex data.

Some important examples are:

  1. String
  2. Arrays
  3. Classes

1. String

String is used to store a sequence of characters or text.

Example:

String name = "Abishek";

System.out.println(name);
Enter fullscreen mode Exit fullscreen mode

Output:

Abishek
Enter fullscreen mode Exit fullscreen mode

Important points:

  • String is a non-primitive reference type.
  • String is a class in Java.
  • String uses double quotation marks.
  • String is immutable, meaning its existing content cannot be changed.

2. Array

An array is used to store multiple values of the same data type in a single variable.

Example:

int[] marks = {80, 90, 75, 85, 95};

System.out.println(marks[0]);
Enter fullscreen mode Exit fullscreen mode

Output:

80
Enter fullscreen mode Exit fullscreen mode

Important points:

  • Array stores multiple values.
  • Array indexing starts from 0.
  • Array has a fixed length after creation.
  • Arrays are objects in Java.

3. Class

A class is a blueprint used to create objects.

Example:

class Student {

    String name = "Abishek";
    int age = 21;
}
Enter fullscreen mode Exit fullscreen mode

Here, Student is a class.

We can create an object using the new keyword.

Student s1 = new Student();

System.out.println(s1.name);
System.out.println(s1.age);
Enter fullscreen mode Exit fullscreen mode

Output:

Abishek
21
Enter fullscreen mode Exit fullscreen mode

5. Primitive vs Non-Primitive Data Types

Primitive Non-Primitive
Basic built-in data types Reference types
Java has 8 primitive types Many reference types exist
Holds primitive values Holds references to objects
Cannot store null Can store null
Does not represent an object Refers to an object or null
Examples: int, char, boolean Examples: String, arrays, classes

6. How Are They Stored in Memory?

Java uses different memory areas during program execution.

Two important memory areas are Stack and Heap.

Stack Memory

Stack memory is used for method execution and stack frames.

Local primitive variables and local reference variables are represented in their method's stack frame.

Heap Memory

Heap memory is used for allocating objects and arrays.

Example:

class Student {
    String name = "Abishek";
}

public class Main {

    public static void main(String[] args) {

        int age = 21;

        Student s1 = new Student();
    }
}
Enter fullscreen mode Exit fullscreen mode

Conceptual memory representation:

             JVM MEMORY
                  |
          ┌───────┴───────┐
          |               |
        STACK           HEAP
          |               |
     ┌──────────┐    ┌───────────┐
     | age = 21 |    | Student   |
     |          |    |           |
     | s1       |───>| name      |
     |          |    | Abishek   |
     └──────────┘    └───────────┘
Enter fullscreen mode Exit fullscreen mode

Here:

  • age is a primitive variable.
  • s1 is a reference variable.
  • The Student object is allocated in Heap.
  • s1 holds a reference that allows us to access the Student object.

Note: This is a conceptual model of JVM memory, not a guarantee of the exact physical placement of every variable.

7. Real-World Example

Imagine developing a student management application.

We need to store student details such as name, age, marks, grade, and result.

public class Student {

    public static void main(String[] args) {

        // Primitive Data Types

        int age = 21;

        double percentage = 85.5;

        char grade = 'A';

        boolean isPassed = true;

        // Non-Primitive Data Types

        String name = "Abishek";

        int[] marks = {80, 90, 85};

        System.out.println(name);
        System.out.println(age);
        System.out.println(percentage);
        System.out.println(grade);
        System.out.println(isPassed);

        System.out.println(marks[0]);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we use primitive and non-primitive data types together to manage student information.

Conclusion

Data types are an important foundation of Java programming.

They help us decide what kind of information we want to store in a program.

Java provides 8 primitive data types for storing basic values and different reference types for working with objects and complex data.

Understanding data types makes it easier to learn variables, classes, objects, arrays, and other Java concepts.




Enter fullscreen mode Exit fullscreen mode

Top comments (0)