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

     

    什么是包装类

    让基本数据类型也有对象的功能可以有方法,属性,可交互对象;

    包装类和常用基本数据类型的对应关系

    装箱:把基本数据类型转换为包装类;

    1自动装箱

    2手动装箱

    拆箱:把包装类转换为数据类型;

    1手动拆箱

    2自动拆箱

    package com.jiedada.wrapp;
    
    public class OneWrapp {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
             //自动装箱
            int n1=2;
            Integer n2=n1;
            //手动装包
            Integer n3=new Integer(n1);
            System.out.println(n1);
            System.out.println(n2);
            System.out.println(n3);
            System.out.println("**********************");
            //手动拆箱
            int n5=n2.intValue();
            
            //自动装箱
            int n6=n2;
            System.out.println(n5);
            System.out.println(n6);
        }
    
    }
    View Code

    基本数据类型和字符串类型转换

    package com.jiedada.wrapp;
    
    public class WrappTwo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            //int转换为string
            int i=2;
            String str=Integer.toString(i);
            System.out.println(str);
            //string转换为int
            //第一种
            int i1=Integer.parseInt(str);
            //第二种
            int i2=Integer.valueOf(str);
            System.out.println(i1);
            System.out.println(i2);
        }
    
    }
    View Code

    包装类的常用方法

    在orci中有所有的包类的方法,我们可以通过自己不断地学习了解这些方法在这里

    数字类型是final继承number,在Interger中有byteValue(将Int转换为其他的)登各种转换;

    需要知道的几个知识点

    当基本属性不赋值时都会有默认值

    那在包装类中的返回值为null因为他们是一个类;

     当Integer one=100时会产生下面的问题电脑会自动开辟一个缓存区,但是只能存放-128--127的数;

    当==两边为对象时判断的为对象地址是不是相等;

     

     在其中有2种类型不能实现以上对象池的类型

    而字符类型也是final但是继承Object;

  • 相关阅读:
    生成函数
    FFT【快速傅里叶变换】FWT【快速沃尔什变换】
    牛客网多校赛第9场 E-Music Game【概率期望】【逆元】
    hdu6393Traffic Network in Numazu【树状数组】【LCA】
    hdu 6395Sequence【矩阵快速幂】【分块】
    hdu6390GuGuFishtion【数论】
    欧拉函数和莫比乌斯函数
    hdu4614 Vases and Flowers【线段树】【二分】
    hdu4578 Transformation【线段树】
    hdu3974 Assign the task【线段树】
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/10719747.html
Copyright © 2011-2022 走看看