-
下列代码在1.5以后版本的JVM中的执行结果是?
public class Example { public static void main(String[] args) { Integer i1 = -128; Integer i2 = -128; Integer i5 = -129; Integer i6 = -129; System.out.println(i1 == i2); System.out.println(i1.equals(i2)); System.out.println(i5 == i6); System.out.println(i5.equals(i6)); } }
A. 代码不能编译通过,因为赋值运算左右数据类型不一致
B. false true false true
C. true true false true
D. true true true true
可以先看看这篇文章《包装类型实例优先使用整数池的解析》
-
下列代码的执行结果是( )
public class Example { public static void main(String[] args) { List list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); List<Integer> intList = list; for (int i = 0; i < list.size(); i++) { System.out.println(intList.get(i)); } } }
A. 第7行编译错误,因为列表的泛型类型不同
B. 编译成功,运行时在第9行出现类型转换错误
C. 输出a b c
D. 编译成功,无任何输出
-
下列代码的执行结果是?()
public class Example { public static void main(String[] args) { Object str = "test"; Math math = (Math) str; System.out.println(math instanceof String); } }
A. 程序编译成功,运行时在第4行出现对象造型异常
B. 程序编译失败
C. true
D. false
-
以下代码的执行结果是?
public class Example { public static void main(String[] args) { TreeSet<String> t = new TreeSet<String>(); if (t.add("one")) if (t.add("two")) if (t.add("three")) t.add("four"); for (String s : t) { System.out.print(s); } } }
A. one
B. onethreetwo
C. onetwothreefour
D. fouronethreetwo
-
下列代码执行的结果是( )
public class Example { public static void stringReplace (String text) { text = text.replace (‘j’ , ‘i’); } public static void bufferReplace (StringBuffer text) { text.append (“C”); text=new StringBuffer(“Hello”); text.append(“World!”); } public static void main (String args[]) { String textString = new String (“java”); StringBuffer textBuffer = new StringBuffer (“java”); stringReplace (textString); bufferReplace (textBuffer); System.out.println (textString + textBuffer); } }
A. iavaHelloWorld
B. javajavaC
C. javaHelloWorld
D. iavajavaC
参考答案:CCBDB
第一题优先使用整数池没问题,
第二题Java所谓的泛型,其实就是一颗语法糖。C#的泛型是基于类型膨胀,那是真正的实现,但Java的泛型只存在于源码中,在编译后就已经不存在了,变为了原生类型,也叫裸类型,这也就是著名的类型擦除了!所以,对运行期的Java来说,ArrayList String和 ArrayList int没有区别。泛型,只是编译器的一个小把戏而已啦!这种实现,也叫伪泛型!
第三题的话instanceof运算符的应用,
第四题字符串的排序是按照Unicode排序,
第五题StringBuffer和String的区别。