java开始到熟悉63-65

           本次内容:java常用类

1、包装类

 1 package array;
 2 
 3 public class wrapperclass {
 4     public static void main(String[] args)
 5     {
 6         Integer i=new Integer(100);
 7         System.out.println(i);
 8         System.out.println(Integer.MAX_VALUE);
 9         System.out.println(Integer.MIN_VALUE);
10         Integer i2=new Integer("234");
11         System.out.println(i2);
12         System.out.println(234+10);
13         Integer i3=Integer.parseInt("333");
14         System.out.println(i3+23);
15     }
16 }

运行结果:
100
2147483647
-2147483648
234
244
356

类之间的继承关系如上。

继承Number类的类

a.下面讲解自动装箱与拆箱和缓存问题

 1 package array;
 2 /**
 3  * 自动装箱,拆箱
 4  * @author acer
 5  *
 6  */
 7 public class autobox {
 8     public static void main(String[] args)
 9     {
10         Integer a=1000;//JDK5.0之后,自动装箱,编译器帮我们改进代码:该句等价于Integer a=new Integer(1000);
11         int b=new Integer(2000);//自动拆箱,编译器帮我们改进到吗,该句等价于new Integer(2000).inValue();
12         System.out.println(b);
13     }
14 
15 }

运行结果:
2000

b.下面用代码证明编译器自动调用了方法:

 1 package array;
 2 
 3 public class autobox {
 4     public static void main(String[] args)
 5     {
 6         Integer a=null;
 7         int b=a;
 8     }
 9 
10 }

运行结果:
Exception in thread "main" java.lang.NullPointerException
 at array.autobox.main(autobox.java:14)

没有对象而且调用对象,所以报错了。

c.缓存问题

 1 package array;
 2 /**
 3  * 自动装箱,拆箱的缓存问题
 4  * @author acer
 5  *
 6  */
 7 public class auobox1 {
 8     public static void main(String[] args)
 9     {
10         Integer a1=1234;
11         Integer a2=1234;
12         System.out.println(a1==a2);
13         System.out.println(a1.equals(a2));
14         System.out.println("*******************");
15         Integer a3=123;//[-128,127]之间的数,依然按照基本数据类型处理;因为基本数据类型处理效率高
16         Integer a4=123;
17         System.out.println(a3==a4);;
18         System.out.println(a3.equals(a4));
19     }
20 }

运行结果:
false
true
*******************
true
true

这是JDK的规定,暂时当概念记住。不过依然封装,只是当做基本数据类型处理,的确处理效率提高。

2、时间相关的类

 1 package array;
 2 
 3 import java.util.Date;
 4 
 5 
 6 public class data {
 7     public static void main(String[] args)
 8     {
 9         Date time=new Date();
10         long t=System.currentTimeMillis();
11         System.out.println(t);
12         System.out.println(time.toGMTString());
13         Date time2=new Date(1000);
14         System.out.println(time2.toGMTString());//加横线表示不建议使用
15         time2.setTime(1000);
16         System.out.println(time2.getTime());
17         System.out.println(time.getTime()<time2.getTime());
18     }
19 }

运行结果:
1398870140885
30 Apr 2014 15:02:20 GMT
1 Jan 1970 00:00:01 GMT
1000
false

原文地址:https://www.cnblogs.com/xiaojingang/p/3702372.html