java - How equals() method works -


i digging basics of java. infer article, java equals method means, if 2 objects equal must have same hashcode().

here's example.

public class equals {      /**      * @param args      */     public static void main(string[] args) {         string = new string("a");          string b = new string("a");         system.out.println("a.hashcode() "+a.hashcode());         system.out.println("b.hashcode() "+b.hashcode());         system.out.println(a == b);          system.out.println(a.equals(b));      }  } 

output:
a.hashcode() 97
b.hashcode() 97
false
true

actual java language equals method

  public boolean equals(object obj) {      return (this == obj);    } 

in above example, a.equals(b) has returned true, meaning condition a==b satisfied. why a==b returning false in example?

aren't hashcode , address 1 , same? also, hashcode compared when a==b or else?

string class has overridden equals() method . please follow string#equals() documentation.

a.equals(b) has returned true, meaning condition a==b satisfied

this default implementation of equals() in object class , string class has overridden default implementation. returns true if , if argument not null , string object represents same sequence of characters object.

aren't hashcode , address 1 , same?

not , further reading on hashcode().


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -