Is a String in Java really immutable?

We all know of the famous java.lang.String class in Java. The very next thing that comes to our mind when we see this class is, "oh, yeah. Strings are immutable."

But...are they, really? Let's take a closer look.

Consider the following snippet of code.

    String user = "guest";
    System.out.println("user is of type: " + user);
    Class<String> type = String.class;
    Field field = type.getDeclaredField("value");
    field.setAccessible(true);
    char [] chars = (char[]) field.get(user);
    chars[0] = 'a';
    chars[1] = 'd';
    chars[2] = 'm';
    chars[3] = 'i';
    chars[4] = 'n';

    System.out.println("user is of type" + user);

(This piece of code will run on JDK 8, using the Reflection API. However, in JDK 11, it will throw java.lang.ClassCastException)

So there we go, Strings are...technically NOT immutable. :P