Java equals & ==

[size=medium][color=blue]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)//小心大小写
-------------------------------------------------------------------------
一道IBM笔试题(考==与EQUALS的区别)

题目:
Integer i = new Integer(42)
Long l = new Long(42)
Double d = new Double(42.0)
下面哪些返回true
1: i==l
2: i==d
3: l==d
4:i.equals(l)
5:i.equals(d)
6:l.equals(d)
7:i.equals(42)

答案:7;


编码验证:
public class Equivalence {
public static void main(String[] args) {
Integer i = new Integer(42)
Long l = new Long(42)
Double d = new Double(42.0)
System.out.println(i==l);
System.out.println(i==d);
System.out.println(l==d);
System.out.println(i.equals(l) );
System.out.println(i.equals(d) );
System.out.println(l.equals(d) );
System.out.println(i.equals(42));
}
}

运行结果:

1-3:编译不通过;
4-6:false
7:true

原理解释:
呵呵,我从thinking in java电子版中摘了一段:
1. 检查对象是否相等
关系运算符==和!=也适用于所有对象,但它们的含义通常会使初涉Java领域的人找不到北。下面是一个例子:


//: Equivalence.java

public class Equivalence {
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1 == n2);
System.out.println(n1 != n2);
}
} ///:~

其中,表达式System.out.println(n1 == n2)可打印出内部的布尔比较结果。一般人都会认为输出结果肯定先是true,再是false,因为两个

Integer对象都是相同的。但尽管对象的内容相同,句柄却是不同的,而==和!=比较的正好就是对象句柄。所以输出结果实际上先是false

,再是true。这自然会使第一次接触的人感到惊奇。
若想对比两个对象的实际内容是否相同,又该如何操作呢?此时,必须使用所有对象都适用的特殊方法equals()。但这个方法不适用于“

主类型”,那些类型直接使用==和!=即可。下面举例说明如何使用:


//: EqualsMethod.java

public class EqualsMethod {
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1.equals(n2));
}
} ///:~

正如我们预计的那样,此时得到的结果是true。但事情并未到此结束!假设您创建了自己的类,就象下面这样:


//: EqualsMethod2.java

class Value {
int i;
}

public class EqualsMethod2 {
public static void main(String[] args) {
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
}
} ///:~

此时的结果又变回了false!这是由于equals()的默认行为是比较句柄。所以除非在自己的新类中改变了equals(),否则不可能表现出我们希

望的行为。



[/color][/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值