zoukankan      html  css  js  c++  java
  • Integer和new Integer

    Java code
     
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static void main(String[] args) {
        Integer i1=10;
        Integer i2=10;
        Integer i3=new Integer(10);
        Integer i4=128;
        Integer i5=128;
        System.out.println(i1==i2);//true
        System.out.println(i1==i3);//false
        System.out.println(i4==i5);//false
    }



    Integer x = 10;//自动装箱,如果在-128到127之间,则值存在常量池中
    Integer y = new Integer(10);//普通的堆中的对象

    Java自动装箱和拆箱定义

           Java 1.5中引入了自动装箱和拆箱机制:

           (1)自动装箱:把基本类型用它们对应的引用类型包装起来,使它们具有对象的特质,可以调用toString()、hashCode()、getClass()、equals()等方法。

            如下:

            Integer a=3;//这是自动装箱

            其实编译器调用的是static Integer valueOf(int i)这个方法,valueOf(int i)返回一个表示指定int值的Integer对象,那么就变成这样: 

            Integer a=3;   =>    Integer a=Integer.valueOf(3);

            (2)拆箱:跟自动装箱的方向相反,将Integer及Double这样的引用类型的对象重新简化为基本类型的数据。

             如下:

             int i = new Integer(2);//这是拆箱

             编译器内部会调用int intValue()返回该Integer对象的int值

             注意:自动装箱和拆箱是由编译器来完成的,编译器会在编译期根据语法决定是否进行装箱和拆箱动作。

  • 相关阅读:
    DAY1 linux 50条命令
    安卓2.0,3.0,4.0的差别
    java历史
    晶体管共射极单管放大电路
    jquery取消选择select下拉框
    oarcle数据库导入导出,创建表空间
    360chrome,google chrome浏览器使用jquery.ajax加载本地html文件
    jquery 选择器
    nodejs 相关
    关于http请求
  • 原文地址:https://www.cnblogs.com/Yxxxxx/p/6864373.html
Copyright © 2011-2022 走看看