Module: Variables and Data Types

Primitive and Non-Primitive Types

Java Core: Variables and Data Types - Primitive and Non-Primitive Types

In Java, every variable has a data type. This data type specifies the kind of value the variable can hold. Java data types are broadly categorized into two groups: Primitive Data Types and Non-Primitive Data Types (also known as Reference Data Types).


1. Primitive Data Types

  • Definition: Primitive data types are the basic building blocks for representing data in Java. They directly store values. They are not objects.

  • Characteristics:

    • Predefined by the Java language.
    • Fixed size (determined by the data type).
    • Stored directly in memory.
    • Faster to access and manipulate.
    • No methods associated with them.
  • Types: Java has eight primitive data types:

    Data Type Size (bits) Range Default Value Description
    byte 8 -128 to 127 0 Represents small integer values.
    short 16 -32,768 to 32,767 0 Represents integer values (larger than byte).
    int 32 -2,147,483,648 to 2,147,483,647 0 Represents integer values.
    long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L Represents large integer values.
    float 32 Approximately ±3.4028235e+38F 0.0F Represents single-precision floating-point numbers.
    double 64 Approximately ±1.7976931348623157e+308 0.0 Represents double-precision floating-point numbers.
    boolean 1 true or false false Represents a logical value.
    char 16 Unicode character (0 to 65,535) '\u0000' Represents a single character.
  • Example:

    int age = 30;
    double price = 19.99;
    boolean isStudent = true;
    char initial = 'J';
    

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

  • Definition: Non-primitive data types are created by the programmer. They are used to store complex data. They do not directly store values; instead, they store the memory address (reference) of the object.

  • Characteristics:

    • Created by the programmer (or provided by Java libraries).
    • Variable size (depends on the object).
    • Stored in the heap memory.
    • Slower to access and manipulate compared to primitive types.
    • Have methods associated with them.
    • Can be null (meaning they don't point to any object).
  • Types:

    • Classes: Blueprints for creating objects. (e.g., String, ArrayList, MyCustomClass)
    • Interfaces: Contracts that define a set of methods.
    • Arrays: Collections of elements of the same data type.
    • Enums: A special data type that enables a variable to be assigned a value from a predefined set of named constants.
  • Example:

    String name = "John Doe"; // String is a class
    int[] numbers = {1, 2, 3, 4, 5}; // Array of integers
    // MyCustomClass is a user-defined class
    MyCustomClass myObject = new MyCustomClass();
    
  • Important Note about Strings: Although String is often used like a primitive type, it's actually a class in Java. Therefore, it's a non-primitive type. String literals are often treated specially by the Java compiler for performance reasons (string interning), but they are still objects.


Key Differences Summarized

Feature Primitive Data Types Non-Primitive Data Types
Storage Directly in memory Reference to object in heap
Size Fixed Variable
Creation Predefined User-defined or library-defined
Speed Faster Slower
Methods No methods Methods available
Default Value Yes null
Examples int, double, boolean String, Array, Class

Understanding the difference between primitive and non-primitive data types is fundamental to writing efficient and correct Java code. Choosing the appropriate data type for a variable is crucial for memory management, performance, and overall program logic.