Code that corrupts the concatenated string when compiled with Eclipse:
public class Main { public static void main(String[] args) throws Exception { System.out.println("Java Version: " + System.getProperty("java.vm.version")); long[] durations = new long[60]; for (int i = 0; true; i++) { // this empty for-loop is required to reproduce this bug for (long duration : durations) { // do nothing } { String s = "test"; int len = s.length(); s = s + s; len = len + len; s = s + s; len = len + len; s = s + s; len = len + len; if (s.length() != len) { System.out.println("Failed at iteration: " + i); System.out.println("Length mismatch: " + s.length() + " <> " + len); System.out.println("Expected: \"" + "test" + "test" + "test" + "test" + "test" + "test" + "test" + "test" + "\""); System.out.println("Actual: \"" + s + "\""); System.exit(0); } } } } }
Code that corrupts the concatenated string when compiled with Javac:
public class Main { public static void main(String[] args) throws Exception { System.out.println("Java Version: " + System.getProperty("java.vm.version")); long[] durations = new long[60]; for (int i = 0; true; i++) { // this empty for-loop is required to reproduce this bug for (long duration : durations) { // do nothing } { String s = "test"; int len = s.length(); s = new StringBuilder(String.valueOf(s)).append(s).toString(); len = len + len; s = new StringBuilder(String.valueOf(s)).append(s).toString(); len = len + len; s = new StringBuilder(String.valueOf(s)).append(s).toString(); len = len + len; if (s.length() != len) { System.out.println("Failed at iteration: " + i); System.out.println("Length mismatch: " + s.length() + " <> " + len); System.out.println("Expected: \"" + "test" + "test" + "test" + "test" + "test" + "test" + "test" + "test" + "\""); System.out.println("Actual: \"" + s + "\""); System.exit(0); } } } } }
Output:
Java Version: 23.0-b21 Failed at iteration: 11983 Length mismatch: 16 <> 32 Expected: "testtesttesttesttesttesttesttest" Actual: "nullnulltesttest"