• 【Java】Java代码经典错误清单


    一、String 对照 == 和 equals。详细描写叙述例如以下

     "=="操作符的作用
    1)用于基本数据类型的比較,例如以下:

    byte(字节) 8 -128 - 127 0
    shot(短整型) 16 -32768 - 32768 0
    int(整型) 32 -2147483648-2147483648 0
    long(长整型) 64 -9233372036854477808-9233372036854477808 0 
    float(浮点型) 32 -3.40292347E+38-3.40292347E+38 0.0f
    double(双精度)	64 -1.79769313486231570E+308-1.79769313486231570E+308 0.0d
    char(字符型) 16 ‘ u0000 - uffff ’ ‘u0000 ’
    boolean(布尔型) 1 true/false false
    2)推断引用是否指向堆内存的同一块地址。
    equals所在位置:
    在Object类其中,而Object是全部类的父类,包括在jdk里面。但并不适合绝大多数场景。通常须要重写

    public boolean equals(Object obj) {
            return (this == obj);
        }
    equals的作用:

    用于推断两个变量是否是对同一个对象的引用,即堆中的内容是否同样。返回值为布尔类型


    二、Long 对照  == 和 equals。详细描写叙述例如以下

    Long相对来讲是一个比較特殊的,先说以下的样例:

    样例1:

    /**
         * <一句话功能简述> <功能具体描写叙述>
         * @author xutianlong
         * @param args
         * @see [类、类#方法、类#成员]
         */
        public static void main(String[] args)
        {
            Long long1 = 128L;
            Long long2 = 128L;
            if (long1 == long2)
            {
                System.out.println("true");
            }
            else
            {
                System.out.println("false");
            }
    
            if (long1.equals(long2))
            {
                System.out.println("true");
            }
            else
            {
                System.out.println("false");
            }
    
        }

    输出:false 和true


    样例2:

     /**
         * <一句话功能简述> <功能具体描写叙述>
         * @author xutianlong
         * @param args
         * @see [类、类#方法、类#成员]
         */
        public static void main(String[] args)
        {
            Long long1 = 1L;
            Long long2 = 1L;
            if (long1 == long2)
            {
                System.out.println("true");
            }
            else
            {
                System.out.println("false");
            }
    
            if (long1.equals(long2))
            {
                System.out.println("true");
            }
            else
            {
                System.out.println("false");
            }
    
        }

    输出:true和true


    可能非常多人认为这是一件非常奇妙的事,事实上不然。这个须要读JDK的源代码才干知道原因,找到源代码

    Long的里面有一个私有的静态内部类,实现例如以下:

      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);
            }
        }
    再看他怎么使用的:

     /**
         * Returns a {@code Long} instance representing the specified
         * {@code long} value.
         * If a new {@code Long} instance is not required, this method
         * should generally be used in preference to the constructor
         * {@link #Long(long)}, as this method is likely to yield
         * significantly better space and time performance by caching
         * frequently requested values.
         *
         * Note that unlike the {@linkplain Integer#valueOf(int)
         * corresponding method} in the {@code Integer} class, this method
         * is <em>not</em> required to cache values within a particular
         * range.
         *
         * @param  l a long value.
         * @return a {@code Long} instance representing {@code l}.
         * @since  1.5
         */
        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);
        }

    显而易见。-128到127直接的值都放在cache里,不会创建新的对象,所以==比較的时候。结果是正确的。
    当超过这个范围,会创建的新对象。所以不会相等

    三、Integer类型也是同理与Long类型,能够查看到Integer实现的时候也是存在IntegerCache,例如以下:

    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) -1);
                }
                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() {}
        }

     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);
        }


  • 相关阅读:
    看动画轻松理解「递归」与「动态规划」
    21天,在Github上获取 6300 star
    啥是佩奇排名算法
    什么是平衡二叉树(AVL)
    看完动画你还会不懂 快速排序么
    冰与火之歌:「时间」与「空间」复杂度
    21天,在Github上获取 6300 star
    看完动画你还会不懂 快速排序么
    五分钟学会一个有意思的排序:计数排序
    十大经典排序算法动画,看我就够了!
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7131847.html
Copyright © 2020-2023  润新知