Java ... equals

Source: Internet
Author: User

Java ... equals
  1. Integer,string equals comparison is content
  2. object is compared to the memory address

  3. True if the parameter obj references an object that is the same object as the current object, and False instead

  4. There are classes in the JDK class that cover the Equals () method of the Oject class, and the comparison rules are:

    如果两个对象的类型一致,并且内容一致,则返回true,这些类有:java.io.file,java.util.Date,java.lang.string,包装类(Integer,Double等)
  5. When overriding the Equals method

    1、自反性   :对任意引用值X,x.equals(x)的返回值一定为true.2、对称性:   对于任何引用值x,y,当且仅当y.equals(x)返回值为true时,x.equals(y)的返回值一定为true;3、传递性:如果x.equals(y)=true, y.equals(z)=true,则x.equals(z)=true4、一致性:如果参与比较的对象没任何改变,则对象比较的结果也不应该有任何改变5、非空性:任何非空的引用值X,x.equals(null)的返回值一定为false
  6. Overriding the main points of the Equals method

    1、使用==操作符检查“实参是否为指向对象的一个引用”。 2、使用instanceof操作符检查“实参是否为正确的类型”。 3、把实参转换到正确的类型。 4、对于该类中每一个“关键”域,检查实参中的域与当前对象中对应的域值是否匹 配。对于既不是float也不是double类型的基本类型的域,可以使用==操作符 进行比较;对于对象引用类型的域,可以递归地调用所引用的对象的equals方法; 对于float类型的域,先使用Float.floatToIntBits转换成int类型的值, 然后使用==操作符比较int类型的值;对于double类型的域,先使用Double.doubleToLongBits转换成long类型的值,然后使用==操作符比较 long类型的值。 5、当你编写完成了equals方法之后,应该问自己三个问题:它是否是对称的、传 递的、一致的?(其他两个特性通常会自行满足)如果答案是否定的,那么请找到 这些

Java ... equals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.