๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Non-Primitive Data Types in Java  


What are Data Types ?

  • In Java, a data type specifies the type of value a variable can hold.
  • It tells the compiler how much memory to allocate and what kind of data to expect.
  • For example :
    int rollno = 101;   // 'int' is a data type for integers
    String name = "deepak";   // 'String' is a non-primitive data type for text
    int[] marks = {85, 90, 78};  // 'int[]' is an array to store multiple integers
  • There are 2 types of data types in Java :-
    • Primitive Data Types
    • Non-Primitive Data Types

Primitive Data Types

Click Here to read Primitive Data Types deeply.


Non-Primitive Data Types

  • a) Non-Primitive Data Types are user-defined or derived data types
    • Non-primitive data types are more complex structures in Java or other programming languages.
    • They are not pre-defined by the compiler but are created by programmers using classes or interfaces.
    • Non-primitive data types can store references to objects or collections of data, such as strings, arrays, or custom objects.
    • For example : String name = "Deepak";
      In this case:
      • The compiler knows that String is a class capable of storing sequences of characters.
      • There are total 6 characters in name variable, thus it will allocates 12 bytes of memory to store String value.
    • Some Points to Note :
      • Non-Primitive Data Types are created by the programmer using classes or interfaces and provide more flexibility.
      • Non-Primitive Data Types are slower and require more memory because they store references to the data instead of directly storing values.
      • Non-Primitive Data Types are objects or references to objects, offering methods and functionality beyond raw data storage.
  • b) Non-Primitive Data Types include String, Arrays, classes, interfaces etc
    • Java does not have a fixed number of non-primitive data types like it does for primitive types.
    • Java provides pre-defined non-primitive types like String and collections from the java.util package, but we can create an infinite number of non-primitive types (classes, interfaces, etc.) as needed for your application.
    • Some common non-primitive data types categories in Java are as below :-
      • Classes: User-defined types that act as blueprints for creating objects. Examples: String, Scanner, Student, etc.
      • Interfaces: Abstract types that specify methods a class must implement. Examples: List, Runnable, etc.
      • Arrays: Containers that hold multiple values of the same type. Examples: int[], String[].
      • Collections: Part of the Java Collections Framework, such as ArrayList, HashMap, HashSet, etc.
      • Enums: Fixed sets of constants. Examples: enum Day { MONDAY, TUESDAY }.
  • c) Non-Primitive Data Types have variable memory sizes
    • Non-primitive data types in Java do not have a fixed memory size like primitive types. Their memory usage depends on the data they store and the runtime environment.
    • For example :
      String name = "deepak"; // Allocates 12 bytes for storage (1 char size is 2 byte and total there are 6 characters)
    • Non-primitive types can grow or shrink in size based on the data stored. For example:-
      An ArrayList grows dynamically as elements are added.
      ArrayList numbers = new ArrayList<>();  
      numbers.add(10); // Memory adjusts as elements are added.
    • Java uses garbage collection to manage the memory of non-primitive types automatically. Unused objects are cleaned up to free memory.
      String unused = new String("Garbage");  
      unused = null; // The memory for "Garbage" is now eligible for garbage collection.