Java

Java Data Types: Complete Reference with Memory Size and Examples

By Utility Zone · 2025-11-06T09:34:37.583267

Java is a statically typed language, meaning every variable must be declared with a data type before use. Java provides a comprehensive set of data types organized into two main categories: primitive data types and reference data types.123


Primitive Data Types (8 Types)

Primitive data types store actual values directly in memory and have fixed sizes defined by the Java language. They are stored on the stack and provide fast access.2456

Integer Types (Signed Whole Numbers)

1. byte

The smallest integer type, useful for memory-efficient storage in large arrays.712

  • Size: 1 byte (8 bits)52
  • Range: -128 to 12712
  • Default Value: 025
  • Example:2
byte age = 25;
byte temperature = -50;

2. short

A 16-bit integer type used when memory conservation in large arrays is important but byte range is insufficient.81

  • Size: 2 bytes (16 bits)52
  • Range: -32,768 to 32,76712
  • Default Value: 052
  • Example:2
short temp = -200;
short year = 2025;

3. int

The default choice for integer values. Most operations in Java use int by default.8712

  • Size: 4 bytes (32 bits)52
  • Range: -2,147,483,648 to 2,147,483,64732
  • Default Value: 025
  • Example:2
int population = 2000000;
int count = 100;

4. long

Used for very large integer values that exceed int range.781

  • Size: 8 bytes (64 bits)52
  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,80735
  • Default Value: 0L52
  • Example:2
long worldPopulation = 7800000000L;  // Note the 'L' suffix
long distance = 999999999999L;

Important: When assigning a value to a long variable, append the letter L to indicate it's a long literal.2

Floating-Point Types (Decimal Numbers)

5. float

A single-precision 32-bit floating-point type used to save memory in large arrays of decimal numbers.731

  • Size: 4 bytes (32 bits)52
  • Precision: ~6-7 significant decimal digits352
  • Default Value: 0.0f52
  • Example:2
float pi = 3.14f;  // Note the 'f' suffix
float distance = 12.5f;

Important: Always append f to float literals. Never use float for precise values like currency.73

6. double

A double-precision 64-bit floating-point type providing better accuracy than float. This is the default choice for decimal numbers.1372

  • Size: 8 bytes (64 bits)52
  • Precision: ~15-16 significant decimal digits325
  • Default Value: 0.0d25
  • Example:2
double precisePi = 3.141592653589793;
double temperature = 98.6;

Character Type

7. char

Stores a single Unicode character using 16-bit encoding.132

  • Size: 2 bytes (16 bits)52
  • Range: 0 to 65,535 (Unicode characters)12
  • Default Value: '\u0000'52
  • Example:2
char grade = 'A';
char symbol = '@';
char unicode = '\u0041';  // Represents 'A'

Boolean Type

8. boolean

Represents logical values used in conditional statements and control flow.812

  • Size: 1 byte (JVM-dependent, typically 1 byte)52
  • Values: true or false32
  • Default Value: false25
  • Example:2
boolean isJavaFun = true;
boolean flag = false;

Reference Data Types

Reference data types store references (memory addresses) that point to objects stored in memory (the heap), rather than storing the values directly. All reference types default to null if not initialized.495

1. String

The most commonly used reference type, representing sequences of characters.9

  • Size: Variable (stored on heap)52
  • Default Value: null52
  • Example:9
String language = "Java";
String name = new String("John");
String message = "Hello, World!";

Key Point: Strings are immutable in Java, meaning once created, they cannot be modified.10

2. Arrays

Collections of elements of the same type stored sequentially.1112

  • Size: Variable (stored on heap)5
  • Default Value: null5
  • Element Access: Uses 0-based indexing11
  • Example:12
int[] numbers = {1, 2, 3, 4, 5};
String[] names = new String[^10];
double[] scores = {95.5, 87.3, 92.1};

// Accessing elements
int first = numbers[^0];  // Returns 1

Important: When arrays are passed to methods, the method receives a copy of the reference. Changes to array elements persist after method execution.12

3. Classes (Objects)

Custom data types created by programmers that bundle data and methods together.135

  • Size: Variable (stored on heap)5
  • Default Value: null5
  • Example:
public class Student {
    String name;
    int age;
}

Student student = new Student();

4. Interfaces

Abstract reference types that define a contract for implementing classes.13

  • Size: Variable (stored on heap)5
  • Default Value: null5
  • Example:
interface Animal {
    void speak();
}

class Dog implements Animal {
    public void speak() {
        System.out.println("Woof!");
    }
}

Animal animal = new Dog();

Wrapper Classes

Java provides wrapper classes for each primitive type, allowing you to treat primitives as objects. These classes are useful for:1415

  • Using primitives in collections (which require objects)
  • Accessing special methods and constants
  • Converting between types
PrimitiveWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Example:1514

// Autoboxing (primitive to wrapper)
Integer num = 100;
Double value = 3.14;

// Unboxing (wrapper to primitive)
int primitive = num;  // Converts back to int

// Using wrapper class methods
int maxInt = Integer.MAX_VALUE;  // 2,147,483,647
int minInt = Integer.MIN_VALUE;  // -2,147,483,648
double maxDouble = Double.MAX_VALUE;

Key Differences: Primitive vs Reference Types

AspectPrimitiveReference
Storage LocationStack (direct values)Heap (memory addresses)
MemoryFixed sizeVariable size
Default ValueType-specific (0, false, etc.)null
SpeedFasterSlower
Copy BehaviorCopy by valueCopy by reference
Examplesint, double, booleanString, Arrays, Objects

Practical Example: Using All Data Types

public class DataTypesDemo {
    public static void main(String[] args) {
        // Primitive Types
        byte myByte = 100;
        short myShort = 5000;
        int myInt = 1000000;
        long myLong = 15000000000L;
        
        float myFloat = 3.14f;
        double myDouble = 3.14159265359;
        
        char myChar = 'J';
        boolean myBoolean = true;
        
        // Reference Types
        String myString = "Java Programming";
        int[] myArray = {1, 2, 3, 4, 5};
        
        // Output
        System.out.println("byte: " + myByte);
        System.out.println("int: " + myInt);
        System.out.println("long: " + myLong);
        System.out.println("double: " + myDouble);
        System.out.println("String: " + myString);
        System.out.println("Array: " + java.util.Arrays.toString(myArray));
    }
}

Output:

byte: 100
int: 1000000
long: 15000000000
double: 3.14159265359
String: Java Programming
Array: [1, 2, 3, 4, 5]

Selection Guide

  • Use byte or short: When memory is critical and values fit within their range
  • Use int: For most integer values (default choice)
  • Use long: For very large numbers (population, distances)
  • Use float: Rarely, when memory is extremely limited
  • Use double: For all decimal calculations (default choice)
  • Use char: For single characters
  • Use boolean: For true/false conditions
  • Use String: For text data
  • Use arrays: For collections of same-type data
  • Use custom classes: For complex data structures combining multiple types

Understanding these data types and their memory requirements is essential for writing efficient, reliable Java applications.312 <span style="display:none">161718</span>


<div align="center">⁂</div>

Footnotes

  1. https://www.datacamp.com/doc/java/java-data-types ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13

  2. https://www.inexture.com/java-data-types-size-range-usage-guide/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14 ↩15 ↩16 ↩17 ↩18 ↩19 ↩20 ↩21 ↩22 ↩23 ↩24 ↩25 ↩26 ↩27 ↩28 ↩29 ↩30 ↩31 ↩32 ↩33 ↩34 ↩35 ↩36 ↩37 ↩38 ↩39 ↩40 ↩41 ↩42

  3. https://www.geeksforgeeks.org/java/java-data-types/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11

  4. https://www.freecodecamp.org/news/java-data-types-and-variables/ ↩ ↩2

  5. https://devstacktips.com/web-development/2025/08/14/java-data-types-for-high-performance-coding-a-complete-reference/ ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14 ↩15 ↩16 ↩17 ↩18 ↩19 ↩20 ↩21 ↩22 ↩23 ↩24 ↩25 ↩26 ↩27 ↩28 ↩29 ↩30

  6. https://www.freecodecamp.org/news/data-types-in-java/ ↩

  7. https://www.shiksha.com/online-courses/articles/primitive-data-types-in-java-blogId-164457 ↩ ↩2 ↩3 ↩4 ↩5 ↩6

  8. https://www.geekster.in/articles/java-data-types/ ↩ ↩2 ↩3 ↩4

  9. https://hyperskill.org/university/java/java-data-types ↩ ↩2 ↩3

  10. https://www.sololearn.com/en/Discuss/1488976/are-strings-reference-type ↩

  11. https://prepinsta.com/java/array-references-in-java/ ↩ ↩2

  12. https://java-programming.mooc.fi/part-3/3-arrays/ ↩ ↩2 ↩3

  13. https://www.tutorialspoint.com/what-are-reference-data-types-in-java ↩ ↩2

  14. https://runestone.academy/ns/books/published/csawesome/Unit2-Using-Objects/topic-2-8-IntegerDouble.html ↩ ↩2

  15. https://runestone.academy/ns/books/published/csjava/Unit2-Using-Objects/topic-2-8-IntegerDouble.html ↩ ↩2

  16. https://www.geeksforgeeks.org/java/types-references-java/ ↩

  17. https://stackoverflow.com/questions/15183387/java-arrays-as-reference-types-in-methods ↩

  18. https://codehs.com/textbook/apcsa_textbook/2.8 ↩