Saturday 19 November 2011

difference between equals and == in java

hello friends from two days i working behind this topic , and now i can say that i can tell you equals method easily because i find some methods in java which can clears your concept easily . but still at the point of " == " still i found nothing for " ==" so sorry but you can also make a search for it . please try your best for finding the answer .

look at the what type of things are happened inside equals method or why it is different from "==" :---
                                                                                                             java is very good for users , community just add this method inside Object class that anyone can use this method . so what is the process for equals method .
1. create any object .
2. find its hash code .
    using ob.hashCode( );
    by finding hashCode , now you have the object identity , and the identity lies between                                                    Integer .MAX_VALUE + Integer.MIN_VALUE ,
3. find hexString of this hashCode .
    you can find the hashCode with the help of Integer.toHexString( ob.hashCode( ) ) ; , now i find that the hashCode is just a object-id and  Integer.toHexString( ob.hashCode( ) ) is works as a reference . so when ever you perform operation like ..
 now from here what i want to explain think about this !
here i just put code for the previous explained thing .
here i just compare the hash code of two string and found "yes_1" in the output . means hash code only works as the object id .
now the main thing starts here Integer.toHexString(str.hashCode() )  this works as reference , so the link created for this reference is created from stack , and str & str_1 are stored into stack with different addresses and == also deals with stack so it prints no_1 in the output .

now here the main thing comes the equals method which works on heap , means which is stored inside heap so by this string stores same thing so things are type copied from string pool so content is same and the output is yes_1
so the difference is also cleared from 3 & 4 picture with a explanation .


No comments: