今天做了一套全英的笔试题,这道题印象比较深刻,题目如下:
1. public class X {
2. public static void main (String[]args) {
3. String s1 = new String (“true”);
4. Boolean b1 = new Boolean (true);
5. if (s2.equals(b1)) {
6. System.out.printIn(“Equal”);
7. }
8. }
9. }
What is the result?
A. The program runs and prints nothing.
B. The program runs and prints “Equal”
C. An error at line 5 causes compilation to fail.
D. The program runs but aborts with an exception.
网上搜这道题时,发现是SCJP考试题。
原来是这样的,惭愧惭愧, SCJP (sun certificated java programmer),Java程序员认证,一种Java认证考试 。
言归正传,上面题目的正确选项为A,我当时好像选的D。
下面看一下String类里的equals方法就OK了。
equalspublic boolean equals(Object anObject) |
equals()方法里是一个对象就可以了,我以为是只能和String比较呢,学的不扎实呀!