本文要点:

  • 基本数据类型的包装类
  • 字符串相关类:
    • 不可变字符序列:String
    • 可变字符序列:StringBuffer、StringBuilder
  • 时间处理相关类:
    • Date
    • DateFormat、SimpleDateFormat
    • Calendar
  • Math类
  • File类
  • 枚举类

一、基本数据类型的包装类(Wrapper Class)

 

  • 为什么需要包装类?

  Java并不是纯面向对象的语言。Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的。但是我们在实际使用中经常需要将基本数据转化成对象,便于操作。比如:集合的操作中,这时,我们就需要将基本类型数据转化成对象

  • 包装类均位于java.lang包,包装类和基本数据类型的对应关系如下:

Java基础——常用类(Date、File)以及包装类

  • 包装类的作用:
    • 提供:字符串、基本类型数据、对象之间互相转化的方式
    • 包含每种基本数据类型的相关属性,如最大值、最小值等。
  • 所有的包装类(Wrapper Class)都有类似的方法,掌握一个其他都类似。下面例子就是以Integer为例。

代码示例:

 1 public class test01 {
 2 
 3     public static void main(String[] args) {
 4 
 5         Integer i=new Integer(1000);
 6         System.out.println(Integer.MAX_VALUE);//最大值
 7         System.out.println(Integer.toHexString(i));//把i变量转成十六进制的字符串
 8         
 9         //把字符串转换成有符号十进制整数
10         Integer i2=Integer.parseInt("234");//方式1
11         System.out.println(i2);
12         System.out.println(i2+10);
13         Integer i3=new Integer("333");//方式2
14         System.out.println(i3);
15         
16         System.out.println(i2.intValue());//如i2.intValue();intValue可以把当前对象转换成真正的数字
17         
18         //整数转字符串
19         String str=234+"";
20         
21     }
22 
23 }

二、自动装箱和自动拆箱(auto-boxing & unboxing)

  • 自动装箱

基本类型就自动地封装到与它相同类型的包装中,如:

Integer i=100;

本质上是,编译器编译时为我们添加了:

Integer  i=new Integer(100);

  • 自动拆箱

包装类对象自动转换成基本类型数据,如:

 int i=new Integer(1500);

代码示例:

 1 /**@Description: 测试自动装箱和自动拆箱    
 2  * @date 2016-2-17 下午3:50:21    
 3  */
 4 public class test02 {
 5 
 6     public static void main(String[] args) {
 7         Integer i=new Integer(1000);//JDK1.5之前
 8         //自动装箱
 9         Integer i1=1000;//JDK1.5之后出现自动装箱:编译器帮我们改进代码Integer i=new Integer(1000);
10         Integer i2=2000;
11         
12         //自动拆箱
13         int i3=new Integer(1500);//自动拆箱,编译器改进:new Integer(1500).intValue();
14         int i4=i2;//i2.intValue();
15         
16         /*假设Integer i2=null;
17          * int i4=i2;
18          * 则程序运行起来会报空指针异常NullPointException*/
19         
20         Integer d=1234;
21         Integer d1=1234;
22         System.out.println(d==d1);//false:属性值相同,但是两个是不同的对象
23         System.out.println(d.equals(d1));//true:equals()比较的是两个的值intValue()

注意下面:

Java基础——常用类(Date、File)以及包装类

再者:

 1 public static void main(String[] args) {
 2         Integer a1 = Integer.valueOf(60);  //danielinbiti
 3         Integer b1 = 60;  
 4         System.out.println("1:="+(a1 == b1));   
 5         
 6         Integer a2 = 60;  
 7         Integer b2 = 60;  
 8         System.out.println("2:="+(a2 == b2));  
 9         
10         Integer a3 = new Integer(60);  
11         Integer b3 = 60;  
12         System.out.println("3:="+(a3 == b3));  
13         
14         Integer a4 = 129;  
15         Integer b4 = 129;  
16         System.out.println("4:="+(a4 == b4));  
17     }

这段代码的比较结果,如果没有执行不知道各位心中的答案都是什么。

要知道这个答案,就涉及到Java缓冲区和堆的问题。

java中Integer类型对于-128-127之间的数是缓冲区取的,所以用等号比较是一致的。但对于不在这区间的数字是在堆中new出来的。所以地址空间不一样,也就不相等。

Integer b3=60,这是一个装箱过程也就是Integer b3=Integer.valueOf(60)

所以,以后碰到Integer比较值是否相等需要用intValue()

对于Double没有缓冲区。

1:=true
2:=true
3:=false
4:=false
View Code

相关文章: