Live AI Powered DevOps with AWS
JavaBasics

Data Types

1. Introduction

Data types define the type of data a variable can store and how much memory it occupies.
Java is a strongly typed language, which means every variable must have a declared type.

Understanding data types is essential because they determine:

  • The memory size required
  • The values allowed
  • The operations permitted
  • How data is stored and processed by the JVM

Java provides two categories of data types:

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

This document covers both with detailed explanations and examples.

2. Categories of Data Types in Java

Java data types are divided into:

            Data Types
            /        \
     Primitive     Non-Primitive

data-types

3. Primitive Data Types

Primitive types store simple values and are not objects.
They are pre-defined by Java and stored directly in stack memory (except arrays inside heap).

There are 8 primitive data types:

TypeSizeRangeExample
byte1 byte-128 to 127byte b = 10;
short2 bytes-32,768 to 32,767short s = 200;
int4 bytes-2^31 to 2^31-1int x = 50000;
long8 bytes-2^63 to 2^63-1long l = 900000L;
float4 bytes~7 decimal digitsfloat f = 10.5f;
double8 bytes~15 decimal digitsdouble d = 99.99;
char2 bytesSingle Unicode characterchar grade = 'A';
boolean1 bit (JVM-dependent)true or falseboolean isAdmin = true;

4. Detailed Explanation of Each Primitive Type

4.1 byte

Used for small integers, memory saving in arrays.

byte age = 25;

4.2 short

Rarely used, but useful in large arrays to reduce memory.

short temperature = 280;

4.3 int

Default choice for integers.

int salary = 75000;

4.4 long

Use when integer value exceeds int range. Must end with L or l.

long population = 7800000000L;

4.5 float

Used for fractional values with less precision. Must end with f or F.

float rating = 4.5f;

4.6 double

Default for decimal values.

double price = 999.99;

4.7 char

Stores a single Unicode character.

char grade = 'A';
char symbol = '#';

4.8 boolean

Stores true or false.

boolean isActive = true;

5. Non-Primitive Data Types

These types refer to objects, not primitive values. They are stored in heap memory, and the variable stores a reference.

Examples include:

  • String
  • Arrays
  • Classes
  • Interfaces
  • Enums
  • Wrapper Classes

5.1 String

String name = "John";

Strings are objects in Java, even though they behave like primitives.

5.2 Arrays

int[] numbers = {1, 2, 3, 4};

5.3 Classes & Objects

class Student { }
Student s = new Student();

5.4 Wrapper Classes

Every primitive has a corresponding wrapper:

PrimitiveWrapper
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

data-types

6. Type Ranges and Memory Usage

Memory allocation helps JVM optimize performance.

Example:

int a = 10;
double b = 20.5;
char c = 'X';

JVM stores these in stack memory with fixed sizes.

Non-primitive example:

String city = "Mumbai";

Here, city (reference) is in stack, the actual string object is stored in heap.

7. Type Casting

Sometimes you need to convert one type to another.

7.1 Implicit Casting (Widening)

Small → large type (safe)

byte → short → int → long → float → double

Example:

int x = 10;
double y = x;  // implicit

7.2 Explicit Casting (Narrowing)

Large → small type (risk of data loss)

double → float → long → int → short → byte

Example:

double d = 99.9;
int i = (int) d; // i becomes 99

8. Default Values of Primitive Types (Instance Variables)

Local variables do NOT get default values.

TypeDefault
int0
double0.0
booleanfalse
char'\u0000'
Objectnull

9. Examples

Primitive Example

int age = 21;
double height = 5.9;
char section = 'A';
boolean isEligible = true;

Non-Primitive Example

String course = "Java Programming";
int[] marks = {85, 90, 78};
Student st = new Student();

10. Common Mistakes

  • Using float without f suffix
  • Using long without L suffix
  • Confusing char with String type
  • Thinking boolean stores 1/0
  • Using uninitialized local variables

11. Summary

  • Java has 8 primitive and many non-primitive data types.
  • Primitive types store values; non-primitives store references.
  • Memory for primitives is fixed; objects grow dynamically.
  • Type casting allows conversion between types.
  • Understanding data types is essential for efficient memory and performance.

Written By: Shiva Srivastava

How is this guide?

Last updated on