服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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

? 总结
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)//小心大小写

扫描关注微信公众号