Java中String.valueOf()方法的解釋

來源:互聯網
上載者:User

標籤:字元   except   等於   字串   tostring   char   boolean   解釋   需要   

1. 由 基本資料型態轉換成 String 
String 類別中已經提供了將基本資料型態轉換成 String 的 static 方法 
也就是 String.valueOf() 這個參數多載的方法 
有下列幾種 
String.valueOf(boolean b) : 將 boolean 變數 b 轉換成字串 
String.valueOf(char c) : 將 char 變數 c 轉換成字串 
String.valueOf(char[] data) : 將 char 數組 data 轉換成字串 
String.valueOf(char[] data, int offset, int count) : 
將 char 數組 data 中 由 data[offset] 開始取 count 個元素 轉換成字串 
String.valueOf(double d) : 將 double 變數 d 轉換成字串 
String.valueOf(float f) : 將 float 變數 f 轉換成字串 
String.valueOf(int i) : 將 int 變數 i 轉換成字串 
String.valueOf(long l) : 將 long 變數 l 轉換成字串 
String.valueOf(Object obj) : 將 obj 對象轉換成 字串, 等於 obj.toString() 
用法如: 
int i = 10; 
String str = String.valueOf(i); 
這時候 str 就會是 "10" 
2. 由 String 轉換成 數位基本資料型態 
要將 String 轉換成基本資料型態轉 
大多需要使用基本資料型態的封裝類別 
比如說 String 轉換成 byte 
可以使用 Byte.parseByte(String s) 
這一類的方法如果無法將 s 分析 則會丟出 NumberFormatException 
byte : 
Byte.parseByte(String s) : 將 s 轉換成 byte 
Byte.parseByte(String s, int radix) : 以 radix 為基底 將 s 轉換為 byte 
比如說 Byte.parseByte("11", 16) 會得到 17 
double : 
Double.parseDouble(String s) : 將 s 轉換成 double 
float : 
Double.parseFloat(String s) : 將 s 轉換成 float 
int : 
Integer.parseInt(String s) : 將 s 轉換成 int 
long : 
Long.parseLong(String s) : 將 s 轉換成 long

Java中String.valueOf()方法的解釋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.