Differences Between Primitive and Non-Primitive Data Types in Java

Differences Between Primitive and Non-Primitive Data Types in Java

Primitive Data Types:

  1. Examples: long, int, char, boolean, float.

  2. Primitive data types are basic types provided by a programming language.

  3. Stored in stack memory.

  4. Directly contain their values.

  5. When assigned to another variable, a new memory location is created.

  6. Uninitialized primitive variables produce a compiler error.

Non-Primitive Data Types:

  1. Anything not a primitive is a non-primitive data type.

  2. Non-primitive data types are actually classes.

  3. Classes can be user-defined or predefined (e.g., System in Java).

  4. Example: User-defined class Point with members x and y.

  5. Created using the new operator.

  6. Stored in heap memory.

  7. Non-primitive variables are references to memory locations where objects are stored.

  8. Default initialization values:

    • Integers: 0

    • Boolean: false

    • Floating point: 0.0

    • References: null

  9. Uninitialized members of non-primitive types get default values, while uninitialized primitive types cause compiler errors.

  10. Access members using the dot operator (e.g., p.x).

Differences Between Primitive and Non-Primitive Data Types:

  1. Memory Allocation:

    • Primitives: Stack memory.

    • Non-Primitives: Heap memory.

  2. References:

    • Primitives: Directly contain their values.

    • Non-Primitives: Contain references to objects in heap memory.

  3. Assignment:

    • Primitives: Creating a new variable with a primitive type and assigning it a value from another variable creates a new memory location.

    • Non-Primitives: Creating a new variable with a non-primitive type and assigning it a value from another variable makes both variables reference the same memory location.

  4. Initialization:

    • Primitives: Must be explicitly initialized; otherwise, a compiler error occurs.

    • Non-Primitives: Members get default values if not explicitly initialized.

Example:

  • Primitive Example:

      int x1 = 10;
      int x2 = x1;
      x2 = 20;
      // Output: x1 = 10, x2 = 20
    
  • Non-Primitive Example:

      Point p1 = new Point();
      p1.x = 10;
      p1.y = 20;
      Point p2 = p1;
      p2.x = 30;
      // Output: p1.x = 30, p2.x = 30
    

Differences Between Primitive and Non-Primitive Data Types [ Table Format ]

FeaturePrimitive Data TypesNon-Primitive Data Types
Exampleslong, int, char, boolean, floatUser-defined classes, predefined classes
Memory AllocationStack memoryHeap memory
Variable TypeNormal variables (contain actual values)Reference variables (point to memory locations)
AssignmentCreates a new memory locationReferences the same memory location
InitializationMust be explicitly initializedMembers get default values if uninitialized
AccessDirectlyUsing dot operator (e.g., p.x)
Default ValuesCompiler error if uninitializedIntegers: 0, Boolean: false, Floating point: 0.0, References: null

Interview Questions:

  1. What are primitive data types in Java?

    • Examples: long, int, char, boolean, float.

    • Basic types provided by the language, stored in stack memory.

  2. What are non-primitive data types?

    • Examples: User-defined classes, predefined classes like System.

    • Stored in heap memory, created using new operator.

  3. How do primitive and non-primitive data types differ in terms of memory allocation?

    • Primitives: Stack memory.

    • Non-Primitives: Heap memory.

  4. What is the difference between a reference and a normal variable?

    • Reference: Points to a memory location (used by non-primitives).

    • Normal Variable: Directly contains its value (used by primitives).

  5. What happens when you assign one non-primitive variable to another?

    • Both variables reference the same memory location.
  6. How are uninitialized members of non-primitive types handled?

    • They get default values (e.g., 0 for integers, false for boolean).
  7. What is the result of trying to access an uninitialized primitive variable?

    • Compiler error.
  8. Explain the use of the new operator in creating non-primitive variables.

    • Allocates memory on the heap for the non-primitive type and returns a reference.
  9. Can you give an example where changing a non-primitive variable's member affects another variable?

    • Yes, if p2 is assigned to p1, changing p2.x also changes p1.x because both refer to the same memory location.
  10. Why do non-primitive types have default values for their members, while primitive types do not?

    • Non-primitives are reference types and can point to null or default initialized values, whereas primitives must be explicitly initialized to prevent undefined behavior.

Did you find this article valuable?

Support YASH BLOGS by becoming a sponsor. Any amount is appreciated!