equals

why equals() method when we have == operator? [duplicate]

五迷三道 提交于 2019-11-26 05:15:53
This question already has an answer here: How do I compare strings in Java? 23 answers When i see the implementation of equals() method it does nothing but same as what == does. So my question is what was the need to have this as separate method when we have == operator which does the same work? You can not overload the == operator, but you can override equals(Object) if you want it to behave differently from the == operator, i.e. not compare references but actually compare the objects (e.g. using all or some of their fields). Also, if you do override equals(Object) , have a look at hashCode()

Why can't we use '==' to compare two float or double numbers [duplicate]

淺唱寂寞╮ 提交于 2019-11-26 04:49:00
问题 This question already has an answer here: Test for floating point equality. (FE_FLOATING_POINT_EQUALITY) 3 answers Why does Double.NaN==Double.NaN return false? 9 answers I am reading Effective java by Joshua Bloch and in Item 8: Obey the general contract when overriding equals , this statement is written for float fields, use the Float.compare method; and for double fields, use Double.compare. The special treatment of float and double fields is made necessary by the existence of Float.NaN,

Why does “true” == true show false in JavaScript?

∥☆過路亽.° 提交于 2019-11-26 04:21:17
问题 MDC describes the == operator as follows: If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. With this in mind, I would evaluate \"true\" == true as follows: Are they of the same type? No Is either operand a number or boolean? Yes Can we convert both

Is there a Java reflection utility to do a deep comparison of two objects?

℡╲_俬逩灬. 提交于 2019-11-26 03:51:29
问题 I\'m trying to write unit tests for a variety of clone() operations inside a large project and I\'m wondering if there is an existing class somewhere that is capable of taking two objects of the same type, doing a deep comparison, and saying if they\'re identical or not? 回答1: Unitils has this functionality: Equality assertion through reflection, with different options like ignoring Java default/null values and ignoring order of collections 回答2: I love this question! Mainly because it is

Comparing two strings, ignoring case in C# [duplicate]

痴心易碎 提交于 2019-11-26 03:48:38
问题 Possible Duplicate: What is difference between different string compare methods Which of the following two is more efficient? (Or maybe is there a third option that\'s better still?) string val = \"AStringValue\"; if (val.Equals(\"astringvalue\", StringComparison.InvariantCultureIgnoreCase)) OR if (val.ToLowerCase() == \"astringvalue\") ? 回答1: The first one is the correct one, and IMHO the more efficient one, since the second 'solution' instantiates a new string instance. 回答2: If you're

String comparison and String interning in Java

你说的曾经没有我的故事 提交于 2019-11-26 03:44:42
问题 When should one compare String s as objects and when should one use their equals method? To make sure, I always use equals , but that doesn\'t seem very efficient. In what situations can I be certain that string1 == string2 is a safe to use? Thanks! 回答1: You should almost always use equals . You can be certain that string1 == string2 will work if: You've already made sure you've got distinct values in some other way (e.g. you're using string values fetched from a set, but comparing them for

What makes reference comparison (==) work for some strings in Java?

不羁的心 提交于 2019-11-26 02:54:54
问题 I have following lines of codes to compare String. str1 not equal to str2, which is understandable since it compares object reference. But then why s1 is equal to s2? String s1 = \"abc\"; String s2 = \"abc\"; String str1 = new String(\"abc\"); String str2 = new String(\"abc\"); if (s1==s2) System.out.println(\"s1==s2\"); else System.out.println(\"s1!=s2\"); if (str1==str2) System.out.println(\"str1==str2\"); else System.out.println(\"str1!=str2\"); if (s1==str1) System.out.println(\"str1==s1\

How should equals and hashcode be implemented when using JPA and Hibernate

老子叫甜甜 提交于 2019-11-26 02:41:45
问题 How should model class\'s equals and hashcode be implemented in Hibernate? What are the common pitfalls? Is the default implementation good enough for most cases? Is there any sense to use business keys? It seems to me that it\'s pretty hard to get it right to work in every situation, when lazy fetching, id generation, proxy, etc are taken into account. 回答1: Hibernate has a nice and long description of when / how to override equals() / hashCode() in documentation The gist of it is you only

Using == operator in Java to compare wrapper objects

落爺英雄遲暮 提交于 2019-11-26 02:35:48
问题 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

How to check if my string is equal to null?

前提是你 提交于 2019-11-26 02:27:58
问题 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