equals

why equals() method when we have == operator? [duplicate]

一笑奈何 提交于 2019-11-26 01:54:18
问题 This question already has answers here : How do I compare strings in Java? (23 answers) Closed 6 years ago . When i see the implementation of equals() method it does nothing but same as what == does. So my question is what was the need to have this as separate method when we have == operator which does the same work? 回答1: You can not overload the == operator, but you can override equals(Object) if you want it to behave differently from the == operator, i.e. not compare references but actually

Any reason to prefer getClass() over instanceof when generating .equals()?

拥有回忆 提交于 2019-11-26 01:35:17
问题 I\'m using Eclipse to generate .equals() and .hashCode() , and there is an option labeled \"Use \'instanceof\' to compare types\". The default is for this option to be unchecked and use .getClass() to compare types. Is there any reason I should prefer .getClass() over instanceof ? Without using instanceof : if (obj == null) return false; if (getClass() != obj.getClass()) return false; Using instanceof : if (obj == null) return false; if (!(obj instanceof MyClass)) return false; I usually

Difference between null and empty (“”) Java String

匆匆过客 提交于 2019-11-25 23:51:29
问题 What is the difference between null and the \"\" (empty string)? I have written some simple code: String a = \"\"; String b = null; System.out.println(a == b); // false System.out.println(a.equals(b)); // false Both statements return false . It seems, I am not able to find what is the actual difference between them. 回答1: "" is an actual string, albeit an empty one. null, however, means that the String variable points to nothing. a==b returns false because "" and null do not occupy the same

Compare two List<T> objects for equality, ignoring order [duplicate]

我的未来我决定 提交于 2019-11-25 23:49:24
问题 This question already has answers here : Comparing two collections for equality irrespective of the order of items in them (19 answers) Closed 2 years ago . Yet another list-comparing question. List<MyType> list1; List<MyType> list2; I need to check that they both have the same elements, regardless of their position within the list. Each MyType object may appear multiple times on a list. Is there a built-in function that checks this? What if I guarantee that each element appears only once in

Equals(=) vs. LIKE

只谈情不闲聊 提交于 2019-11-25 23:39:59
问题 When using SQL, are there any benefits of using = in a WHERE clause instead of LIKE ? Without any special operators, LIKE and = are the same, right? 回答1: Different Operators LIKE and = are different operators. Most answers here focus on the wildcard support, which is not the only difference between these operators! = is a comparison operator that operates on numbers and strings. When comparing strings, the comparison operator compares whole strings . LIKE is a string operator that compares

Why would you use String.Equals over ==? [duplicate]

笑着哭i 提交于 2019-11-25 23:27:22
问题 This question already has answers here : C# difference between == and Equals() (17 answers) Closed 5 years ago . I recently was introduced to a large codebase and noticed all string comparisons are done using String.Equals() instead of == What\'s the reason for this, do you think? 回答1: It's entirely likely that a large portion of the developer base comes from a Java background where using == to compare strings is wrong and doesn't work. In C# there's no (practical) difference (for strings) as

Java comparison with == of two strings is false? [duplicate]

被刻印的时光 ゝ 提交于 2019-11-25 23:22:51
问题 This question already has an answer here: How do I compare strings in Java? 23 answers String parts is String[6]: [\"231\", \"CA-California\", \"Sacramento-155328\", \"aleee\", \"Customer Service Clerk\", \"Alegra Keith.doc.txt\"] But when I compare parts[0] with \"231\" : \"231\" == parts[0] the above result is false, I\'m confused, so could anybody tell me why? 回答1: The == operator compares the object references, not the value of the String s. To compare the values of String s, use the

How to override equals method in Java

北战南征 提交于 2019-11-25 23:01:32
问题 I am trying to override equals method in Java. I have a class People which basically has 2 data fields name and age . Now I want to override equals method so that I can check between 2 People objects. My code is as follows public boolean equals(People other){ boolean result; if((other == null) || (getClass() != other.getClass())){ result = false; } // end if else{ People otherPeople = (People)other; result = name.equals(other.name) && age.equals(other.age); } // end else return result; } //

Why do I need to override the equals and hashCode methods in Java?

て烟熏妆下的殇ゞ 提交于 2019-11-25 22:53:10
问题 Recently I read through this Developer Works Document. The document is all about defining hashCode() and equals() effectively and correctly, however I am not able to figure out why we need to override these two methods. How can I take the decision to implement these methods efficiently? 回答1: Joshua Bloch says on Effective Java You must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object.hashCode(), which

How to determine equality for two JavaScript objects?

浪子不回头ぞ 提交于 2019-11-25 22:51:58
问题 A strict equality operator will tell you if two object types are equal. However, is there a way to tell if two objects are equal, much like the hash code value in Java? Stack Overflow question Is there any kind of hashCode function in JavaScript? is similar to this question, but requires a more academic answer. The scenario above demonstrates why it would be necessary to have one, and I\'m wondering if there is any equivalent solution . 回答1: The short answer The simple answer is: No, there is