equals

Integer == int allowed in java

℡╲_俬逩灬. 提交于 2019-11-26 21:02:10
问题 I was wondering if java automatically turns a Integer into an int when comparing to an int? Or will the == try and compare references on primitives? Is this always true or do I need to do i.intValue()==2 ? Integer i = Integer.valueOf(2); if (i==2){ //always? } 回答1: Yes, when comparing int using == arguments will be unboxed if necessary. Relevant section from the Java Language Specification: 15.21.1 Numerical Equality Operators == and != If the operands of an equality operator are both of

Equals vs GetHashCode when comparing objects

我只是一个虾纸丫 提交于 2019-11-26 20:57:29
问题 Should we override both Equals and GetHashCode properties when implementing a custom class instances comparison? In the following code I have a collection of classes. The class A is compared by the ID , the class B - by Code . using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { List<I> bars = new List<I>(); bars.Add(new A() { Id = 1, Code = "one A" }); bars.Add(new B() {

Should one override equals method for asserting the object equality in a unit test?

青春壹個敷衍的年華 提交于 2019-11-26 20:54:16
问题 Let's say we are testing the result of a method by asserting the equality of all the properties of the result object with properties of an expected result object. Should we implement equals method and use Assert.AreEqual(expectedResult, actualResult)... But equals may mean something different in production code. Which is the best practice? Asserting the equality of the objects through overriden equals method or Asserting the equality of all the properties 回答1: I for one use custom assertions.

When do I need to override equals and hashcode methods? [duplicate]

拥有回忆 提交于 2019-11-26 20:44:46
问题 Possible Duplicate: Overriding equals and hashCode in Java If I have class A { int x = 1; } ... A a1 = new A(); A a2 = new A(); a1.equals(a2); If I compare 2 instances of A without override the equals method, will I get expected result? 回答1: If I compare 2 instances of A without override the equals method, will I get expected result? That depends on what you expect :) The default implementation will give you reference equality - in other words, when you compare two references, equals will

Java: Use hashCode() inside of equals() for convenience?

本秂侑毒 提交于 2019-11-26 20:36:20
问题 Consider the following test case, is it a bad practice to use the hashCode() method inside of equals as a convenient shortcut? public class Test { public static void main(String[] args){ Test t1 = new Test(1, 2.0, 3, new Integer(4)); Test t2 = new Test(1, 2.0, 3, new Integer(4)); System.out.println(t1.hashCode() + "\r\n"+t2.hashCode()); System.out.println("t1.equals(t2) ? "+ t1.equals(t2)); } private int myInt; private double myDouble; private long myLong; private Integer myIntObj; public

Is it possible in java make something like Comparator but for implementing custom equals() and hashCode()

我的未来我决定 提交于 2019-11-26 18:48:59
I have an array of objects and I want to concatenate it with another array of objects, except that objects that have same id's. That objects are used in many places in the system and don't have hash code or equals implemented. So I don't want to implement hashCode() and equals() , cause I'm afraid to break something somewhere in the system where that objects are used and I don't know about that. I want to put all that objects in a set, but somehow make the objects use custom hashCode() and equals() . Something like custom Comparator , but for equals. Yes it is possible to do such a thing. But

Hashcode and Equals for Hashset [duplicate]

匆匆过客 提交于 2019-11-26 18:40:33
This question already has an answer here: When does HashSet 'add' method calls equals? [duplicate] 4 answers Please clarify my doubt in Hashset. Consider the following code, class Person { String name; Person(String n) { name=n; } public String getName() { return name; } @Override public boolean equals(Object arg0) { System.out.println("in equals"); Person obj=(Person)arg0; System.out.println("1st "+getName()); System.out.println("2nd "+obj.getName()); if(this.getName().equals(obj.getName())) { return true; } return false; } @Override public int hashCode() { System.out.println("in hash code");

String equals and == with String concatenation [duplicate]

孤人 提交于 2019-11-26 18:35:13
问题 This question already has an answer here: Getting strange output when printing result of a string comparison 3 answers I am trying to understand the String concatenation with the output of String compare. To be clear, i have the class to compare two strings using == and equals. I am trying to concat the output of == and equals() to a string. The output of equals() concats, but the output of == does not concat. Using boxing feature of java, the boolean concatenated with string will contact.

Is relational comparison between int and float directly possible in C?

寵の児 提交于 2019-11-26 18:28:57
问题 I am using Visual Studio 6 with some old time code written in c. I found an issue where the code looks like this.. int x = 3; float y = 3.0; if(x == y){ do some crazy stuff } is this a valid comparison? is it possible at run time the allocation for the float is 3.0000001 and this would fail? 回答1: Well, I guess you won't be too surpised to hear that comparing floats for equality is a rookie mistake then. The problem is that many increments smaller than an integer values can't actually be

What's the difference between IEquatable and just overriding Object.Equals()?

喜你入骨 提交于 2019-11-26 18:16:10
I want my Food class to be able to test whenever it is equal to another instance of Food . I will later use it against a List, and I want to use its List.Contains() method. Should I implement IEquatable<Food> or just override Object.Equals() ? From MSDN: This method determines equality by using the default equality comparer, as defined by the object's implementation of the IEquatable.Equals method for T (the type of values in the list). So my next question is: which functions/classes of the .NET framework make use of Object.Equals() ? Should I use it in the first place? The main reason is