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 }

 

原文地址:https://www.cnblogs.com/jiangyi-uestc/p/5678841.html