java 1.70 65 b20_java开始到熟悉63-65

本次内容:java常用类

1、包装类

35059b8c7e0290e30e42cf8919fa3b4e.png

e7bd860c95b2c332bbb3e135095e492d.png

a7fc732a3cd72b7d378a245503851529.png

1 package array;2

3 public classwrapperclass {4 public static voidmain(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

3aafe826be328783bcf44a52f8dff314.png

类之间的继承关系如上。

1f6ab57bed3660cc5def352780bb82b0.png

继承Number类的类

d9dc38c8d60e8e4b641cc70682856748.png

16a3ebb58fb8a2e23c96cad0c88e5172.png

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

3c7dff1a945c5d2866913a589339150b.png

1 package array;2 /**3 * 自动装箱,拆箱4 * @author acer5 *6 */

7 public classautobox {8 public static voidmain(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 classautobox {4 public static voidmain(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 acer5 *6 */

7 public classauobox1 {8 public static voidmain(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、时间相关的类

d9fbfd654139754dca0b7346997dba68.png

1 package array;2

3 import java.util.Date;4

5

6 public classdata {7 public static voidmain(String[] args)8 {9 Date time=newDate();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()

运行结果:

1398870140885

30 Apr 2014 15:02:20 GMT

1 Jan 1970 00:00:01 GMT

1000

false

494248758acd4163e9953131e527b640.png

51c5f3c21a3b41e72f445b9797d3d954.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值