equals

Equals method for data class in kotlin

自作多情 提交于 2019-11-30 12:25:30
问题 I have the following data class data class PuzzleBoard(val board: IntArray) { val dimension by lazy { Math.sqrt(board.size.toDouble()).toInt() } } I read that data classes in Kotlin get equals()/hashcode() method for free. I instantiated two objects. val board1 = PuzzleBoard(intArrayOf(1,2,3,4,5,6,7,8,0)) val board2 = PuzzleBoard(intArrayOf(1,2,3,4,5,6,7,8,0)) But still the following statements return false. board1 == board2 board1.equals(board2) 回答1: In Kotlin data classes equality check,

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

送分小仙女□ 提交于 2019-11-30 12:24:22
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? Who really needs an operator that is effectively undefined with built in language types? You asked for some real-world examples.

T[].Contains for struct and class behaving differently

末鹿安然 提交于 2019-11-30 12:20:55
This is a followup question to this: List<T>.Contains and T[].Contains behaving differently T[].Contains is behaving differently when T is class and struct. Suppose I have this struct : public struct Animal : IEquatable<Animal> { public string Name { get; set; } public bool Equals(Animal other) //<- he is the man { return Name == other.Name; } public override bool Equals(object obj) { return Equals((Animal)obj); } public override int GetHashCode() { return Name == null ? 0 : Name.GetHashCode(); } } var animals = new[] { new Animal { Name = "Fred" } }; animals.Contains(new Animal { Name = "Fred

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

孤街浪徒 提交于 2019-11-30 11:34:19
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 statement and prints "No it doesn't". I wondered if the name of the node might have some kind of non-printing

What does the .= operator mean in PHP?

百般思念 提交于 2019-11-30 11:05:14
I have a variable that is being defined as $var .= "value"; How does the use of the dot equal function? 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 . This is for concatenation $var = "test"; $var .= "value"; echo $var; // this will give you testvalue the " . " operator is the string concatenation operator. and " .= " will concatenate strings. Example: $var = 1; $var .= 20; This is same as: $var = 1 . 20; the " .= " operator is a string operator, it first

equals method - how to override

倖福魔咒の 提交于 2019-11-30 10:09:55
问题 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

equals() and operator “==” in java

蓝咒 提交于 2019-11-30 09:44:25
I know that equals() will compare the value of objects, the '==' operator will check if the variable point to the same memory. I do not understand how equals() compare the value of objects, for example: class Test { public Test(int x, float y) { this.x = x; this.y = y; } int x, float y; } Test test1 = new Test(1,2.0); Test test2 = new Test(1,2.0); So if I use equals() , will it compare each properties in each object? And what about if we are talking about String? using equals() and operator “==”, do we still need to override the equals()? No, if you don't override the equals -method in your

Google App Engine, JDO, and equals/hashCode

混江龙づ霸主 提交于 2019-11-30 08:56:47
I've got an app in Google App Engine that was working fine. I realized that one on of my JDO-enhanced objects that I forgot to implement equals and hashCode (I need to use the object in a set). So I did. I didn't really do anything special in these implementations, in fact I just used Eclipse to generate them. Like so: @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Long id; @Persistent private String appleId; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((appleId == null) ? 0 : appleId.hashCode()); return

Comparing primitive to wrapper object with == behaviour unexplained

懵懂的女人 提交于 2019-11-30 07:59:43
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. c and cy refer to different instances of the Character class (each time you invoke a constructor, you create a new instance), so

Why can't I “static import” an “equals” method in Java?

风格不统一 提交于 2019-11-30 07:56:45
I like using this method here: org.apache.commons.lang.ObjectUtils.equals(Object object1, Object object2) The only drawback (compared to Google Guava, for instance), is that I cannot static import the method. I.e. this is useless: import static org.apache.commons.lang.ObjectUtils.equals; ... as my Eclipse compiler will not correctly link that method when writing equals(obj1, obj2); The error is: The method equals(Object) in the type Object is not applicable for the arguments (..., ...) Why is that? Is my statically imported method not applicable if there is a method with the same name (but not