I am trying to swap two strings in Java. I never really understood \"strings are immutable\". I understand it in theory, but I never came across it in practice.
Also
Java Strings are implemented with references, so you need to swap their references.
String s1 = "Hello";
String s2 = "World";
AtomicReference String1 = new AtomicReference(s1);
AtomicReference String2 = new AtomicReference(s2);
String1.set(String2.getAndSet(String1.get()));
System.out.println(String1 + " " + String2);
It will give you this output:
World Hello