• Java 基础(包装类 Wrapper 的使用)


    针对八种基本数据类型定义相应的引用类型--包装类(封装类)
    有了类的特点,就可以调用类中的方法,Java才是真正的面向对象

    基本数据类型 包装类
    byte Byte
    short Short
    int Integer
    long Long
    float Float
    double Double
    boolean Boolean
    char Character

    Byte, Short, Integer, Long, Float, Double 父类是 Number

    基本数据类型包装成包装类的实例 ---装箱

    • 通过包装类的构造器实现:
    int i = 500; Integer t = new Integer(i);
    
    • 还可以通过字符串参数构造包装类对象:
    Float f = new Float("4.56");
    Long l = new Long("asdf"); //NumberFormatException
    

    获得包装类对象中包装的基本类型变量 ---拆箱

    • 调用包装类的 .xxxValue() 方法:
      boolean b = bObj.booleanValue();

    JDK1.5之后,支持自动装箱,自动拆箱。但类型必须匹配。

    基本类型,包装类与 String 类间的转换

    举例

    WrapperTest.java

    package com.klvchen.java2;
    
    import org.junit.jupiter.api.Test;
    
    public class WrapperTest {
    	
    	//String 类型 ---> 基本数据类型,包装类
    	@Test
    	public void test5() {
    		String str1 = "123";
    		//错误情况
    //		int num1 = (int)str1;
    //		Integer in1 = (Integer)str1;
    		
    		//可能会报 NumberFormatException
    		int num2 = Integer.parseInt(str1);
    		System.out.println(num2 + 1);  //124
    		
    		String str2 = "true1";
    		boolean b1 = Boolean.parseBoolean(str2);
    		System.out.println(b1);        //false
    	}
    	
    	
    	//基本数据类型,包装类 ---> String 类型
    	@Test
    	public void test4() {
    		
    		int num1 = 10;
    		//方式1: 连接运算
    		String str1 = num1 + "";
    		//方式2: 调用 String 的 valueOf(XXX xxx)
    		float f1 = 12.3f;
    		String str2 = String.valueOf(f1);
    		System.out.println(str2);     //12.3
    		
    		Double d1 = new Double(12.4);
    		String str3 = String.valueOf(d1);
    		System.out.println(str3);     //12.4
    		
    		
    	}
    	
    	
    	
    	//基本数据类型 ---> 包装类的对象(JDK 5.0 的特性,自动装箱与自动拆箱)
    	@Test
    	public void test3() {
    //		int num1 = 10;
    //		method(num1);
    		
    		//自动装箱:基本数据类型 ---> 包装类
    		int num2 = 10;
    		Integer in1 = num2; //自动装箱
    		
    		boolean b1 = true;
    		Boolean b2 = b1;    //自动装箱
    		
    		//自动拆箱: 包装类 ---> 基本数据类型
    		System.out.println(in1.toString());  //10
    		
    		int num3 = in1;  //自动拆箱
    	}
    	
    	public void method(Object obj) {
    		System.out.println(obj);
    	}
    	
    	//包装类 ---> 基本数据类型:调用包装类的 xxxValue()
    	@Test
    	public void test2() {
    		Integer in1 = new Integer(12);
    		
    		int i1 = in1.intValue();
    		System.out.println(i1 + 1);  //13
    		
    		Float f1 = new Float(12.3);
    		float f2 = f1.floatValue();
    		System.out.println(f2 + 1);  //13.3
    		
    	
    				
    	}
    	
    	//基本数据类型 ---> 包装类
    	@Test
    	public void test1() {
    		
    		int num1 = 10;
    //		System.out.println(num1.toString()); //基本数据类型,无法调用方法
    		Integer in1 = new Integer(num1);
    		System.out.println(in1.toString());  //10
    		
    		Integer in2 = new Integer("123");
    		System.out.println(in2.toString());  //123
    		
    		//报异常 java.lang.NumberFormatException
    //		Integer in3 = new Integer("123abc");
    //		System.out.println(in3.toString());
    		
    		Float f1 = new Float(12.3f);  
    		Float f2 = new Float("12.3"); 
    		System.out.println(f1);    //12.3
    		System.out.println(f2);    //12.3
    		
    		Boolean b1 = new Boolean(true);
    		Boolean b2 = new Boolean("true");
    		System.out.println(b1);    //true
    		System.out.println(b2);    //true
    		
    		Boolean b3 = new Boolean("true123");
    		System.out.println(b3);   //false
    		
    		Order order = new Order();
    		System.out.println(order.isMale);  //false
    		System.out.println(order.isFemale);  //null
    		
    	}
    
    }
    
    class Order{
    	
    	boolean isMale;
    	Boolean isFemale;
    }
    

    总结

    基本数据类型 ---> 包装类
    自动装箱:

    eg: int num2 = 10;
        Integer in1 = num2;
    

    包装类 ---> 基本数据类型
    自动拆箱:

    eg: Integer in1 = new Integer(12);
        int num2 = in1;
    

    基本数据类型,包装类 ---> String 类型
    调用 String 的 valueOf(XXX xxx)

    eg: float f1 = 12.3f;
        String str2 = String.valueOf(f1);
    

    String 类型 ---> 基本数据类型,包装类
    调用对应方法的包装类的 parseXxx(Sting)

    eg: String str1 = "123";
        int num2 = Integer.parseInt(str1);
    
  • 相关阅读:
    [转载]openerp 6.0.2库存业务
    [转载]OPENERP 6.0.2中的财务概念
    负载均衡
    SQL Server高可用性部署实施 SQL server 群集
    在苹果lion中安装xcode3系列版本
    MacBook 以及 XCode 使用小结
    C++必知的几个知识点
    最新 xcode 4.2 ios 5.0无证书发布ipa文件
    负载参考
    SQLSERVER 2005 表分区说明
  • 原文地址:https://www.cnblogs.com/klvchen/p/14455517.html
Copyright © 2020-2023  润新知