我是靠谱客的博主 俊逸紫菜,最近开发中收集的这篇文章主要介绍封装类之为什么这些数据的地址相等,而不在这个范围的数据就不相等了呢?前言 Integer封装类Short类Long类Float类Double类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

前言

在Java中,“==”有一个特性,

举个例子:

如果==比较的是基本数据类型,那么比较的是两个基本数据类型的值是否相等;

如果==是比较的两个对象,那么比较的是两个对象的引用,也就是两个对象是否为同一个对象,并不是比较的对象的内容

 那什么时候是对象,在Java数中是这样说的,

要创建新的对象,需要使用new这个关键字和想要它创建对象的类名(比较的一方使用了new,就证明是比较的是对象,我是这样认为的,有错希望更正一下)

 Integer封装类

源码第一个介绍

The {@code Integer} class wraps a value of the primitive type
 * {@code int} in an object. An object of type {@code Integer}
 * contains a single field whose type is {@code int}.
 *
 * <p>In addition, this class provides several methods for converting
 * an {@code int} to a {@code String} and a {@code String} to an
 * {@code int}, as well as other constants and methods useful when
 * dealing with an {@code int}.
 *
 * <p>Implementation note: The implementations of the "bit twiddling"
 * methods (such as {@link #highestOneBit(int) highestOneBit} and
 * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
 * based on material from Henry S. Warren, Jr.'s <i>Hacker's
 * Delight</i>, (Addison Wesley, 2002).
 *
 * @author  Lee Boynton
 * @author  Arthur van Hoff
 * @author  Josh Bloch
 * @author  Joseph D. Darcy
 * @since JDK1.0

代码(为什么当Integer初始化的值超过(-128,127)这个范围就会不在常量池了)

代码

public static void main(String[] args) {
        Integer i1 =127;
        Integer i2 = 128;
        Integer i3 = 127;
        Integer i4 = 128;
        System.out.println(i1 == i3);
        System.out.println(i2 == i4 );
    }

结果:

true

false

分析源码:

public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low));
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }

结论:

当Integer初始化的值超过了(-128,127)这个范围会导致它会new一下,此时地址就不在常量池了。

Short类

源码第一个介绍:

 * The {@code Short} class wraps a value of primitive type {@code
 * short} in an object.  An object of type {@code Short} contains a
 * single field whose type is {@code short}.
 *
 * <p>In addition, this class provides several methods for converting
 * a {@code short} to a {@code String} and a {@code String} to a
 * {@code short}, as well as other constants and methods useful when
 * dealing with a {@code short}.
 *
 * @author  Nakul Saraiya
 * @author  Joseph D. Darcy
 * @see     java.lang.Number
 * @since   JDK1.1

代码(为什么当数据超过(-128,127)这个范围就不在常量池了)

代码:

 public static void main(String[] args) {
        Short  i1 = 129;
        Short  i2 = 12;
        Short  i3 = 129;
        Short  i4 = 12;
        System.out.println(i1 == i3);
        System.out.println(i2 == i4 );
    }

结果:

false

true

分析源码:

public static Short valueOf(short s) {
        final int offset = 128;
        int sAsInt = s;
        if (sAsInt >= -128 && sAsInt <= 127) { // must cache
            return ShortCache.cache[sAsInt + offset];
        }
        return new Short(s);
    }
private static class ShortCache {
        private ShortCache(){}

        static final Short cache[] = new Short[-(-128) + 127 + 1];

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Short((short)(i - 128));
        }
    }

结论:

在ValueOf这个Short的静态函数中说明了在(-128,127)之外会new一下,从而导致了地址变化

Long类

源码第一段介绍

