• Java 面试题分析


    分析代码如下:

    demo1:

     public static void main(String[] args)
        {
            String str1 = "abc";//常量池
            String str2 = "abc";//常量池
            String str3 = new String("abc");//堆 中
            System.out.println(str1 == str2);//同一个直 true
            System.out.println(str1 == str3);//不同 false
            System.out.println(str1.equals(str3));//Hashcode 相同
            System.out.println(str2.equals(str3));//Hashcode 相同
            
            System.out.println();
            Integer i =127,j=127,m=129,n=129;
            System.out.println(i==j);//
            System.out.println(m==n);
        }
    

      结果:

    true
    false
    true
    true
    
    true
    false
    

    Integer 原码如下:

    assert IntegerCache.high >= 127;
        /**
         * Returns an {@code Integer} instance representing the specified
         * {@code int} value.  If a new {@code Integer} instance is not
         * required, this method should generally be used in preference to
         * the constructor {@link #Integer(int)}, as this method is likely
         * to yield significantly better space and time performance by
         * caching frequently requested values.
         *
         * This method will always cache values in the range -128 to 127,
         * inclusive, and may cache other values outside of this range.
         *
         * @param  i an {@code int} value.
         * @return an {@code Integer} instance representing {@code i}.
         * @since  1.5
         */
        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);
        }
    

      

    demo2;

    public class NULL
    {
        public static void test()
        {
            System.out.println("test");
        }
    
        public static void main(String[] args)
        {
            ((NULL) null).test();//同 NULL.test(); 
            
            NULL n = new NULL();
            n.test(); //NULL.test();
            
            Object obj = 0.6324;
            System.out.println(obj.getClass());//Double
    
            final StringBuffer a = new StringBuffer("aaa");
            a.append("a");//special
            System.out.println(a);
        }
    }
    

      

  • 相关阅读:
    在独立的文件里定义WPF资源
    Irrlicht 3D Engine 笔记系列 之 教程6- 2D Graphics
    Java实现二叉树的创建、递归/非递归遍历
    NDK在windows下的开发环境搭建及开发过程
    硬件路由转发原理浅析
    ubuntu下vim中内容拷贝到浏览器
    python调用Java代码,完毕JBPM工作流application
    C++组合通信
    linux杂谈(十八):DNSserver的配置(一)
    Codeforces 550D. Regular Bridge 构造
  • 原文地址:https://www.cnblogs.com/xue88ming/p/7286148.html
Copyright © 2020-2023  润新知