java的基本类型和其包装类

  java一共提供了八种基本数据类型,分别为:byte,short,int,long,float,double,boolean,char。他们所对应的包装类都在java.lang包中,分别对应为:Byte,Short

Integer,Long,Float,Double,Boolean,Character。

  当有的地方必须使用对象或者引用数据类型时(集合类),基本数据类型就不能使用了,因为他们的存储原理和引用数据类型是完全不一样的。把基本数据类型的变量转换为包装类型很简单,用new语句创建或调用包装类的一个静态方法valueOf(),把基本类型的变量作为参数传入就行了。

    int i = 9;   //定义变量
    Integer it = Integer.valueOf(i);  //用valueOf()方法把i转成Integer变量
    it = new Integer(i);    //构造方法创建
    
    List list =    new ArrayList();    //创建List类型的集合变量
    list.add(it);        //把封装类型的it放入list

包装类转换为基本数据类型,调用包装类对象的intValue(),shortValue()..方法

    Integer it = Integer.valueOf(i);
    it.intValue();
奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗生命在于奋斗,也为自己带盐奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗
原文地址:https://www.cnblogs.com/hardwork/p/4083583.html