Java的学习01

记录每天的学习情况。加油。

 1 /**
 2  * 测试包装类
 3  * @author 小白
 4  *
 5  */
 6 public class TestWrappedClass {
 7         public static void main(String[] args) {
 8             
 9             //基本数据类型转成包装类对象
10             Integer a = new Integer(3);
11             Integer b = Integer.valueOf(30);
12             
13             //把包装类对象转成基本数据类型
14             int c = b.intValue();
15             double d = b.doubleValue();
16             
17             //把字符串转成包装类对象
18             Integer e = new Integer("99999");
19             Integer f = Integer.parseInt("999888");
20             
21             //把包装类对象转换成字符串
22              String str = f.toString();
23              
24              //常见的常量
25              System.out.println("int类型最大的整数:"+Integer.MAX_VALUE);
26         }
27 }
 1 /**
 2  * 测试自动拆箱,自动装箱
 3  * @author 小白
 4  *
 5  */
 6 public class TestAutoBox {
 7     public static void main(String[] args) {
 8         Integer a = 234;    //自动装箱,相当于Integer a = Integer.valueOf(234);
 9         int b = a;    //自动拆箱 ,编译器会修改成:int b = a.intValue();
10     
11         Integer c = null;
12         if(c!=null){
13             int d = c; //自动拆箱:调用了:c.intValue();
14         }
15         
16         //缓存[-128,127]之间的数字.实际就是系统初始的时候,创建了[-128,127]之间的一个缓存数组
17         //当我们调用valueOf()的时候,首先检查是否在[-128,127]之间,如果在这个范围则直接从缓存数组中拿出已经建好的对象
18         //如果不在这个范围,则创建新的Integer对象。
19         Integer in1 = -128;
20         Integer in2 = -128;
21         System.out.println(in1==in2); //true因为-128在缓存范围内
22         System.out.println(in1.equals(in2));//true
23         System.out.println("#########");
24         Integer in3 = 1234;
25         Integer in4 = 1234;
26         System.out.println(in3==in4);//false因为1234不在缓存范围内
27         System.out.println(in3.equals(in4));//true
28     }
29 
30 }
原文地址:https://www.cnblogs.com/tianqianlan/p/9877829.html