Java 中哪個包包含 Wrapper 類?
Java 在 java.lang 包中提供了一些稱為包裝類的類。這些類的物件在其內部包裝基本資料型別。以下是基本資料型別及其各自類的列表 -
| 基本資料型別 | 包裝類 |
|---|---|
| char | Character |
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| boolean | Boolean |
包
Java 中的包裝類屬於 java.lang 包,因此在使用它們時無需顯式匯入任何包。
示例
以下 Java 示例接受使用者的各種基本變數並建立它們各自的包裝類。
import java.util.Scanner;
public class WrapperClassesExample {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer value: ");
int i = sc.nextInt();
//Wrapper class of an integer
Integer obj1 = new Integer(i);
System.out.println("Enter a long value: ");
long l = sc.nextLong();
//Wrapper class of a long
Long obj2 = new Long(l);
System.out.println("Enter a float value: ");
float f = sc.nextFloat();
//Wrapper class of a float
Float obj3 = new Float(f);
System.out.println("Enter a double value: ");
double d = sc.nextDouble();
//Wrapper class of a double
Double obj4 = new Double(d);
System.out.println("Enter a boolean value: ");
boolean bool = sc.nextBoolean();
//Wrapper class of a boolean
Boolean obj5 = new Boolean(bool);
System.out.println("Enter a char value: ");
char ch = sc.next().toCharArray()[0];
//Wrapper class of a boolean
Character obj6 = new Character(ch);
System.out.println("Enter a byte value: ");
byte by = sc.nextByte();
//Wrapper class of a boolean
Byte obj7 = new Byte(by);
System.out.println("Enter a short value: ");
short sh = sc.nextShort();
//Wrapper class of a boolean
Short obj8 = new Short(sh);
}
}輸出
Enter an integer value: 254 Enter a long value: 4444445455454 Enter a float value: 12.324 Enter a double value: 1236.22325 Enter a boolean value: true Enter a char value: c Enter a byte value: 125 Enter a short value: 14233
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP