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

    所为的包装类值得是将基本数据类型封装在一个类

    这个时候的MyInt实际上是Int类型的包装类,利用MyInt可以实现基本数据类型转换为对象

    范例:

    结论:将基本数据类型包装为一个类对象的本质在于方便进行Object进行接收处理。那么java中有8个基本数据类型。如果每种类型都按照。都按照以上形式编写,那么就会存在以下问题,

        发现所有开发中代码会重复

        在进行数学计算的时候,必须利用明确的方法将包装类中包装的基本数据取出才可以进行。

      所以java为了方便用户的开发,专门提供了一个包装类的使用,

        对象型(Object直接子类):Boolean Character(char);

        数值型(Number直接子类):Byte Double Short Long Integer(int) Float;

    说明:关于Number类

       Number类的定义:public abstract class Number extends Object implements Serializable 

       在Number类里面实际上定义有六个重要的方法:intValue() shortValue() byteValue() longValue doubleValue floatValue()

    装箱和拆箱操作

    在包装类和基本类型的处理中,存在两个概念:

      装箱:将基本数据类型为包装类对象!

        利用每一个包装类提供的构造方法实现装箱处理

      拆箱:将包装类中包装的基本数据类型取出,

        利用Number类中提供的一系列xxValue()方法,

    范例:

    public class Newbegin{
        public static void main(String args    []) {
            Integer x=10;// 自动装箱
            //这个时候发现可以直接利用包装类对象操作
            System.out.println(++x*2);
        }
    }

    自动装箱,但是这个时候依然会存在有==和equals()问题

    范例:观察问题

    1 public class Newbegin{
    2     public static void main(String args    []) {
    3         Integer num1=10;//自动装箱
    4         Integer num2=10;//自动装箱
    5         System.out.println(num1==num2);
    6         System.out.println(num1==new Integer(10));
    7         System.out.println(num1.equals(new Integer(10)));
    8     }
    9 }

    选择是用int还是Integer

      在接受数据的时候,使用的一定都是int,而保存数据的时候会使用Integer()  int可以设置为0 Integer可以设置为null

     以后编写的简单java类一定不要再去使用基本数据类型,全部换位包装类

    字符串与基本数据类型的转换

     以后如果要进行各种数据的输入一定都是字符串类型接受。所以在开发之中就存在有一种需求,如何将字符串变为基本数据类型,这个时候就需要包装类支持了:

        String变为int类型(Integer类):

        String变为double类型(Double类):

        String变为int类型(Boolean类):

    范例:将字符串转换为int 

    1 public class Newbegin{
    2     public static void main(String args    []) {
    3         String str="123"; //字符串
    4         int num=Integer.parseInt(str);//字符串变为基本类型
    5         System.out.println(num*2);
    6     }
    7 }

    范例:将字符串转换为double

    1 public class Newbegin{
    2     public static void main(String args    []) {
    3         String str="123"; //字符串
    4         double num=Double.parseDouble(str);
    5         System.out.println(num*2);
    6     }
    7 }

      但是特别要引起注意的是,如果在将字符串变为数字的时候,字符串的组成由非数字,那么转换就将出现错误:

    (NumberFormatException)以后就因为这个异常,我们要写一堆的来回避

      而特别需要说明的是,字符串与Boolean转换不受局限。

    将基本数据类型转换为字符串有两种方式:

      任何数据类型使用了+连接空白字符串就变为了字符串型:

    1 public class Newbegin{
    2     public static void main(String args    []) {
    3         String str=100+""; 
    4         System.out.println(str);
    5     }
    6 }

      使用String类中提供的ValueOf()方法,这个方法可以进行转换,

    1 public class Newbegin{
    2     public static void main(String args    []) {
    3         String str=String.valueOf(100); 
    4         System.out.println(str);
    5     }
    6 }

    这个操作作为基本概念

  • 相关阅读:
    SCRIPT TO GENERATE SQL*LOADER CONTROL FILE
    Setting an Oracle event:The structure of the trace syntax
    Script to Collect RAC Diagnostic Information (racdiag.sql)
    Data Block Cache Header Format Changes (Oracle8 Physical layout)
    Function Based Indexes and Global Temporary Tables
    EVENT: 10231 "skip corrupted blocks on _table_scans_"
    Materialized Views and Dimensions
    Script: Computing Table Size
    DBMS_REPAIR example
    Sql2005+:ssms 的【临时】服务器连接配置文件:
  • 原文地址:https://www.cnblogs.com/Tony98/p/10424696.html
Copyright © 2011-2022 走看看