Can anyone help give the code to find address of s ?
In both cases it's actually calling the toString() method. The default toString method for objects is to append the class name followed by the hashCode (default is the memory address).
For obvious reasons, the String toString() method returns itself. However, the String class also overrides the hashCode() method so there's no easy way to retrieve the memory address of the string (I don't think it's possible without using JNI).
The larger question is why would you want to know the memory address of strings? In Java, you can't manually modify this value, except by setting it to a "valid" object of that type or null.
I wanted to know the address for the reason that :
I understand why it's false in the first case. If it's true in the second then what is true (is it the address?)Code Java:
what is there at the addresses of s1 and s2 ?
For object equality, you should be using the equals as opposed to equality with '==', which is more for primitives. Using your example: