Java——八種基礎資料型別 (Elementary Data Type)(常用類)

來源:互聯網
上載者:User

標籤:基本   val   數字   功能   sdi   錯誤   字串轉換   資料類型   堆記憶體   

  • 裝箱和拆箱
  1. 裝箱:基礎資料型別 (Elementary Data Type)轉為封裝類
  2. 拆箱:封裝類轉為基礎資料型別 (Elementary Data Type)
  3. jdk1.5(即jdk5.0)之後的版本都提供了自動裝箱和自動拆箱功能
  • 基礎資料型別 (Elementary Data Type)的封裝類
  • 舉兩個例子,看一下
 1 public class Demo01 { 2      3     public static void main(String[] args) { 4          5          6          7         int i = 3;//基礎資料型別 (Elementary Data Type) 8         Integer i1 = new Integer(i);//封裝類  裝箱 9         System.out.println(i);10         System.out.println(i1);11         12         13         //把字串的100 轉成 數位100 14         String s = "100";15         //String s = "abc";  錯誤的, java.lang.NumberFormatException16         Integer i2 = new Integer(s);17         System.out.println(i2);18         19         20         int i3 = i1.intValue();//拆箱21         System.out.println(i3);22         23         24         // s -- > int25         int i4 = Integer.parseInt(s);//將字串轉換為數位方式26         System.out.println(i4);27         28         29         30         //jdk 1.5 後 實現自動的裝箱和拆箱31         int j = 5;32         33         Integer j1 = j; // 自動裝箱  //Integer j3 = new Integer(j);34         35         int j2 = j1;  // 自動拆箱36         37         38         //列印int類型的最大值和最小值39         System.out.println(Integer.MAX_VALUE);40         System.out.println(Integer.MIN_VALUE);41         42         43         //進位轉換44         //十進位轉十六進位45         System.out.println(Integer.toHexString(1000));46         //十進位轉八進位47         System.out.println(Integer.toOctalString(9));48         //十進位轉二進位49         System.out.println(Integer.toBinaryString(3));50         51         52         53         Integer ii1 = new Integer(1234);//堆記憶體中取54         Integer ii2 = 1234;//去方法區中找55         int ii3 = 1234; //ii1 拆箱  int56         57         System.out.println(ii1 == ii3);//T58         59         //雖然屬性值相同, 但是引用的地址不同, “==” 比較的是引用的地址60         System.out.println(ii1==ii2);//F61         62         //Integer 類中重寫了equals方法, 比較的是屬性值63         System.out.println(ii1.equals(ii2));//T64         65         66         //byte  [-128 - 127] 67         68         Byte b1 = -123;69         Byte b2 = -123;70         71         System.out.println(b1 == b2);72         System.out.println(b1.equals(b2));73         74         75         76     }
 1 public class Demo02_Character { 2      3     public static void main(String[] args) { 4          5         System.out.println((int)‘1‘); 6          7         char c1 = ‘A‘; 8         char c2 = 49; 9         System.out.println("c2 = " + c2);10         11         Character c3 = c1; //Character c4 = new Character(c1);12         13         System.out.println(Character.isDigit(c1));//判斷字元是否為數字    F14         System.out.println(Character.isLetter(c1));//判斷字元是否為字母  T15         System.out.println(Character.isLowerCase(c1));//判斷是否為小寫字母  F16         System.out.println(Character.isUpperCase(c1));//判斷是否為大寫字母  T17         18         System.out.println(Character.toLowerCase(‘C‘));//大寫轉小寫   c19         System.out.println(Character.toUpperCase(‘a‘));//小寫轉大寫   A20         21     }
  • 對於byte/short/long/float/double和Integer(int)類用法類似

 

Java——八種基礎資料型別 (Elementary Data Type)(常用類)

聯繫我們

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