equals

Using == operator in Java to compare wrapper objects

爱⌒轻易说出口 提交于 2019-11-26 11:48:00
I'm reading SCJP Java 6 by Kathy Sierra and Bert Bates and this book is confusing me so much. On page 245 they state that the following code below. Integer i1 = 1000; Integer i2 = 1000; if(i1 != i2) System.out.println("different objects"); //Prints output different objects Then on the very next page they have the following code Integer i3 = 10; Integer i4 = 10; if(i3 == i4) System.out.println("same objects"); //Prints output same objects I'm so confused! When I try this out on my own it seems that you cannot use the == to compare the same way you would use equals() method. Using the == always

How to check if my string is equal to null?

♀尐吖头ヾ 提交于 2019-11-26 11:34:06
I want to perform some action ONLY IF my string has a meaningful value. So, I tried this. if (!myString.equals("")) { doSomething } and this if (!myString.equals(null)) { doSomething } and this if ( (!myString.equals("")) && (!myString.equals(null))) { doSomething } and this if ( (!myString.equals("")) && (myString!=null)) { doSomething } and this if ( myString.length()>0) { doSomething } And in all cases my program doSomething in spite on the fact that my string IS EMPTY. It equals to null . So, what is wrong with that? ADDED: I found the reason of the problem. The variable was declared as a

When Should a .NET Class Override Equals()? When Should it Not?

心已入冬 提交于 2019-11-26 11:22:10
问题 The VS2005 documentation Guidelines for Overloading Equals() and Operator == (C# Programming Guide) states in part Overriding operator == in non-immutable types is not recommended. The newer .NET Framework 4 documentation Guidelines for Implementing Equals and the Equality Operator (==) omits that statement, although one post in Community Content repeats the assertion and references the older documentation. It seems that it is reasonable to override Equals() at least for some trivial mutable

Java - equals method in base class and in subclasses

南楼画角 提交于 2019-11-26 11:10:22
问题 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. 回答1: Take a look at "Implementing equals() To Allow Mixed-Type Comparison" from Angelika Langer . Here is a brief explanation of some problems and a

Overriding GetHashCode for mutable objects?

感情迁移 提交于 2019-11-26 10:26:00
问题 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

Should I write equals() and hashCode() methods in JPA entities?

若如初见. 提交于 2019-11-26 10:14:36
问题 I want to check if entity is in a Collection member ( @OneToMany or @ManyToMany ) of another entity: if (entity2.getEntities1().contains(entity1)) { } 回答1: Not necessarily. There are three options: don't override - thus you will be working with instances. This is fine in cases when you are working with the collections with only entities that are attached to the session (and hence guaranteed to be the same instance). This is (for me) the preferred way in many cases, because it requires less

Why Java does not see that Integers are equal?

一个人想着一个人 提交于 2019-11-26 09:42:03
问题 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

double equals vs is in python [duplicate]

荒凉一梦 提交于 2019-11-26 09:24:09
问题 This question already has an answer here: Is there a difference between “==” and “is”? 14 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? 回答1: 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

How default .equals and .hashCode will work for my classes?

别说谁变了你拦得住时间么 提交于 2019-11-26 09:21:37
问题 Say I have my own class public class MyObj { /* ... */ } It has some attributes and methods. It DOES NOT implement equals, DOES NOT implement hashCode. Once we call equals and hashCode, what are the default implementations? From Object class? And what are they? How the default equals will work? How the default hashCode will work and what will return? == will just check if they reference to the same object, so it\'s easy, but what about equals() and hashCode() methods? 回答1: Yes, the default

What's the best strategy for Equals and GetHashCode?

允我心安 提交于 2019-11-26 08:27:33
问题 I\'m working with a domain model and was thinking about the various ways that we have to implement these two methods in .NET. What is your preferred strategy? This is my current implementation: public override bool Equals(object obj) { var newObj = obj as MyClass; if (null != newObj) { return this.GetHashCode() == newObj.GetHashCode(); } else { return base.Equals(obj); } } // Since this is an entity I can use its Id // When I don\'t have an Id, I usually make a composite key of the properties