In Java, all strings are immutable(Can't change). When you are trying to modify a String, what you are really doing is creating a new one.
Following ways we can create the string object
Using String literal
String str="java";
Using new keyword
String str = new String("java");
Using character array
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String helloString = new String(helloArray);
coming to String immutability, simply means unmodifiable or unchangeable
Let's take one example
I'm initializing the value to the String literal s
String s="kumar";
Below I'm going to display the decimal representation of the location address using hashcode()
System.out.println(s.hashCode());
Simply printing the value of a String s
System.out.println("value "+s);
Okay, this time I'm inittializing value "kumar" to s1
String s1="kumar"; // what you think is this line, takes new location in the memory ???
Okay let's check by displaying hashcode of the s1 object which we created
System.out.println(s1.hashCode());
okay, let's check below code
String s2=new String("Kumar");
System.out.println(s2.hashCode());
Okay, check this below code at last
String s3=new String("KUMAR");
System.out.println(s3.hashCode());
YES, if you see Strings 's' and 's1' having the same hashcode because the value hold by 's' & 's1' are same that is 'kumar'
Let's consider String 's2' and 's3' these two Strings hashcode appears to be different in the sense, they both stored in a different location because you see their values are different.
since s and s1 hashcode is same because those values are same and storing in the same location.
Example 1:
Try below code and analyze line by line
public class StringImmutable {
public static void main(String[] args) {
String s="java";
System.out.println(s.hashCode());
String s1="javA";
System.out.println(s1.hashCode());
String s2=new String("Java");
System.out.println(s2.hashCode());
String s3=new String("JAVA");
System.out.println(s3.hashCode());
}
}
Example 2: Try below code and analyze line by line
public class StringImmutable {
public static void main(String[] args) {
String s="java";
s.concat(" programming");
System.out.println("value of s "+s);
System.out.println(" hashcode of s "+s.hashCode());
String s1="java";
String s2=s.concat(" programming");
System.out.println("value of s1 "+s1);
System.out.println(" hashcode of s1 "+s1.hashCode());
System.out.println("value of s2 "+s2);
System.out.println(" hashcode of s2 "+s2.hashCode());
}
}
Okay, Let's look what is the difference between mutable and immutable.
mutable(it change) vs. immutable (it can't change)
public class StringMutableANDimmutable {
public static void main(String[] args) {
String s="java";
s.concat(" programming");
System.out.println("value of s == "+s);
System.out.println(" hashcode of s == "+s.hashCode()+"\n\n");
StringBuffer s1= new StringBuffer("java");
s1.append(" programming");
System.out.println("value of s1 == "+s1);
System.out.println(" hashcode of s1 == "+s1.hashCode());
}
}
Any further questions?? please write on...
StringBuffer
object, String is immuatable object right – RaghuStringBuffer
Object is not mutable String. A String once created is always immutable. – TheLostMindStringBuffer
ismutable String
, I am tellingStringBuffer
ismutable Object
. Am I right now? – Raghu