zoukankan      html  css  js  c++  java
  • Java 包装类

    Java 基本数据的包装类

    基本数据类型包装类的定义:

    Java在设计之初的设计原则就是:“一切皆对象”,一切的操作都要求用对象的形式进行表述。

    class MyInt { // 一个类

             private int num ; //类包装的基本数据

             public MyInt(int num) {

                       this.num = num ;

             }

             public int intValue() { //将类包装的数据内容返回 >>> [拆包]

                       return this.num ;

             }

    }

    public class TestDemo {

             public static void main(String [] args) {

                       MyInt mi = new MyInt(10) ; // 将 int 包装为类

                       int temp = mi.intValue() ; // 将类对象中包装的数据取出

                       System.out.println( temp * 2 ) ;

             }

    }

    Java为了方便用户使用,专门提供了一组包装类;

    基本类型:byde(Byte)   short(Short)     int(Integer)      long(Long)        float(Float)       double(Double)         char(Character)         Boolean(Boolean)

    对象型包装类(Object直接子类):Character , Boolean

    数值型包装类(Number直接子类):Byte ,  Short , Integer , Long , Float

    Number是一个抽象类,里面一共定义了六个操作方法:

    IntValue() , doubleValue() , floatValue() , longValue() , byteValue() , shortValue()

    装箱与拆箱操作

    ····【JDK 1.5版本】

             ·装箱操作:将基本数据类型变为包装类的形式;

                       每个包装类的构造方法,都可以接收各自的数据类型的变量

             ·拆箱操作:从包装类当中取出被包装的数据;

                       利用Number类中提供的一系列: xxxValue()方法完成

    public class TestDemo {

             public static void main(String [] args) {

                       Integer obj = new Integer(10) ; //将基本类型装箱

                       int temp = obj.intValue() ; // 将基本数据类型拆箱

                       System.out.println( temp * 2 ) ;

             }

    }//将上一个MyInt类,变为了Integer系统包装类

    public class TestDemo {

             public static void main(String [] args) {

                       Boolean obj = new Boolean(10) ; //将基本类型装箱

                       boolean temp = obj.Boolean() ; // 将基本数据类型拆箱

                       System.out.println( temp ) ;

             }

    }// 即使不是Number子类的方法,但是也在Object中沿用了这个 装箱和拆箱的形式

    ···【JDK 1.5+版本】

             Java在1.5+后加入了自动装箱和自动拆箱的机制,并且可以直接使用包装类的对象进行数学计算。

    public class TestDemo {

             public static void main(String [] args) {

                       Integer obj = 10 ; //自动装箱

                       int temp = obj ; // 自动拆箱

                       obj ++ ; // 包装类直接进行数学计算

                       System.out.println( temp * obj ) ;

             }

    }//使用自动装箱、拆箱和数学计算,就省去了手工拆箱的部分

    两者不同:

    public class TestDemo {

             public static void main(String [] args) {

                       Integer obja = 10 ;

                       Integer objb = 10 ;

                       Integer objc = new Integer(10) ;

                       System.out.println(obja == objb) ;   // true

                       System.out.println(obja == objc) ;  // falue

                       System.out.println(objb == objc) ;   // falue

    System.out.println(obja.equals(objc)) ;   // true

             }

    }

    ~

             在使用包装类的时候,很少使用构造方法完成,几乎全部使用直接赋值(与String相同),但是在判断内容是否相等的时侯,一定要使用.equals()方法。

             Object可以接收一切的引用数据类型……

    public class TestDemo {

             public static void main(String [] args) {

                       Object obj = 10 ; //先包装再转换

                       //Object不可以直接向下转型为int

                       int temp = (Integer) obj ;  //向下变为Integer后,再执行自动拆箱

                       System.out.println( temp * 2 ) ;

             }

    }

    基本数据类型 —— 自动装箱(成为对象) —— 向上转型为Object

    数据类型转换(核心):

    在包装类中提供将String类转换为普通数据类型的方法

    ·Integer类:public static int parseInt(String s)

    ·Double类:public static double parseDouble(String s)

    ·Boolean类:public static boolean  parseBoolean(String s)

    ~~Character类中并不存在String变为字符的方法,因为String中有一个chatAt()的方法根据索引取出字符内容。

    public class TestDemo {

             public static void main(String [] args) {

                       String str = "123" ; //字符串型

                       int temp = Integer.parseInt(str) ;    //使用parseInt将str转为int型

                       System.out.println( temp * 2 ) ;

             }

    }

  • 相关阅读:
    MySQL数据库8(十九)外键
    MySQL数据库8(十八)用户权限管理
    HTML基本样式
    HTML5之部分新特性
    MySQL数据库8(十七)数据库的备份还原
    MySQL数据库8(十六)子查询
    MySQL数据库(十五)联合查询与连接查询
    MySQL数据库8(十四)聚合函数和运算符
    MySQL数据库8(十三)高级数据操作之select指令
    MySQL数据库8(十二)高级数据操作
  • 原文地址:https://www.cnblogs.com/wangyuyang1016/p/10851355.html
Copyright © 2011-2022 走看看