"==" and equals method is what is the difference?

== operator designed to compare two values ​​are equal, i.e. for comparing the variables corresponding to the values ​​stored in the memory are the same, to compare two basic types of data or two reference variables are equal, == operator can only be used. If the variable points to a data object type, then this time involves two memory occupied by the object itself a memory (heap memory), a memory occupancy variables, e.g. Objet obj = new Object (); obj is a variable memory, new Object () is a second memory, at this time, the value stored in memory corresponding to the variable obj is the object occupies the first address of that memory. For variable type pointing object, if you want to compare two variables refer to the same object, i.e., the value depends on two variables corresponding to the memory are equal, this time need to be compared with == operator. equals method is used to compare the contents of two separate objects are the same, like is the same ratio to compare the appearance of two people, it compares two objects are independent. For example, the following code:
String a=new String("foo");

String b=new String("foo");
Two statements create two new objects, then a, b are two variables point to one object, which is two different objects, they are different from the first address, i.e., values ​​of a and b stored is not the same, therefore, the expression a == b will return false, and the content of these two objects are the same, therefore, the expression a.equals (b) returns true. In the actual development, we often want to compare the string to be passed such as whether the content, for example, String input = ...; input.equals ( "quit"), a lot of people a little attention on the use == to compare, this is wrong, just to find a few items of practical instructional videos from the internet to see, which have a large number of such errors. Remember, basically string comparison using the equals method. String class equals method is already off the override. If a class does not own definition of the equals method, then the equals method it inherits the Object class, equals method of the Object class codes are as follows:
boolean equals(Object o){
return this==o;
}
This means that if a class does not own definition of the equals method, which by default equals method (inherited from the Object class) is to use the == operator, also comparing two variables point to whether the object is the same object, and this time the use of equals use == you will get the same results, if the comparison of two separate objects are always returns false. If you want to write the kind of content can compare two instances of objects created from that class is the same, then you must override the equals method to you to decide what to write code that can be considered in the content of the two objects are the same. Supplementary: hashCode method
一般来讲,equals 这个方法是给用户调用的,如果你想判断 2 个对象是否相等,你可以重写 equals 方法,然后在代码中调用,就可以判断他们是否相等了。简单来讲,equals 方法主要是用来判断从表面上看或者从内容上看,2 个对象是不是相等。举个例子,有个学生类,属性只有姓名和性别,那么我们可以认为只要姓名和性别相等,那么就说这 2 个对象是相等的。
hashcode 方法一般用户不会去调用,比如在 hashmap 中,由于 key 是不可以重复的,他在判断 key 是不是重复的时候就判断了 hashcode 这个方法,而且也用到了 equals 方法。这里不可以重复是说 equals 和 hashcode 只要有一个不等就可以了!所以简单来讲,hashcode 相当于是一个对象的编码,就好像文件中的 md5,他和 equals 不同就在于他返回的是 int 型的,比较起来不直观。

我们一般在覆盖 equals 的同时也要覆盖 hashcode,让他们的逻辑一致。举个例子,还是刚刚的例子,如果姓名和性别相等就算 2 个对象相等的话,那么 hashcode 的方法也要返回姓名的 hashcode 值加上性别的 hashcode 值,这样从逻辑上,
他们就一致了。

要从物理上判断 2 个对象是否相等,用==就可以了。)
Reference: [1] online download java android face questions (address forgot)

Reproduced in: https: //my.oschina.net/itfanr/blog/195617

Guess you like

Origin blog.csdn.net/weixin_33858485/article/details/91799399