equals

Prevent stubbing of equals method

怎甘沉沦 提交于 2019-12-17 20:19:10
问题 I would like to test my class' equals() method but Mockito seems to be calling the stub version every time. My test is as follows; PluginResourceAdapter adapter = mock (PluginResourceAdapter.class); PluginResourceAdapter other = mock (PluginResourceAdapter.class); when(adapter.getNumberOfEndpointActivation()).thenReturn(1); when(other.getNumberOfEndpointActivation()).thenReturn(0); boolean result = adapter.equals(other); assertFalse(result); I know I cannot stub the equals method which means

How do I check if an object is equal to a new object of the same class?

▼魔方 西西 提交于 2019-12-17 19:17:00
问题 If I have a object like: public class Person { public int id {get;set;} public string name {get;set;} } And I want the behavior: Person a = new Person(); Person b = new Person(); a == b; and that a == b returns true, do I have to override the Object.Equals() method? or is there some other way of doing it without overriding the Equals method? EDIT I want to compare data, as I want to know if a external method that I call returns a new object or a object with different data than a new object

Best practices regarding equals: to overload or not to overload?

纵然是瞬间 提交于 2019-12-17 18:26:36
问题 Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[] args) { class Thing { final int x; Thing(int x) { this.x = x; } public int hashCode() { return x; } public boolean equals(Thing other) { return this.x == other.x; } } List<Thing> myThings = Arrays.asList(new Thing(42)); System.out.println(myThings.contains(new Thing(42))); // prints "false" } } Note that contains returns false !!! We seems to have lost our things!! The bug, of

XPath operator “!=”. How does it work?

你离开我真会死。 提交于 2019-12-17 18:25:42
问题 XML document: <doc> <A> <Node>Hello!</Node> </A> <B> <Node/> </B> <C> </C> <D/> </doc> How would you evaluate the following XPath queries? /doc/A/Node != 'abcd' /doc/B/Node != 'abcd' /doc/C/Node != 'abcd' /doc/D/Node != 'abcd' I would expect ALL of these to evaluate to true . However, here are the results: /doc/A/Node != 'abcd' true /doc/B/Node != 'abcd' true /doc/C/Node != 'abcd' false /doc/D/Node != 'abcd' false Is this expected behavior? Or is it a bug with my XPath provider (jaxen)? 回答1:

Where is the implementation of InternalEquals(object objA, object objB)

不想你离开。 提交于 2019-12-17 16:38:31
问题 While disassembling the .Net Source Code using Reflector, I came upon the Equals implementation in the Object Class and it refers to bool InternalEquals(object objA, object objB); Which again refers to internal static extern bool InternalEquals(object objA, object objB); I am now confused regarding where to find the implementation of this InternalEquals(object objA, object objB) function and how is it using this function and in which .Net assembly is this function defined and also if each and

Why are two AtomicIntegers never equal?

半城伤御伤魂 提交于 2019-12-17 16:25:22
问题 I stumbled across the source of AtomicInteger and realized that new AtomicInteger(0).equals(new AtomicInteger(0)) equals false . Why is this? Is it some "defensive" design choice related to concurrency issues? If so, what could go wrong if it was implemented differently? (I do realize I could use get and == instead.) 回答1: This is partly because an AtomicInteger is not a general purpose replacement for an Integer . The java.util.concurrent.atomic package summary states: Atomic classes are not

ReferenceEquals working wrong with strings

我的未来我决定 提交于 2019-12-17 16:13:25
问题 Why in this situation ReferenceEquals method of object behaves differently? string a= "fg"; string b= "fg"; Console.WriteLine(object.ReferenceEquals(a, b)); So in this situation it's get a result true . In case, it compares values of my strings and not references. But when I write something like: StringBuilder c = new StringBuilder("fg"); string d = c.ToString(); Console.WriteLine(object.ReferenceEquals(a, d)); In this case it works fine and result is false , because it compares references of

Demonstrating string comparison with Java

女生的网名这么多〃 提交于 2019-12-17 14:45:56
问题 I want to demonstrate with a few line of code that in Java, that to compare two strings ( String ), you have to use equals() instead of the operator == . Here is something I tried : public static void main(String Args[]) { String s1 = "Hello"; String s2 = "Hello"; if (s1 == s2) System.out.println("same strings"); else System.out.println("different strings"); } I was expecting this output : different strings , because with the test s1 == s2 I'm actually comparing two references (i.e. addresses

Why are these == but not `equals()`?

穿精又带淫゛_ 提交于 2019-12-17 09:35:55
问题 I'm a bit confused about the way Java treats == and equals() when it comes to int , Integer and other types of numbers. For example: Integer X = 9000; int x = 9000; Short Y = 9000; short y = 9000; List<Boolean> results = new ArrayList<Boolean>(); // results.add(X == Y); DOES NOT COMPILE 1) results.add(Y == 9000); // 2) results.add(X == y); // 3) results.add(X.equals(x)); // 4) results.add(X.equals(Y)); // 5) results.add(X.equals(y)); // 6) System.out.println(results); outputs (maybe you

Why does 1234 == '1234 test' evaluate to true? [duplicate]

那年仲夏 提交于 2019-12-17 08:54:22
问题 This question already exists : Closed 7 years ago . Possible Duplicate: php == vs === operator An easy answer for someone I'm sure. Can someone explain why this expression evaluates to true? (1234 == '1234 test') 回答1: Because you are using the == (similarity) operator and PHP is coercing the string to an int. To resolve it use the === (equality) operator, which checks not only if the value is the same, but also if the data type is the same, so "123" string and 123 int won't be considered