关于面试的一些小记
来源:互联网 发布: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
- 关于面试的一些小记
- 关于jquery的一些小记
- 关于mipmap的一些小记
- 关于面试的一些问题
- 关于面试的一些认识
- 关于面试的一些总结
- 关于面试的一些事
- 关于面试的一些感想
- 关于面试的一些总结
- 关于面试的一些知识点
- 面试小记:关于数组和指针的一个笑话
- 关于安卓知识的一些小记录
- 关于数组的一些面试题目
- 关于嵌入式面试的一些题
- 关于面试学习的一些memory
- (二)关于Unity3d面试的一些说明
- 面试中关于字符串的一些问题
- 关于一些 iOS 面试问题的解答
- Android-Tab单选控件
- PHP生成静态页Smarty机制(简单、快速)
- alter table 常见语法
- Linq查询满足条件记录集
- 设置系统路径
- 关于面试的一些小记
- 密码学基础知识(七)公钥密码
- IOS学习之strong-weak,assign,nonatomic关键字整理
- 推荐一个手把手叫学greendao的好教程
- 技术分享-2015.7.8编
- Got fatal error 1236 from master when reading data from binary log
- 访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”
- 排序算法!~
- BaseExpandableListAdapter的用法