[學習筆記]Java基礎資料型別 (Elementary Data Type)封裝類__Java

來源:互聯網
上載者:User
概述 根據Java萬物皆對象的思想,將基礎資料型別 (Elementary Data Type)封裝成對象的好處,在於可以在對象中定義更多的功能方法操作該資料。基礎資料型別 (Elementary Data Type)對象封裝類的最常見作用,就是用於基礎資料型別 (Elementary Data Type)和字串類型之間做轉換。
對應關係 資料類型  封裝類 byte     Byte short    Short int      Integer long     Long boolean  Boolean float    Float double   Double char     Character
字串與數實值型別的轉換(以int為例) 1. 字串 -> 數值(只有Character沒有解析方法) static int  parseInt(String s) static int  parseInt(String s, int radix)
2. 數值 -> 字串 static String  String.valueOf(int i) static String  toString(int i) static String  toString(int i, int radix) static String  toBinaryString(int i) static String  toHexString(int i) static String  toOctalString(int i)
3. 數值/字串 -> 封裝類對象 構造器:Integer(int/String) static Integer  valueOf(int i) static Integer  valueOf(String s) static Integer  valueOf(String s, int radix)
4. 封裝類對象 -> 數值/字串 int  intValue(); String  toString();
樣本
 // 1. 字串 -> 數值 
int num1 = Integer.parseInt("100"); // 100 
int num2 = Integer.parseInt("100", 16); // 256 // 可以使用該方法轉換其他進位到十進位 // 解析異常:NumberFormatException   // 2. 數值 -> 字串 String str1 = Integer.toString(100); // "100" String str2 = Integer.toString(100, 4); // "1210" String str3 = Integer.toBinaryString(100); // "1100100" String str4 = Integer.toOctalString(100); // "144" String str5 = Integer.toHexString(100); // "64" // 可以轉換十進位到其他進位 String str6 = String.valueOf(100); // "100"   // 3. 數值/字串 -> 封裝類對象 Integer inte1 = 
new Integer(100); // 100 Integer inte2 = Integer.valueOf(100); // 100 Integer inte3 = 
new Integer( "100" ); // 100 Integer inte4 = Integer.valueOf("100"); // 100 Integer inte5 = Integer.valueOf("100", 4); // 16   // 4. 封裝類對象 -> 數值/字串 
int no = inte1.intValue(); // 100 String str = inte1.toString(); // "100" 

自動裝箱拆箱(Java 5支援) Integer i = 10; // 自動裝箱,相當於:Integer i = Integer.valueOf(10); i = i + 1; // 自動拆箱,相當於:i = Integer.valueOf(i.intValue() + 1);
記憶體駐留機制(享元模式) 在自動裝箱時,如果數值範圍在byte範圍內,則共用記憶體空間,類似於String常量。 Integer a1 = 127; Integer a2 = 127; System.out.println( a1 == a2); // true
Integer b1 = 128; Integer b2 = 128; System.out.println( b1 == b2); // false
案例
 
package wrapper;   
import java.util.Arrays;   
public 
class WrapperTest {   
private 
static 
final String SPACE = " ";   
public 
static 
void main(String[] args) { /* * 對一字串中資料進行排序 * 思路: * 1. 將字串轉換為數組。 * 2. 對數組進行排序。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.