printing the address of the string instances
java, string
Solution
If you mean "address" as this:
System.out.println(new Object());
java.lang.Object@31ec0130
then you can just do,
String s = new String("one");
System.out.println(Integer.toHexString(s.hashCode()));
1ae66
Since what you think is the "address" is just the hashCode() converted to hex string.
Side Note This answer was historically accepted as correct and will only work for classes that didn't override the `hashCode()` method, but (as mentioned in comments) it will not work for `String` classes since they override the `hashCode()` method. Readers looking for up-to-date information on the topic of this question should first go through all the comments/discussion on this answer and should consult further resources, or ask a new question citing this question and answer and explicitly asking for new information on things that have changed since they were written.
Problem
``` package com.testProject; public class JavaSample { public static void main(String[] args) { String myString1 = new String("Sample1"); System.out.println(myString1); String myString2 = new String("Sample2"); System.out.println(myString2); } } ``` in the above piece of code how to print the address of these Strings which i created "Sample1" and "Sample2", i need to print the memory location of the String object myString1 and myString2