Object类和String类equals方法的区别

String类的equals方法和“==”


相信很多学习过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= str2.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”);
    String str6=str5;
    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。

equals()方法在String类中的定义


string 类的成员变量

<span style="font-size:14px;">public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence
{
    /** The value is used for character storage. */
    private final char value[];

    /** The offset is the first index of the storage that is used. */
    private final int offset;

    /** The count is the number of characters in the String. */
    private final int count;

    /** Cache the hash code for the string */
    private int hash; // Default to 0

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -6849794470754667710L;
</span>


equals方法源码

public boolean equals(Object anObject) {
	if (this == anObject) {
	    return true;
	}
	if (anObject instanceof String) {
	    String anotherString = (String)anObject;
	    int n = count;
	    if (n == anotherString.count) {
		char v1[] = value;
		char v2[] = anotherString.value;
		int i = offset;
		int j = anotherString.offset;
		while (n-- != 0) {
		    if (v1[i++] != v2[j++])
			return false;
		}
		return true;
	    }
	}
	return false;
    }
在String类的equals方法中,先使用“==”比较两个对象是否是同一个对象的引用,如果是,返回true;如果不是,怎么继续比较两个String对象的大小以及内容。

equals()方法在Object类中的定义


equals方法源码

<span style="font-size:24px;">  </span><span style="font-size:14px;">public boolean equals(Object obj) {
	return (this == obj);
    }</span>

"=="用于比较引用和比较基本数据类型时具有不同的功能:

            比较基本数据类型,如果两个值相同,则结果为true ,而在比较引用时,如果引用指向内存中的同一对象,结果为true.

"=="在这里是在比较两个对象是否指向内存中的同一对象。
在Object类的equals方法的本质其实是和“==”一样的,都是比较两个对象引用是否指向同一个对象(即两个对象是否为同一对象)

  我们通常自定义的对象,通常需要重写Equals方法。

equals()方法

<span style="font-size:14px;">class Person
{
 private String name;
 private int age;
 
 public Person(String name, int age)
 {
  this.name = name ;
  this.age = age ;
 }
 public void show()
 {
  System.out.println(name + ", " + age);
 }
 
 public boolean equals (Object obj)
 {
  if(!(obj instanceof Person))   //如果不是Person类型,直接返回false
     return false;
  //this是p1 ,obj是p2
  Person p = (Person)obj; s //由于访问person特有的成员,需要把obj装换成person类.
  //if(this.name != p.name)
  
  if(this.name == null)
  {
     if (p.name != null)       //比较名字
        return false;
   }
  else if(!this.name.equals(p.name))
     return false;
     
  if (this.age != p.age)
        return false;
     return true;
  //return (this = obj);
 }
}</span>



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值