J2se eQuals usage

Source: Internet
Author: User

Equals is a method in the Object class. Determine whether the two objects are the same. Defined as x. queals (y). If x and y are the same object, true is returned.

Otherwise, false is returned.

J2set provides classes that overwrite the queal usage of objects and determine whether the referenced object is of the same class and returns true if the attribute content is the same.

Let's look at a small test.

public class equal{public static void main(String[] args){cat c1=new cat();cat c3=new cat();System.out.println(c1==c3);}} class cat{int color;int height,weight; }


The output is


Is it because the weight and the color height are different? So let's write a constructor to make them all the same and check the effect.

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> public class equal {public static void main (String [] args) {cat c1 = new cat (1, 2, 3 ); cat c3 = new cat (1, 2, 3); System. out. println (c1.equals (c3) ;}} class cat {intcolor; intheight, weight; public cat (int color, int height, int weight) {this. color = color; this. height = height; this. weight = weight ;}}

Let's take a look at the compilation result/or false.


Why is this always the case. This is the default comparison of equals. It compares whether the current object and the comparison object are the same (x = y) and

Is the same.

Let's explain it. They compare the addresses and view the memory distribution chart. The two new object references are always different.


The reference content is compared here, so it is different.

So what are we comparing, and compare whether the content of the two objects is the same.

How to compare: 

Object cannot be blank

Symmetric

.


Implementation of the provided method. We need to override the parent class method and remove the default comparison. Implement it by yourself

Public boolean equals (Object obj ){

}


See Implementation

Public class equal {public static void main (String [] args) {catc1 = new cat (1, 2, 4); catc3 = new cat (1, 2, 4); System. out. println (c1.equals (c3) ;}} class cat {intcolor; intheight, weight; public cat (int color, int height, int weight) {this. color = color; this. height = height; this. weight = weight;} public boolean equals (Object obj) {if (obj = null) return false; else {// determine which type of if (objinstanceof cat) {// forcibly convert cat c = (cat) obj; if (c. color = this. color & c. height = this. height & c. weight = this. weight) {return true ;}} returnfalse ;}}

Test Results


Through three tests, we found that what we see is not what we see. The equals of an object indicates whether the content of the object is the same. We need to override the comparison method of the equals object to better compare whether the object is consistent.

Related Article

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.