equals

equals method - how to override

筅森魡賤 提交于 2019-11-29 18:15:48
I need help on to override the equals method. I have everything working except for the equals method. The equals method that I currently have is not giving me the correct answer. I can not seem to figure out what could be the problem. My Class: package myclasses; public class Currency { private int dollars, cents; public Currency() { dollars = 0; cents = 0; } public Currency(int d, int c) { this.dollars = d; this.cents = c; setCents(cents); } public int getDollars() { return dollars; } public int getCents() { return cents; } private void setDollars(int dollars) { this.dollars = dollars; }

Is there a reason not to use <=> (null safe equals operator) in mysql instead of =?

瘦欲@ 提交于 2019-11-29 17:37:35
问题 MySQL provides a nice operator <=> that works with comparisons that could contain a null such as null <=> null or null <=> 5 etc. giving back intuitive results as many programming languages. Whereas the normal equals operator always just returns null, which catches many new MySQL users such as myself awry. Is there a reason MySQL has both and not JUST the functionality in <=> ? Who really needs an operator that is effectively undefined with built in language types? 回答1: Who really needs an

Strings don't seem to be equal in Java on Android, even though they print the same

两盒软妹~` 提交于 2019-11-29 17:16:05
问题 I've got a problem that I'm rather confused about. I have the following lines of code in my android application: System.out.println(CurrentNode.getNodeName().toString()); if (CurrentNode.getNodeName().toString() == "start") { System.out.println("Yes it does!"); } else { System.out.println("No it doesnt"); } When I look at the output of the first println statement it shows up in LogCat as "start" (without the quotes obviously). But then when the if statement executes it goes to the else

What does the .= operator mean in PHP?

一笑奈何 提交于 2019-11-29 15:59:40
问题 I have a variable that is being defined as $var .= "value"; How does the use of the dot equal function? 回答1: It's the concatenating assignment operator. It works similarly to: $var = $var . "value"; $x .= differs from $x = $x . in that the former is in-place, but the latter re-assigns $x . 回答2: This is for concatenation $var = "test"; $var .= "value"; echo $var; // this will give you testvalue 回答3: the " . " operator is the string concatenation operator. and " .= " will concatenate strings.

hashcode()和equals()的是是非非

懵懂的女人 提交于 2019-11-29 15:51:57
我们在很多博客的文章当中,我们都看到这样一句话:在重写equals方法的同时一定要重写hashCode方法。这是为什么?很多人会说,我的业务代码,只用equals比较比较两个对象是否相等不就可以了,为什么要重写hashcode? 其实上面说的话不完全是错的,如果说你的对象在业务里永远不会放入到hash容器当中,完全不用考虑重写hashcode方法的问题,但是你的对象如果要放到HashMap这样的以hash code为基础的容器中时,就要考虑重写hashcode方法。 再准确一点的说,如果你的对象在HashMap、HashTable等中要作为key存放的时候,还有对象放到HashSet中时,就一定要重写equals和hashcode。为什么?我们来看一下HashSet的add方法的实现吧。 HashSet的add方法调用了HashMap的put方法,因为HashSet的底层本质,还是一个Map,只是value是一个固定的Object对象。 HashMap中,e.hash其实就是key的hash值。HashMap判断key值是否相同的步骤是: 判断key的hash值是否相等,这个判断是使用的HashMap中的hash方法,该方法的实现中,如果对象不是String,会直接调用该对象的hashCode方法 同时判断key的地址是否相等或者equals相等。 综合上面的两个条件

Why .equals method is failing on two same value objects?

蓝咒 提交于 2019-11-29 15:47:43
I have created a value object MarketVO and two instances of this value object have same elements and same value for each element. My value object class is: public class MarketVO { private double floatAmt; private Date marketDate; private long marketCap; } Here are the values: returnedData: FloatAmt: 247657.5418618201, MarketCap: 5249164, MarketDate: 2011-07-29 00:00:00.0 expectedData: FloatAmt: 247657.5418618201, MarketCap: 5249164, MarketDate: 2011-07-29 00:00:00.0 Now in my unit test class, I want to assert that my returned and expected type is same containing same value in same order so am

How does equals() method work in Java [duplicate]

笑着哭i 提交于 2019-11-29 14:41:16
This question already has an answer here: What is the difference between == and equals() in Java? 23 answers The equals method compares whether two object values are equal or not. My question is how it compares the two objects? How can it tell the two objects are equal or not? I want to know based on what it compares the two objects. I am not including the hashCode method. The default implementation, the one of the class java.lang.Object , simply tests the references are to the same object : 150 public boolean equals(Object obj) { 151 return (this == obj); 152 } The reference equality operator

A Mechanism for having different equals (physical equals and logical equals) on objects in Collection

北慕城南 提交于 2019-11-29 14:24:59
Is there any Equalator mechanism like Comparator so I can have different equals for coparing lists? EDIT: My goal is to differentiate between current list1.equals(list2) which checks if its a shallow copy or also a deep copy with all objects a.equals(b) and list1.identical(list2) which checks if its simply shallow copy with unmodified listing All these lists are from the same model. Some are copies of themselves so they hold the pointer to same objects, and others are deep copies so hierarchy is totally replicated, because they have updates in content, not just in structure. I find myself

Comparing primitive to wrapper object with == behaviour unexplained

只谈情不闲聊 提交于 2019-11-29 10:53:45
问题 I have a piece of code which I need to understand: public static void main(String[] args) { Character c = new Character('a'); Character cy = new Character('a'); char cx = 'a'; System.out.println(c == cx); System.out.println(cx == cy); System.out.println(c == cy); } Output: true true false I am unable to understand why only the third statement is failing. EDIT: This question is different to the .equals vs == question as this about primitive versus object comparison. 回答1: c and cy refer to

What are not 2 Long variables equal with == operator to compare in Java?

我怕爱的太早我们不能终老 提交于 2019-11-29 09:11:21
I got a very strange problem when I'm trying to compare 2 Long variables, they always show false and I can be sure they have the same number value by debugging in Eclipse: if (user.getId() == admin.getId()) { return true; // Always enter here } else { return false; } Both of above 2 return values are object-type Long, which confused me. And to verify that I wrote a main method like this: Long id1 = 123L; Long id2 = 123L; System.out.println(id1 == id2); It prints true. So can somebody give me ideas?. I've been working in Java Development for 3 years but cannot explain this case. BlackJoker ==