Tuesday, 23 May 2017

What is the importance of immutable property for String class



In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.

What is String ?

A Java string is therefore a series of characters collected together, like the word "Java", or the phrase "practice makes perfect".
Create a string in the code by writing its chars out between double quotes. String str = "Java";
This picture shows the string object in memory, made up of the individual chars J a v a.
Java String is class that java developers have written and put into java API .  It is present in java.lang package.

How String is immutable class ?
 It is an immutable class, means once we java course in bangalore created its object we can not modify that, if we will try to modify new object will be created.

Strings are considered immutable in Java because when we assign new value to a string then the new value is not allocated at the same address space in memory where the older value was stored.

Instead, the old value is discarded and the space it took in the memory is cleaned up by the JVM's garbage collector and new space is allocated in the memory where new value of the string is stored.

What is the importance of Immutability ?

Performance: By making Strings immutable it is possible to cache them more aggressively

Security: Parameters are typically represented as string in network connections, database connection urls , usernames/passwords etc. If it were mutable, these parameters could be easily changed.

Synchronization and concurrency:  It make String immutable automatically makes them thread safe thereby solving the synchronization issues.

Caching: When compiler optimizes  String objects, it sees that if two objects have same value (a="test", and b="test") and thus you need only one string object (for both a and b, these two will point to the same object).

Class loading: String is used as arguments for class loading. If mutable, it could result in wrong class being loaded (because mutable objects change their state).

How can we get the properties of mutability ?

JDK provides two classes to support mutable strings: StringBuffer and StringBuilder.

String Buffer :

StringBuffer class is used to create a mutable string object. It represents growable and writable character sequence.
As we know that String objects are immutable, so if we do a lot of changes with String objects,  100% job support we will end up with a lot of memory leak.
 So StringBuffer class is used when we have to make lot of modifications to our string. It is also thread safe i.e multiple threads cannot access it simultaneously.

String Builder :

StringBuilder is identical to StringBuffer except for one important difference it is not synchronized, which means it is not thread safe. Its because StringBuilder methods are not synchronised.
In other words, if multiple threads are accessing a StringBuilder instance at the same time, its integrity cannot be guaranteed. However, for a single thread program (most commonly), doing away with the overhead of synchronization makes the StringBuilder faster.

Difference between string , string buffer and string builder:

- String is immutable whereas StringBuffer and StringBuider are mutable          classes.
- StringBuffer is thread safe and synchronized whereas StringBuilder is not,           thats why StringBuilder is more faster than StringBuffer.
- String concat ,+ operator internally uses StringBuffer or StringBuilder class.
- For String manipulations in non-multi threaded environment, we should use HYPERLINK "http://www.infocampus.co.in/java-training-bangalore.html"    StringBuilder else use StringBuffer class.
This is the short description about the String class. To get the more knowledge and to get the job as java developer easily join Infocampus Software Training Institute .

Infocampus is best institute for the java course in bangalore . It provides 100% job support with the live project.
To book the seat for free demo class call at : 9738001024 or to get the more information on java course enquire at : http://www.infocampus.co.in/java-training-bangalore.html

No comments:

Post a Comment