Feel free to copy the full code from the bottom of this page into your own IDE and try it out!
Strings are immutable in Java so when you try to edit a string by taking a substring or concatenating it will sometimes create an entirely new String object to store the result.
String name = "Zuko";
String constructorCopy = new String(name);
String substring0Copy = name.substring(0);
String substring4Copy = "asdfZuko".substring(4);
String plusEmptyStrCopy = name + "";
String combinedLiterals = "Zu" + "ko";
String firstLetter = "Z";
String combinedLiterals2 = firstLetter + "uko";
String result = doesNothingFunction("Zuko");
String result2 = pigLatinFunction("Zuko");
////////////// print out addresses of everything ////////////////
syso("\"Zuko\" is at", addressOf("Zuko"));
syso("name is at", addressOf(name));
syso("constructorCopy is at", addressOf(constructorCopy));
syso("substring0Copy is at", addressOf(substring0Copy));
syso("substring4Copy is at", addressOf(substring4Copy));
syso("\"asdfZuko\" is at", addressOf("asdfZuko"));
syso("plusEmptyStrCopy is at", addressOf(plusEmptyStrCopy));
syso("combinedLiterals is at", addressOf(combinedLiterals));
syso("combinedLiterals2 is at", addressOf(combinedLiterals2));
syso("result is at", addressOf(result));
syso("result2 is at", addressOf(result2));
syso("null is at", addressOf(null));
param1 of doesNothingFunction is at 0xed5ed01f
param2 of pigLatinFunction is at 0xed5ed01f
"Zuko" is at 0xed5ed01f
name is at 0xed5ed01f
constructorCopy is at 0xed5ed025
substring0Copy is at 0xed5ed01f
substring4Copy is at 0xed5ed02f
"asdfZuko" is at 0xed5ed028
plusEmptyStrCopy is at 0xed5ed077
combinedLiterals is at 0xed5ed01f
combinedLiterals2 is at 0xed5ed08d
result is at 0xed5ed01f
result2 is at 0xed5ed7e1
null is at 0x0
Need to configure problem severity to set Forbidden reference (access rules) to Warning instead of Error in project settings
import sun.misc.Unsafe;
import java.lang.reflect.*;
public class StringAddress {
public void run() {
String name = "Zuko";
String constructorCopy = new String(name);
String substring0Copy = name.substring(0);
String substring4Copy = "asdfZuko".substring(4);
String plusEmptyStrCopy = name + "";
String combinedLiterals = "Zu" + "ko";
String firstLetter = "Z";
String combinedLiterals2 = firstLetter + "uko";
String result = doesNothingFunction("Zuko");
String result2 = pigLatinFunction("Zuko");
////////////// print out addresses of everything ////////////////
syso("\"Zuko\" is at", addressOf("Zuko"));
syso("name is at", addressOf(name));
syso("constructorCopy is at", addressOf(constructorCopy));
syso("substring0Copy is at", addressOf(substring0Copy));
syso("substring4Copy is at", addressOf(substring4Copy));
syso("\"asdfZuko\" is at", addressOf("asdfZuko"));
syso("plusEmptyStrCopy is at", addressOf(plusEmptyStrCopy));
syso("combinedLiterals is at", addressOf(combinedLiterals));
syso("combinedLiterals2 is at", addressOf(combinedLiterals2));
syso("result is at", addressOf(result));
syso("result2 is at", addressOf(result2));
syso("null is at", addressOf(null));
}
public String doesNothingFunction(String param1) {
syso("param1 of doesNothingFunction is at ", addressOf(param1));
return param1;
}
public String pigLatinFunction(String param2) {
syso("param2 of pigLatinFunction is at ", addressOf(param2));
return param2.substring(1) + param2.charAt(0);
}
public static void main(String[] args) {
new StringAddress().run();
}
public void syso(String one, String two) {
StringBuilder sb = new StringBuilder();
sb.append(one);
sb.append(two);
while(sb.length() < 35) {
sb.insert(one.length(), ' ');
}
System.out.println(sb.toString());
}
public String addressOf(Object o) {
Object[] helperArray = new Object[] {o};
long baseOffset = unsafe.arrayBaseOffset(Object[].class);
long address = unsafe.getLong(helperArray, baseOffset);
return String.format("0x%x", address);
}
Unsafe unsafe;
public StringAddress() {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
unsafe = (Unsafe) f.get(null);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
}