JAVA基础10-包装类

包装类

  1. 根据八种基本数据类型定义相应的引用类型-包装类
  2. 当基本数据类型有了类的特点后就可以调用类中的方法了
基本数据类型 包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

基本类型,包装类,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
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_64925940/article/details/124576471