equals

Difference between == operator and Equals() method in C#?

扶醉桌前 提交于 2019-11-27 04:41:28
问题 What is the difference between == and Equals() with example? I know that == is used to compare operator and Equals() method is used to compare content of string.So i tried // first example string s1 = "a"; string s2 = "a"; Console.Write(a.Equals(s2)); // returns true, but if I assign "b" to s2, // then result will be false // second example string s1 ="a"; string s2 ="a"; Console.Write(s1 == s2); // returns true How this is so? Both are different object references. Suppose we consider that

Implementing equals and hashCode for objects with circular references in Java

房东的猫 提交于 2019-11-27 04:37:23
问题 I have two classes defined such that they both contain references to the other object. They look similar to this (this is simplified; in my real domain model class A contains a list of B and each B has a reference back to parent A): public class A { public B b; public String bKey; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((b == null) ? 0 : b.hashCode()); result = prime * result + ((bKey == null) ? 0 : bKey.hashCode()); return result; }

Java - equals method in base class and in subclasses

我怕爱的太早我们不能终老 提交于 2019-11-27 04:32:00
I have a simple base class, which is later extended by many separate classes, which potentially introduce new fields, but not necessarily. I defined an equals method in the base class, but also overriden that for a few subclasses. Is it OK to mix definitions in base/subclasses? In my case it was to avoid code duplication checking the same fields. Raph Take a look at "Implementing equals() To Allow Mixed-Type Comparison" from Angelika Langer . Here is a brief explanation of some problems and a possible solution: The equals contract says (amongst others): It is symmetric: for any non-null

Why Java does not see that Integers are equal?

假如想象 提交于 2019-11-27 04:23:38
I have integers that are supposed to be equal (and I verify it by output). But in my if condition Java does not see these variables to have the same value. I have the following code: if (pay[0]==point[0] && pay[1]==point[1]) { game.log.fine(">>>>>> the same"); } else { game.log.fine(">>>>>> different"); } game.log.fine("Compare:" + pay[0] + "," + pay[1] + " -> " + point[0] + "," + point[1]); And it produce the following output: FINE: >>>>>> different FINE: Compare:: 60,145 -> 60,145 Probably I have to add that point is defined like that: Integer[] point = new Integer[2]; and pay us taken from

What does comparison being consistent with equals mean ? What can possibly happen if my class doesn't follow this principle?

核能气质少年 提交于 2019-11-27 04:15:47
From the JavaDoc of TreeMap : Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map

Why does `Array(0,1,2) == Array(0,1,2)` not return the expected result?

我的梦境 提交于 2019-11-27 04:13:42
As far as I understand, Scala's == defines the natural equality of two objects. I expected that Array(0,1,2) == Array(0,1,2) compares the natural equality. For example, checks if all elements of the array return true when compared with the corresponding elements of the other array. People told me that Scala's Array is just a Java [] which only compares identity. Wouldn't it be more meaningful to override Array 's equals method to compare natural equality instead? Scala 2.7 tried to add functionality to Java [] arrays, and ran into corner cases that were problematic. Scala 2.8 has declared that

double equals vs is in python [duplicate]

好久不见. 提交于 2019-11-27 03:28:52
This question already has an answer here: Is there a difference between “==” and “is”? 15 answers String comparison in Python: is vs. == [duplicate] 4 answers I run the following in the Python interpreter: >>> foo = 10 >>> dir(foo) == dir(10) True >>> dir(foo) is dir(10) False >>> Why is this? is checks that 2 arguments refer to the same object, == checks that 2 arguments have the same value. dir() returns a list which contains the same data for both foo and 10 , but the actual list instances for the 2 things are different. 来源: https://stackoverflow.com/questions/15008380/double-equals-vs-is

Overriding GetHashCode for mutable objects?

删除回忆录丶 提交于 2019-11-27 03:10:47
I've read about 10 different questions on when and how to override GetHashCode but there's still something I don't quite get. Most implementations of GetHashCode are based on the hash codes of the fields of the object, but it's been cited that the value of GetHashCode should never change over the lifetime of the object. How does that work if the fields that it's based on are mutable? Also what if I do want dictionary lookups etc to be based on reference equality not my overridden Equals ? I'm primarily overriding Equals for the ease of unit testing my serialization code which I assume

What's wrong with defining operator == but not defining Equals() or GetHashCode()?

让人想犯罪 __ 提交于 2019-11-27 03:08:19
问题 For the code below public struct Person { public int ID; public static bool operator ==(Person a, Person b) { return a.Equals(b); } public static bool operator !=(Person a, Person b) { return !a.Equals(b); } } Why does the compiler give me these warnings? What's wrong with not defining the methods below? warning CS0660: 'Person' defines operator == or operator != but does not override Object.Equals(object o) warning CS0661: 'Person' defines operator == or operator != but does not override

Compare two objects with a check for null

我只是一个虾纸丫 提交于 2019-11-27 03:02:11
问题 Is there a method in the JDK that compares two objects for equality, accounting for nulls? Something like this: public static boolean equals(Object o1, Object o2) { if (o1 == null) { return o2 == null; // Two nulls are considered equal } else if (o2 == null) { return false; } return o1.equals(o2); } It seems silly to write this method myself since I would think that it has to exist already somewhere. 回答1: Java 7.0 added a new handy class: Objects. It has a method exactly for this: Objects