Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaWhy Java Collections Cannot Directly Store Primitives Types?

Why Java Collections Cannot Directly Store Primitives Types?

Primitive types are the most basic data types available within the Java language. Such types serve only one purpose — containing pure, simple values of a kind. Since java is a Statically typed language where each variable and expression type is already known at compile-time, thus you can not define a new operation for such primitive types.

Illustration:

Invalid : vector.addElement(3) ;
Valid   : vector.addElelment("3") ;

Conclusion:

  • Java primitive types are not referenced types. For example, int is not an Object.
  • Java does generics using type-erasure of reference types. For example, A List<?> is really a List<Object> at run-time.

Collections are the framework used to store and manipulate a group of objects. Java Collection means a single unit of objects. Since the above two statements are true, generic Java collections can not store primitive types directly.

Wrapper Class provides a way to use primitive data types (int, boolean, etc..) as objects or a Wrapper class is a class whose object wraps or contains primitive data types. It gives birth to two concepts as follows:

  1. Autoboxing
  2. Unboxing
Primitive Data Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

Autoboxing is the automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. For instance:

  • Conversion of int to Integer
  • Conversion of long to Long
  • Conversion of double to Double, etc.

Unboxing is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example – conversion of Integer to int, Long to long, Double to double, etc.

Illustration: Autoboxing

Java




// Importing input output classes
import java.io.*;
 
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Custom input
        Integer i = new Integer(21);
 
        // Boxing
        Integer j = 5;
        System.out.println("i=" + i + "\n j=" + j);
    }
}


Output:

i=21
j=5

Illustration 2: Unboxing 

Java




// Import input output classes
import java.io.*;
 
// Class
public class GFG {
 
    // MAin driver method
    public static void main(String args[])
    {
 
        // Custom input
        Integer i = new Integer(50);
 
        // Unboxing
        int a = i;
 
        // Unboxing
        int b = i.intValue();
 
        // Print and display
        System.out.println("a=" + a + "\nb=" + b);
    }
}


Output: 

a=50
b=50

Implementation: While using the collection java compiler create a wrapper Object from the primitive type and adds it to the collection using generics.

Example 1: 

Java




// Java Program to illustrate Collections
// are not directly storing primitives types
 
// Importing input output classes
import java.io.*;
// Importing all classes from
// java.util package
import java.util.*;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a list of elements of Integer type.
        List<Integer> list = new ArrayList<Integer>();
 
        // Iterating over elements of List object
        for (int i = 0; i < 10; i++) {
            // Adding the int primitives type values
 
            // If elements are added  using add() method
            // then compiler automatically treats as
            // add(Integer.valueOf(i))
            list.add(i);
            //  This is what compiler does and
            // hence the goal achieved.
 
            // Print the primitive values
            System.out.println(i);
        }
    }
}


Output

0
1
2
3
4
5
6
7
8
9

Example 2: Collections to store Primitive datatype 

Java




// Java Program to illustrate Collections
// are not directly storing primitives types
 
// Importing Map and HashMap classes
// from java.util package
import java.util.HashMap;
import java.util.Map;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
 
        // Creating an object of Map type
        Map map = new HashMap();
 
        // Creating int wrapper object
        // Custom input
        Integer var = new Integer(21);
 
        // Storing int to map
        map.put("key", var);
 
        // Getting int value from map
        Integer refVar = (Integer)map.get("key");
 
        // Get the integer value from wrapper object
        int i = refVar.intValue();
       
      // Display message for successful compilation
      System.out.print("Successfully compiled and executed");
    }
}


Output: 

Successfully compiled and executed

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments