DEV Community

Rittwick Bhabak
Rittwick Bhabak

Posted on

3. String in JAVA

A string is a sequences of characters. In the case of the char, char contains a single character only.
A String can contain a sequence of characters.
Exmaple:
String name = "Rittwick Bhabak"

3 Strings in Java are Immutable

String name = "Rittwick";
String name = name + "Bhabak";
Enter fullscreen mode Exit fullscreen mode

In the above code, name doesn't get appended the value "Bhabak" instead a new String is created which consists of the previous value of name plus "Bhabak".
The net result is same, name has the right values, however, a new String got created and the old one got discarded.
So, String is immutable in Java.
There are a class in Java called StringBuffer which can be changed.

Top comments (1)

Collapse
 
rindraraininoro profile image
Raininoro Rindra • Edited

Actually, in most cases StringBuilder is more suitable as we rarely need to handle strings in a thread-safe manner.
Many blog articles deal with that topic.