国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java辨析(1):==和Equal.doc

2019-11-18 11:22:03
字體:
來源:轉載
供稿:網友

? 總結
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)//小心大小寫



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鹰潭市| 海兴县| 高平市| 烟台市| 昌图县| 广水市| 仪陇县| 井陉县| 通州市| 郁南县| 南陵县| 南漳县| 咸丰县| 四子王旗| 壤塘县| 牙克石市| 屏南县| 乌鲁木齐县| 镇康县| 合作市| 内黄县| 诸城市| 宾川县| 江北区| 科尔| 湖南省| 随州市| 海门市| 望城县| 缙云县| 隆尧县| 东源县| 莱西市| 谢通门县| 道真| 奉节县| 伊吾县| 哈尔滨市| 江北区| 清新县| 绵竹市|