What is string in Java why it's immutable
String in Java
Strings are a series of characters commonly used in Java language, which are considered objects in the Java. To create and manipulate strings, java will provide the String class. Any application program must contain a String as a kind of variable. To store different attributes like usernames and passwords, etc., string references are employed. String objects are immutable in Java. Immutable simply indicates that something cannot be altered or changed.
Creation of String?
We have two different ways to create String
- String Literal method
- New Keyword method
String Literal is simply created by using double quotes
String s="tutorialandexample";
Example:
String s1="tutorialandexample";
String s2="tutorialandexample";// It doesn't create a new instance
Only one object will be created in the preceding example.
New Keyword:
String s=new String("tutorialandexample");
Example of String
public class StringExample{
public static void main(String args[]){
String s1="tutorial";//creating string by Java string literal
char ch[]={'a','n','d'};
String s2=new String(ch);//converting char array to string
String s3=new String("example");//creating string with the help of new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}}
Output

The code above turns a char array into a String object. And the println() function is used to display the String objects s1, s2, and s3 on the console.
What is immutable string java?
An immutable object is one whose inner property will not be change after it has been completely constructed. This indicates that once an object is committed to a variable, we can't modify the reference or the internal state in any manner. Also, we have a separate post that goes into detail about immutable objects. Read the Immutable Objects in Java page for additional details.
Why String objects are immutable in java?
Because of security, synchronization and concurrency, caching, and class loading, the String is immutable in Java. Making a string final is done to remove its immutability and prevent others from extending it.
String pool
The most common data structure is the String. Caching and reusing String variables saves a lot of memory space because they all refer to the same object in the String pool. This is precisely the purpose of the string intern pool.
The Java String Pool is a memory region dedicated to storing Strings by the JVM. Because Strings are immutable in Java, the JVM keeps the amount of memory allocated to them to a minimum by maintaining just one copy of each literal String in the pool. This is referred to as interning:
String s1 = "tutorialandexample";
String s2 = "tutorialandexample";
assertThat(s1 == s2).isTrue();
Because of the String pool's existence in the above example, two separate variables point to the same String object from the pool, conserving a critical memory resource.
Security
Security parameters, such network connections, file opening, database connection URLs, users and passwords, and so on, are likewise specified as Strings in Java. Assume that if the string is malleable, any hacker might change the value to which it refers, jeopardizing the security.
Cache of Hashcode
As is widely known, string objects are extensively used in Java. Java applications such as HashMap, HashTable, and HashSet typically use the string hashcode implementation (). The hashcode() function returns the value of a specified string object. Strings are commonly used as HashMap keys, and an immutable string assures that their value will not be changed. It means that the hashcode doesn't have to be computed every time it's used.
Performance
As we have shown, the string constant pool increases memory storage and execution. String is the most often used data structure, which improves speed.
Classloader
Furthermore, class loading makes use of parameters from the String object. Because strings cannot be altered, Classloader loads the correct class.
Synchronized
Strings are thread-safe because they cannot be changed. Assume there are multiple threads running, but none of them can be adjusted because if a thread alters the value, the String pool will produce a new String instead of modifying the old one. Synchronization is thus no longer required.
Important points
- The untrustworthy caller method keeps the reference and can change the String between integrity checks. As a result, our query is vulnerable to SQL injections in this case. As a result, changing Strings may deteriorate security over time.
- Another thread may likewise view the String userName and change its value after the integrity check.
- In general, immutability helps us in this circumstance since it is easier to deal with sensitive code when values do not change because there are fewer interleavings of actions that might modify the value.