true false false
3254818
3254818
3254818
3254818
javadocs wrote:Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.
The general contract of hashCode is:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
true false false
3254818
3254818
3254818
3254818
true
true
Narayana Bojja wrote:Why s1==s3 is false and s1.hashCode() == s3.hashCode() is true in below code
Java Language Specification wrote:15.21.3. Reference Equality Operators == and !=
If the operands of an equality operator are both of either reference type or the null type, then the operation is object equality.
It is a compile-time error if it is impossible to convert the type of either operand to the type of the other by a casting conversion (§5.5). The run-time values of the two operands would necessarily be unequal (ignoring the case where both values are null).
At run time, the result of == is true if the operand values are both null or both refer to the same object or array; otherwise, the result is false.
The result of != is false if the operand values are both null or both refer to the same object or array; otherwise, the result is true.
While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters (§3.10.5). The contents of two strings s and t can be tested for equality by the method invocation s.equals(t).
If only people would learn those operators' correct names, which tell you that they test whether two references are the same, not whether you have two objects with the same state.salvin francis wrote:. . .
Java Language Specification wrote:15.21.3. Reference Equality Operators . . .
salvin francis wrote:When you compare two objects using "==", then it will return true only if the two objects are exactly the same.
In which case people should be taught that the numeric equality operator looks like the same object operator. Not vice versa.Mike Simmons wrote:. . . they look just like Numeric Equality Operators or Boolean Equality Operators.
Consider Paul's rocket mass heater. |