raul saini wrote:When we say arrays and Strings are immutable, what does it actually mean?
It means that they can't change their state after creation. Remember that Arrays and Strings are objects. A typical example would be 'String' (which is immutable) and 'StringBuffer or StringBuilder' (which are mutable).
String s1 = "Joe";
String s2 = "Blog";
s1 += s2;
System.out.println(s1); // The s1 reference variable still retains its value as "Joe". In other to effectively add the s2 to s1, another String object needs to be created
//in other to effectively add the two objects together.
String s3 = s1 + s2; //which now contains the value: "Joe Blogs".
This is different to the other more specialised String classes such as StringBuilder and StringBuffer - which are mutable. (Use StringBuffer for Thread-safe operations).
StringBuilder sb = nee StringBuilder("Joe");
sb.append("Blogs"); // At this stage, the value of sb is "Joe Blogs".