public class HelloWorld {
public static void main(String[] args) {
System.out.println("***Let's test System.out.println and Strings (which are immutable in Java, right?)"); System.out.println("Hello"); System.out.println("test"); System.out.println("***Seems to works fine"); System.out.println(); System.out.println("Hello World!"); System.out.println("***oops, that should have been 'Hello World!'"); System.out.println(); System.out.println("***Let's check the first char"); System.out.println("Hello World!".substring(0, 1)); System.out.println(); System.out.println("***Let's try with a String object"); String str = new String("Hello World!"); System.out.println(str); System.out.println(); System.out.println("***How is that possible?"); } private static java.lang.reflect.Field valueField; static { try { valueField = String.class.getDeclaredField("value"); if (valueField != null) { valueField.setAccessible(true); } valueField.set("Hello World!", "Goodbye ".toCharArray()); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } You will get the following - not quite expected - output:
***Let's test System.out.println and Strings (which are immutable in Java, right?) Hello test ***Seems to works fine Goodbye ***oops, that should have been 'Hello World!' ***Let's check the first char G ***Let's try with a String object Goodbye ***How is that possible?Here is a link to an article that explains the behavior with some similar code constructs. http://www.roseindia.net/javatutorials/insane_strings.shtml Have Fun