* The {@code Long} class wraps a value of the primitive type {@code
 * long} in an object. An object of type {@code Long} contains a
 * single field whose type is {@code long}.
 *
 * <p> In addition, this class provides several methods for converting
 * a {@code long} to a {@code String} and a {@code String} to a {@code
 * long}, as well as other constants and methods useful when dealing
 * with a {@code long}.
 *
 * <p>Implementation note: The implementations of the "bit twiddling"
 * methods (such as {@link #highestOneBit(long) highestOneBit} and
 * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
 * based on material from Henry S. Warren, Jr.'s <i>Hacker's
 * Delight</i>, (Addison Wesley, 2002).

为什么当long型的值超过了(-128,127)这个范围的时候会出现值不相等的情况

代码

  public static void main(String[] args) {
        long a =127;
        long b =128;
        long c =127;
        long d = 128;
        Long i1 = a;
        Long i2 = b;
        Long i3 = c;
        Long i4 = d;
        System.out.println(i1 == i3);
        System.out.println(i2 == i4 );
    }

结果:

true

false

源码分析:

 public static Long valueOf(long l) {
        final int offset = 128;
        if (l >= -128 && l <= 127) { // will cache
            return LongCache.cache[(int)l + offset];
        }
        return new Long(l);
    }
private static class LongCache {
        private LongCache(){}

        static final Long cache[] = new Long[-(-128) + 127 + 1];

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Long(i - 128);
        }
    }

结论:

在ValueOf这个Long的静态函数中说明了在(-128,127)范围之外会new一下,从而导致了地址变化

Float类

源码第一段:

* The {@code Float} class wraps a value of primitive type
 * {@code float} in an object. An object of type
 * {@code Float} contains a single field whose type is
 * {@code float}.
 *
 * <p>In addition, this class provides several methods for converting a
 * {@code float} to a {@code String} and a
 * {@code String} to a {@code float}, as well as other
 * constants and methods useful when dealing with a
 * {@code float}.
 *
 * @author  Lee Boynton
 * @author  Arthur van Hoff
 * @author  Joseph D. Darcy
 * @since JDK1.0

程序为什么无论使用Float a = 1.0f ,b =1.0f ,a == b 执行的结果是false

 程序

public static void main(String[] args) {
        float a = 1.0f;
        float b = 1.0f;
        System.out.println(a==b);
        Float i1 = 1.0f;
        Float i2 = 2.0f;
        Float i3 = 1.0f;
        Float i4 = 2.0f;
        System.out.println(i1 == i3);
        System.out.println(i2 == i4 );
    }

结果

true

false

false

分析源码

public static Float valueOf(float f) {
        return new Float(f);
    }

结论

因为传进去了一个float值都会使用new。

Double类

源码第一段:

* The {@code Double} class wraps a value of the primitive type
 * {@code double} in an object. An object of type
 * {@code Double} contains a single field whose type is
 * {@code double}.
 *
 * <p>In addition, this class provides several methods for converting a
 * {@code double} to a {@code String} and a
 * {@code String} to a {@code double}, as well as other
 * constants and methods useful when dealing with a
 * {@code double}.
 *

程序,为什么始终使用Double命令的相等的值,可是使用“==”始终都是false

代码:

public static void main(String[] args) {
        double  a = 1.0;
        double  b = 1.0;
        System.out.println(a==b);
        Double  i1 = 1.0;
        Double  i2 = 2.0;
        Double  i3 = 1.0;
        Double  i4 = 2.0;
        System.out.println(i1 == i3);
        System.out.println(i2 == i4 );
    }

结果:

true

false

false

分析源码:

public static Double valueOf(double d) {
        return new Double(d);
    }

结论

传进去一个double值都会被new修饰一边

最后

以上就是俊逸紫菜为你收集整理的封装类之为什么这些数据的地址相等,而不在这个范围的数据就不相等了呢?前言 Integer封装类Short类Long类Float类Double类的全部内容,希望文章能够帮你解决封装类之为什么这些数据的地址相等,而不在这个范围的数据就不相等了呢?前言 Integer封装类Short类Long类Float类Double类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部