栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA基础10-包装类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA基础10-包装类

包装类
  1. 根据八种基本数据类型定义相应的引用类型-包装类
  2. 当基本数据类型有了类的特点后就可以调用类中的方法了
基本数据类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
booleanBoolean
charCharacter
基本类型,包装类,String类之间转换 基本数据类型转包装类
  1. 调用包装类构造器
  2. 示例:
public class WrapperTest {
    public static void main(String[] args) {
        int i1=10;
        Integer integer1=new Integer(i1);
        System.out.println(integer1.toString());
        //10
        Integer integer2=new Integer("10");
        System.out.println(integer2.toString());
        //10 字符串也可转,但字符串内必须是数字
        
        Boolean b1=new Boolean(true);
        System.out.println("b1" +b1);
        //b1true
        Boolean b2=new Boolean("true" );
        System.out.println("b2" +b2);
        //b2true 这里字符串也可转
        Boolean b3=new Boolean("true123");
        System.out.println("b3"+b3);
        //b3false 只有字符串"true"转出来时true,其他都为false
       
        Order o1=new Order();
        System.out.println(o1.isOK1);
        //null
        System.out.println(o1.isOk2);
        //false
    }

}
class Order{
    Boolean isOK1;// 类Boolean未初始化null
    boolean isOk2;// 基本数据类型未初始化false
}
包装类转为基本数据类型
  1. 调用包装类的XXXValue()
  2. 示例:
public class BasicTest {
    public static void main(String[] args) {
        Integer integer1=new Integer(10);
        int i1=integer1.intValue();
        System.out.println(i1 + 1);
        //11 类是不能做运算的,必需先转成基本数据类型
    }
    
}
  1. JDK 5.0新特性:自动装箱与自动拆箱
public class BasicTest {
    public static void main(String[] args) {
     	BasicTest basicTest=new BasicTest();
        int i2=20;
        basicTest.method(i2);
        // 自动转化为Object类的子类Integer
        // 20

        //自动装箱
        int num3=30;
        Integer integer3=num3;
        System.out.println(integer3.toString());
        // 30
       
        //自动拆箱
        int num4=integer3;
        System.out.println("num4" + num4);
        // num4 30 

    }
    public void method(Object object){
        System.out.println(object);
    }
}
基本数据类型,包装类转换为String类型
  1. 调用String的重载valueOf()
  2. 也可以直接运算基本数据类型+ “”
  3. 示例:
public class StringTest {
    public static void main(String[] args) {
        int num=10;
        String str1=num +"";
        System.out.println(str1);
        //10
        float f1=12.3f;
        String str2=String.valueOf(f1);
        System.out.println(str2);
        //12.3
    }

}
String转换为基本数据类型,包装类
  1. 调用包装类的parseXXX()
  2. 示例:
public class StringTest {
    public static void main(String[] args) {
        String str3="123";
        int num2=Integer.parseInt(str3);
     	System.out.println(num2);
     	 //123
        String str4="true";
        boolean b1=Boolean.parseBoolean(str4);
        System.out.println(b1);
         //true
        String str5="true5";
        boolean b2=Boolean.parseBoolean(str5);
        System.out.println(b2);
        //false 这里与前面基本转包装一样,只有"true"为true,其他都是false
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/890508.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号