Immutable string

Immutable string

String pool is feasible solely as a result of String is immutable in Java.This way Java Runtime environment saves heap memory. String variables will be referenced to an equivalent String variable within the pool. String interning wouldn't be possible as a result of if any variable would have modified the worth, it might be mirrored within the different variables too.

The advantage of immutable String class are :

  • It can save memory, by string Constant Pool.
  • Data security as it cannot be breached and altered.
  • Safe in Threading process.

If String isn't immutable then it might cause a severe security threat to the applying Object; for instance, information username, Password are passed as String to urge information affiliation and in socket programming host and port details and could alter the string object. Since String immutable, its worth can’t be modified otherwise any hacker might amendment the documented worth to cause security problems within the application.

Since String are immutable, it's safe for multi threading process and task . A single String instance can be shared across completely different threads. This avoids the process of synchronization for thread safety. Strings are implicitly thread-safe.

Strings are utilized in java category loader and immutability of String provides security that correct class is obtaining loaded by Class Loader.  For instance, consider associate degree instance wherever you're making an attempt to load SQL data from java.sql.Connection category however the documented can be modified to hacked.Connection category which can be altered or unauthenticated access to data and can lead to data breach .

As String is immutable, when the string is created or intantiated it generates its hashcode and so it saves computation of the string object again.


Com.knowledge2life public class Main{ public static void main(String args[]){ String s="Knowledge2"; s=s.concat("life"); System.out.println(s); } }