Java基礎——基本型別包裝類
阿新 • • 發佈:2022-03-22
一、概述:
將基本資料型別封裝成物件
優點:
可以在物件中定義更多的功能方法操作該資料
常見用法:
用於基本型別與字串之間的轉換
基本資料型別 | 包裝類 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
二、Integer類
概述:包裝一個物件中的原始型別int的值
方法名 | 說明 |
---|---|
public Integer(int value) | 根據int值建立Integer物件(過時) |
public Integer(String s) |
根據String值建立Integer物件(過時)(字串應是數字組成的) |
public static Integer valueOf(int i) | 返回表示指定的inter值的Integer例項 |
public static Integer valueOf(String s) | 返回一個儲存指定值的Integer物件String(NumberFormatException不是數字字串報的錯誤) |
三、int與String之間的相互轉換
-
int 轉換成 String
public static String Valueof(int i):返回int 引數的字串形式,該方法是String類中的方法
//int 轉換成字串
int num=100;
//方式一
String s=""+num;//進行拼接將int型別轉換成String
System.out.println(s);
//方式2:public static String valueOf(int i)
String s1=String.valueOf(num);
System.out.println(s1);2.String 轉換成int
public static int praseInt(String s):將字串解析為int型別,該方法是integer類中的方法
//String 轉換成int
String s2="100";
//方式一: String-integer-int
Integer i=Integer.valueOf(s2);
// public int intValue(),該方法不是靜態,通過物件來呼叫
int x=i.intValue();
System.out.println(x);
//方法二:public static int praseInt(String s)
int x1=Integer.parseInt(s2);
System.out.println(x1); -