總結
1、= =操作符比較的是操作符兩端的操作數是否是同一個對象;另外= =操作符兩邊的操作數必須是同一類型的(可以是父子類之間)才能編譯通過。
2、String的equals()方法比較的是兩個String對象的內容是否一樣
3、= =比較的是地址,如果是具體的阿拉伯數字的比較,值相等則為TRUE,如:
int a=10 與 long b=10L 與 double c=10.0都是相同的(為true),因為他們都指向地址為10的堆棧;如下題111;
? String s= "hello";
String t = "hello";
char c[] = {'h','e','l','l','o'}
Which return true?
A. s.equals(t);
B. t.equals(c);
C. s==t;
D. t.equals(new String("hello"));
E. t==c.
答案:(acd)
題目:哪些返回true。
這個在前面第10題的equals()方法和==操作符的討論中論述過。==操作符比較的是操作符兩端的操作數是否是同一個對象,而String的equals()方法比較的是兩個String對象的內容是否一樣,其參數是一個String對象時才有可能返回true,其它對象都返回假。需要指出的是由于s和t并非使用new創建的,他們指向內存池中的同一個字符串常量,因此其地址實際上是相同的(這個可以從反編譯一個簡單的測試程序的結果得到,限于篇幅不列出測試代碼和反編譯的分析),因此答案c也是正確的。
Given the following class:
public class Sample{
long length;
public Sample(long l){ length = l; }
public static void main(String arg[]){
Sample s1, s2, s3;
s1 = new Sample(21L);
s2 = new Sample(21L);
s3 = s2;
long m = 21L;
}
}
Which expression returns true?
A. s1 == s2;
B. s2 == s3;
C. m == s1;
D. s1.equals(m).
答案:(b)//D不對,只有String的equals()方法才比較值;
題目:給出下面的類: …
哪個表達式返回true。
前面已經敘述過==操作符和String的equals()方法的特點,另外==操作符兩邊的操作數必須是同一類型的(可以是父子類之間)才能編譯通過。
再看以下幾道
17. float f=4.2F;
Float g=new Float(4.2F);
Double d=new Double(4.2);
Which are true?
A. f= =g B. g= =g C. d= =f D. d.equals(f) E d.equals(g) F. g.equals(4.2);
答案:B
? 93. Click the exhibit button:
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.
答案:A
比較下題,小心使用equals 和 = =的區別;
? 93. Click the exhibit button:
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 = = 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.
答案:C
? 111. Given:
1. public class Foo {
2. private int val;
3. public foo(int v) (val = v;) }
4. public static void main (String [] args) {
5. Foo a = new Foo (10);
6. Foo b = new Foo (10);
7. Foo c = a;
8. int d = 10;
9. double e = 10.0;
10. }
11. }
Which three logical expressions evaluate to true? (Choose Three)
A.(a ==c)
B.(d ==e)
C.(b ==d)
D.(a ==b)
E.(b ==c)
F.(d ==10.0)
答案:ABF //= =比較的是地址,他們都指向地址為10的堆棧;
Given the following code, what test would you need to put in place of
the comment line?
//place test here to result in an output of the string Equal
public class EqTest{
public static void main(String argv[]){
EqTest e=new EqTest();
}
EqTest(){
String s="Java";
String s2="java";//小心大小寫
//place test here {
System.out.println("Equal");
}else
{
System.out.println("Not equal");
}
}
}
1) if(s==s2)
2) if(s.equals(s2)
3) if(s.equalsIgnoreCase(s2))
4)if(s.noCaseMatch(s2))
答案:3)//小心大小寫
posted on 2011-10-08 16:36
Java_liyadong 閱讀(6378)
評論(0) 編輯 收藏