本文共 4462 字,大约阅读时间需要 14 分钟。
相信很多学习过Java的同学都知道,在比较两个String对象的内容是否相同时是使用equals方法的
如:String str1=new String(“A”);
String str2=new String(“B”);
String str3=new String(“B”);
boolean result1= str1.equals(str2);
boolean result2= str1.equals(str3);
System.out.println(result1);
System.out.println(result2);
则输入的result1为false,result2为true。因为str1与str2的内容不相同,而str2与stR3内容相同都是“B”。
而在String类中使用“==”时,比较的是两个String对象的引用是否指向同一个对象,如
String str4=new String(“B”);
String str5=new String(“B”);
boolean result3=(str4==str5);
boolean result4= (str5==str6);
System.out.println(result3);
System.out.println(result4);
则输入的result3为false,result4为true。因为str4、str5虽然内容相同但它们是不同的对象,就像两个同样的杯子装着同样多的水,可它们是不同的,result3为false。而str5、str6是指向同一个String对象的,所以result4为true。
再说明一点,String str = new String("abc")和String str ="abc"是有一点小区别的,对于new出来的String对象,是每new一个内存里生成一个,也就是说其允许存在内容相同的重复对象。而String str ="abc"这种形式是不允许存在内容相同的重复对象,只要内存已经存在了,就不再新生成,而是把新的引用指向原来的对象。
以上是String类equals方法和"=="的区别和联系。下面再说说Object类的equals方法。
大家首先要明确一点Object类是所有Java类的父类,所有的Java类都要继承Object类,所以通常我们写程序的时候不显式声明继承Object类,而是默认继承Object类。
在Object类的equals方法的本质其实是和“==”一样的,都是比较两个对象引用是否指向同一个对象(即两个对象是否为同一对象)。那为什么String类的equals方法却又是比较两个String对象的内容是否相同呢?
原来是这样的,String类继承Object类后,也继承了equals方法,但String类对equals方法进行了重写,改变了equals方法的比较形式。其实很多其他类继承Object类后也对equals方法进行了重写。
参考二
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)//小心大小写 转载于:https://blog.51cto.com/4610653/893398