Java八種基本數據類型的大小,以及封裝類,自動裝箱/拆箱的用法?


參考:http://blog.csdn.net/mazhimazh/article/details/16799925

 

1. Java八種基本數據類型的大小,以及封裝類,自動裝箱/拆箱的用法?

 原始類型-大小-包裝類型

 (1) char-2B-Character

   booelan-1B-Boolean

(2) byte-1B-Byte

   short-2B-Short

   int-4B-Integer

   long-8B-Long

(3) float-4B-Float

   double-8B-Double

 

 從Java 5開始,引入了自動裝箱/拆箱機制,使得二者可以互換,細節值得注意:

 

 1 public class Solution {  2 
 3     public static void main(String[] args) {  4 
 5         Integer a = new Integer(3);  6 
 7         Integer b = 3; // 將3自動裝箱成Integer類型,new一個Integer對象
 8         Integer c = 3; // 如果整型字面量的值在-128到127之間,那么不會new新的Integer對象,而是直接引用常量池中的Integer對象
 9 
10         int d = 3; 11 
12         Integer e = 200; 13         Integer f = 200; 14 
15         System.out.println(a == b); // a和b是不同對象的引用,返回false
16         System.out.println(b == c); // 3在-128到127之間,故c裝箱時不再new對象,b和c指向同一個對象,返回true
17         System.out.println(b == d); // 自動拆箱,基本類型的比較,返回true
18         System.out.println(e == f); // 200不在-128到127之間,故e和f分別指向不同的對象,返回false
19 
20  } 21 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM