We can test this concept by creating an instance of String and try to modify it and then test if the original instance is modified or not.
Sounds simple, let's get to it:
public class StringTest {
public static void main(String[] args) {
foo();
}
public static void foo() {
String a = "Hello";
String b = a.concat(" World!");
System.out.println("a : " + a);
System.out.println("b : " + b);
}
}
Output of StringTest will be:
a : Hello
b : Hello World
As you can see even after concat () method value of a did not change. It just returned a new String instance.

Doesn't this illustrate only the behaviour of the concat() method?
ReplyDelete