为什么Java集合不能直接存储基元类型?
原始类型是Java语言中可用的最基本的数据类型。此类类型仅用于一个目的——包含一种纯粹、简单的值。由于Java是一种静态类型语言,其中每个变量和表达式类型在编译时都是已知的,因此您不能为此类原始类型定义新操作。
插图:
Invalid : vector.addElement(3) ;
Valid : vector.addElelment("3") ;
结论:
- Java原始类型不是引用类型。例如,int 不是一个对象。
- Java使用引用类型的类型擦除来进行泛型。例如, A List> 在运行时实际上是 List
收藏 是用于存储和操作一组对象的框架。 Java Collection 表示单个对象单元。由于 以上两个说法都是对的,泛型Java集合不能直接存储原始类型。
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 |
自动装箱是将原始类型自动转换为其相应包装类的对象,称为自动装箱。例如:
- int 到 Integer 的转换
- long 到 Long 的转换
- double 到 Double 的转换等。
拆箱只是自动装箱的逆过程。将包装类的对象自动转换为其相应的原始类型称为拆箱。例如 – Integer 到 int 的转换,Long 到 long 的转换,Double 到 double 的转换等。
插图:自动装箱
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);
}
}
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);
}
}
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 list = new ArrayList();
// 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);
}
}
}
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");
}
}
输出:
i=21
j=5
图 2:拆箱
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);
}
}
输出:
a=50
b=50
实现:在使用集合Java编译器时,从原始类型创建一个包装对象,并使用泛型将其添加到集合中。
示例 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 list = new ArrayList();
// 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);
}
}
}
0
1
2
3
4
5
6
7
8
9
示例 2:用于存储原始数据类型的集合
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");
}
}
输出:
Successfully compiled and executed