包装类

2020-09-05  本文已影响0人  有腹肌的豌豆Z

为什么需要包装类(Wrapper Class)

包装类和基本数据类型的关系

包装类的继承关系

包装类的基本操作

具体操作

public class TestInteger {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       Integer i1=new Integer(123);
       Integer i2 = new Integer("123");
       System.out.println("i1=i2:"+(i1==i2));//false
       System.out.println("i1.equals(i2):"+i1.equals(i2));
       System.out.println(i2);
       System.out.println(i2.toString());//说明重写了toString方法
       Integer i3=new Integer(128);
       System.out.println(i1.compareTo(i3));//-1
       System.out.println(i1.compareTo(i2));//0
       System.out.println(i3.compareTo(i2));//1
       //(1)Integer-->int    包装对象.intValue()
       int i=i1.intValue();
       System.out.println(Integer.max(10, 20));//返回最大值
       //(2)String -->int  包装类类名.parseInt(String s)
       int ii=Integer.parseInt("234");
       //(3)int -->Integer
       Integer i4=Integer.valueOf(123);
       //(4)int-->String
       String str=ii+"";
       String s=String .valueOf(ii);
       //(5)String-->Integer;
       Integer i5=new Integer("345");
       //(6)Integer-->String
       String ss=i5.toString();
       System.out.println(ss);
    }

}
上一篇 下一篇

猜你喜欢

热点阅读