关于面试的一些小记

来源:互联网 发布:java 编码url 空格 20 编辑:程序博客网 时间:2024/04/30 00:49

感谢生命中的这些贵人~在很牛的时候不抛弃我们~在关键的时候点醒我们~


1  下面输出结果是?

public static void main(String[] args) {Integer a = 123;Integer b = new Integer(123);System.out.println("result a==b is: " + (a == b));Integer a2 = 100;Integer b2 = 100;System.out.println("result a2==b2 is: " + (a2 == b2));Integer a3 = 128;Integer b3 = 128;System.out.println("result a3==b3 is: " + (a3 == b3));}

答案:false  true false 

简解:

JVM中一个字节以下的整型数据会在JVM启动的时候加载进内存,除非用new Integer()显式的创建对象,否则都是同一个对象;Integer中valueOf源码

 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);    }
可以看出只要超过-127~128这个范围,就会new Integer创建新的对象,==比较就会返回false

2  HashMap怎么判断是否存在某个key?为什么使用?有没有别的办法?containsKey方法原理?能用get方法判断吗?

简解:

使用containsKey方法判断;containsKey方法源码简单介绍

    /**     * Returns <tt>true</tt> if this map contains a mapping for the     * specified key.     *     * @param   key   The key whose presence in this map is to be tested     * @return <tt>true</tt> if this map contains a mapping for the specified     * key.     */    public boolean containsKey(Object key) {        return getEntry(key) != null;    }
 /**     * Returns the entry associated with the specified key in the     * HashMap.  Returns null if the HashMap contains no mapping     * for the key.     */    final Entry<K,V> getEntry(Object key) {        if (size == 0) {            return null;        }        int hash = (key == null) ? 0 : hash(key);        for (Entry<K,V> e = table[indexFor(hash, table.length)];             e != null;             e = e.next) {            Object k;            if (e.hash == hash &&                ((k = e.key) == key || (key != null && key.equals(k))))                return e;        }        return null;    }
注意上面的判断 e.hash == hash &&  ((k = e.key) == key || (key != null && key.equals(k))) 需要hash值相同,equals相等才认为是同一个key;

不能用get方法,原因是hashmap是可以保存<null,null>、<null,value>或者<key,null>key和value都可以为null,如果value为null的时候是没有办法判断key不存在还是存在值为null

3运行时结果

public static void main(String[] args) {Integer a = null;Integer b = (Integer) a; //1Integer c = (int)a;      //2System.out.println("result a == 3 is: " + (a == 3)); //3 System.out.println("result a == new Integer(3) is: " + (a == new Integer(3))); //4}
答案:1 正常 2运行时报空指针异常 3 运行时报空指针异常 4 false
简解:对于第二行写法,本人没有这么写过,待补充~


4 下面运行结果

public static void main(String[] args) {String a = "123";String b = "123";System.out.println("1.result for a == b is: "+(a == b));String a2 = "123"; String b2 = "1"+"23";System.out.println("2.result for a2 == b2 is: "+(a2 == b2));String a3 = "123"; String b3 = new String("123");System.out.println("3.result for a3 == b3 is: "+(a3 == b3));String a4 = "123"; String b4 = "1" + new String("23");System.out.println("4.result for a4 == b4 is: "+(a4 == b4));String a5 = new String("123");String b5 = new String("123");System.out.println("5.result for a5 == b5 is: "+(a5 == b5));}
答案:

1.result for a == b is: true
2.result for a2 == b2 is: true
3.result for a3 == b3 is: false
4.result for a4 == b4 is: false
5.result for a5 == b5 is: false

分析:参见这篇文章吧  http://blog.csdn.net/xbkaishui/article/details/6284702 有启发的第四题 +连接的对象的地址是无法确定 的, 第二个中“1”和“23”在编译期能确定,和“123”是等效的









0 0
原创粉丝点击