Java中的包装类及自动装箱、拆箱

一   包装类简介

1.包装类的概述

在进行类型转换的范畴内,有一种特殊的转换,需要将int这样的基本数据类型转换为对象..所有基本类型都有一个与之对应的类,即包装类.

eg:

class A{

public void sum(Object e){

将e当做基本类型对待

还是当做原先的int类型的1对待

} }

ps:

包装类是不可变类,在构造了包装类对象后,不允许更改包装在其中的值..包装类是final的,不能定义他们的子类。

2.基本类型的包装类及其父类

3.Number及其主要方法

抽象类Number是 Byte,Short,Integer,Long,Float,Double的父类。

Number的子类必须提供将表示的数值转换为 byte,short,int,long,float.double的方法.-doubleValue()以double形式返回指定的数值。

-intValue()以int形式返回指定的数值。

-floatValue()以float形式返回指定的数值。

包装类可以通过对应的方法将自己所表示的值转换成6种基本类型。

eg1:

public void test5(){

        System.out.println("基本数据类型 --> 包装类,调用包装类的构造器");

  

        // 自动装箱,直接将'基本数据类型'转化为'包装类',原来需要toString

        Integer in1 = 123;

        System.out.println(in1);

        // 自动拆箱,直接将'包装类'转化为'基本数据类型',原来需要in2.intValue()

        int int2 = in1;

        System.out.println(int2+1);

    }

eg2:基本数据类型/包装类 --> String ( valueOf )   &&  String --> 基本数据类型/包装类 (parseInt)

 public void test6(){
        int num1 = 10;
        //方式1:连接运算
        String num2 = num1 +"";
        System.out.println(num2+1);
        System.out.println("连接运算--上述运行结果为:101");
        //方式2:调用String的valueof方法
        float f2 = 12.3f;
        String str2 = String.valueOf(f2);
        System.out.println(str2);
        System.out.println("基本数据类型-调用String的valueof方法-上述运行结果为:12.3");
 
        Double d1 = 12.5;
        String str3 = String.valueOf(d1);
        System.out.println(str3);
        System.out.println("引用类型-调用String的valueof方法-上述运行结果为:12.5");
 
        // String类型转换成基本数据类型
        String str4 = "123";
        System.out.println(Integer.parseInt(str4));
        System.out.println("string类型转换成基本数据类型");
    }

二    自动拆箱与自动装箱

自动装箱 :把基本数据类型转换为对应的包装类类型
自动拆箱 :把包装类类型转换为对应的基本数据类型

 

自动装箱就是Java自动将原始类型值转换成对应的对象,比如将int的变量转换成Integer对象,这个过程叫做装箱,反之将Integer对象转换成int类型值,这个过程叫做拆箱。因为这里的装箱和拆箱是自动进行的非人为转换,所以就称作为自动装箱和拆箱。原始类型byte, short, char, int, long, float, double 和 boolean 对应的封装类为Byte, Short, Character, Integer, Long, Float, Double, Boolean。

从Java5.o版本以后加入了autoboxing.功能,自定”拆箱"和"装箱”是依靠JDK5的编译器在编译器的”预处理”工作。下列代码写法均为正确形式:

lnteger a =100; //装箱

lnteger b = 200; //装箱

lnteger c = atb;//先拆箱后装箱double d= c;//拆箱

思考? 一个基本类型是否可以转换成任意包装类型?

可以的,可能会造成精度损失和溢出。

 

三   Integer

Integer 类概述 :包装一个对象中的原始类型 int 的值
Integer 类构造方法如下:
           方法名                                                               说明
   public Integer(int value)                              根据 int 值创建 Integer 对象 ( 过时 )
    public Integer(String s)                               根据 String 值创建 Integer 对象 ( 过时 )
    public static Integer valueOf(int i)               返回表示指定的 int 值的 Integer 实例
   public static Integer valueOf(String s)        返回一个保存指定值的 Integer 对象 String
示例代码
public class IntegerDemo {
   public static void main ( String [] args ) {
  //public Integer(int value) :根据 int 值创建 Integer 对象 ( 过时 )
      Integer i1 = new Integer ( 100 );
      System . out . println ( i1 );
     //public Integer(String s) :根据 String 值创建 Integer 对象 ( 过时 )
      Integer i2 = new Integer ( "100" );
      // Integer i2 = new Integer("abc"); //NumberFormatException
      System . out . println ( i2 );
     System . out . println ( "--------" );
     //public static Integer valueOf(int i) :返回表示指定的 int 值的 Integer 实例
     Integer i3 = Integer . valueOf ( 100 );
    System . out . println ( i3 );
    //public static Integer valueOf(String s) :返回一个保存指定值的 Integer 对象
    String   Integer i4 = Integer . valueOf ( "100" );
    System . out . println ( i4 );
} }
Ps:
01-----int 转换为 String
   转换方式 :
     方式一:直接在数字后加一个空字符串
     方式二:通过 String 类静态方法 valueOf()
示例代码
  public class IntegerDemo {
   public static void main ( String [] args ) {
   //int --- String
   int number = 100 ;
   // 方式 1
   String s1 = number + "" ;
   System . out . println ( s1 );
   // 方式 2
   //public static String valueOf(int i)
   String s2 = String . valueOf ( number );
   System . out . println ( s2 );
  System . out . println ( "--------" );
} }
02-----String 转换为 int
 转换方式
    方式一:先将字符串数字转成 Integer ,再调用 valueOf() 方法
    方式二:通过 Integer 静态方法 parseInt() 进行转换
示例代码 :
public class IntegerDemo {
    public static void main ( String [] args ) {
     //String --- int
    String s = "100" ;
   // 方式 1 String --- Integer --- int
   Integer i = Integer . valueOf ( s );
   //public int intValue()
   int x = i . intValue ();
   System . out . println ( x );
   // 方式 2
   //public static int parseInt(String s)
   int y = Integer . parseInt ( s );
   System . out . println ( y );
} }
 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值