Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Last edited by helloworld922; September 5th, 2010 at 11:48 PM.
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?)
what is there at the addresses of s1 and s2 ?
Last edited by helloworld922; September 6th, 2010 at 03:27 PM.
For object equality, you should be using the equals as opposed to equality with '==', which is more for primitives. Using your example:
For custom objects, you can override the Object.equals method to accomplish this task. In the case of String, you could also call intern() on your strings, which causes the String to be stored/recalled from memory, in which case the == will return true.
Last edited by copeg; September 6th, 2010 at 02:24 PM.
American (September 6th, 2010)