As a java developer or any programming developer , we should get some knowledge regarding String. Because it widely used object in nay language in our day to day implementations.
Lot of advantages(Need of String Pool, Caching Hashmap key with String and security) are there as strings are made as immutable.
Why String Immutable:
String pool : Java Run time environment uses the String pool memory to store the string literals. If we crate any string, first it will check whether that string is available in the pool or not. If string is available in pool, just it returns the reference of it and it won't create new one.
If string is not available, then it will create new string and return reference of it.
Let us consider below lines of code.
String s1 = "java";
String s2 = "java"; //It won't create new object as java is already created from above line. Just it returns reference of it. in this case s1==s2 will be true.
So if one client change the value "java" to "J2Se", it will affect for all other clients and which is not desirable. we can avoid these risks by making String as immutable.
And also String is made as final as no one can customize by extending or overriding String class.
Below scenarios make lot of details for why string Immutable or final:
1. Consider string pool without string immutability, based on above knowledge it is not possible because, in case of string pool if we modify one string liter value, that will affect for all references of it and which is not supposed to happened. Hence string is made as immutable.
2. As string is immutable and we can cache the hashcode, no need to calculate hashcode every time which makes very fast as hashmap uses String as key.
3. As we are using string in class loading mechanism, it is important that string is immutable. Suppose if String is mutable and if we try to load the class java.util.hashmap might have chance to load com.test.javaTest.
4. As string is immutable , we can easily share between threads in Multi Threading implementation. And we no need to go for synchronization String operations externally in multi thread implementation.
5. Suppose we have file access implementation ans if string is not immutable, one client can access the file by name and he might change the name of file either intentionally or accidentally, it will cause problems. Hence String made as immutable.
package com.sample.javase.testing;
public class StringImmutableTest {
public static void main(String[] args) {
String s ="java";
String s1 = "java";// not create new string just returns reference
System.out.println(s1 == s);//true
s = s.toUpperCase(); // It will create new string "JAVA" in pool memory as string is immutable.
System.out.println(s1 == s);//false as S reference changed in above line
System.out.println(s);// it will print JAVA
}
}
Lot of advantages(Need of String Pool, Caching Hashmap key with String and security) are there as strings are made as immutable.
Why String Immutable:
String pool : Java Run time environment uses the String pool memory to store the string literals. If we crate any string, first it will check whether that string is available in the pool or not. If string is available in pool, just it returns the reference of it and it won't create new one.
If string is not available, then it will create new string and return reference of it.
Let us consider below lines of code.
String s1 = "java";
String s2 = "java"; //It won't create new object as java is already created from above line. Just it returns reference of it. in this case s1==s2 will be true.

And also String is made as final as no one can customize by extending or overriding String class.
Below scenarios make lot of details for why string Immutable or final:
1. Consider string pool without string immutability, based on above knowledge it is not possible because, in case of string pool if we modify one string liter value, that will affect for all references of it and which is not supposed to happened. Hence string is made as immutable.
2. As string is immutable and we can cache the hashcode, no need to calculate hashcode every time which makes very fast as hashmap uses String as key.
3. As we are using string in class loading mechanism, it is important that string is immutable. Suppose if String is mutable and if we try to load the class java.util.hashmap might have chance to load com.test.javaTest.
4. As string is immutable , we can easily share between threads in Multi Threading implementation. And we no need to go for synchronization String operations externally in multi thread implementation.
5. Suppose we have file access implementation ans if string is not immutable, one client can access the file by name and he might change the name of file either intentionally or accidentally, it will cause problems. Hence String made as immutable.
package com.sample.javase.testing;
public class StringImmutableTest {
public static void main(String[] args) {
String s ="java";
String s1 = "java";// not create new string just returns reference
System.out.println(s1 == s);//true
s = s.toUpperCase(); // It will create new string "JAVA" in pool memory as string is immutable.
System.out.println(s1 == s);//false as S reference changed in above line
System.out.println(s);// it will print JAVA
}
}
No comments:
Post a Comment