Why Java Collections Cannot Directly Store Primitives Types?
Last Updated :
02 Jun, 2021
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:
- Autoboxing
- 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
import java.io.*;
class GFG {
public static void main(String args[])
{
Integer i = new Integer( 21 );
Integer j = 5 ;
System.out.println( "i=" + i + "\n j=" + j);
}
}
|
Output:
i=21
j=5
Illustration 2: Unboxing
Java
import java.io.*;
public class GFG {
public static void main(String args[])
{
Integer i = new Integer( 50 );
int a = i;
int b = i.intValue();
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> list = new ArrayList<Integer>();
for ( int i = 0 ; i < 10 ; i++) {
list.add(i);
System.out.println(i);
}
}
}
|
Output
0
1
2
3
4
5
6
7
8
9
Example 2: Collections to store Primitive datatype
Java
import java.util.HashMap;
import java.util.Map;
public class GFG {
public static void main(String[] args) throws Exception
{
Map map = new HashMap();
Integer var = new Integer( 21 );
map.put( "key" , var);
Integer refVar = (Integer)map.get( "key" );
int i = refVar.intValue();
System.out.print( "Successfully compiled and executed" );
}
}
|
Output:
Successfully compiled and executed
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...