Difference Between String, StringBuffer, and StringBuilder
1. Objects of String are immutable, and objects of StringBuffer and StringBuilder are mutable.
2. StringBuffer and StringBuilder are similar, but StringBuilder is faster and preferred over StringBuffer for the single threaded program.
If thread safety is needed, then StringBuffer is used
// Java program to demonstrate difference between String,
// StringBuilder and StringBuffer
class StringDiff
{
// Concatenates to String
public static void concat1(String s1)
{
s1 = s1 + "Debu";
}
// Concatenates to StringBuilder
public static void concat2(StringBuilder s2)
{
s2.append("Debu");
}
// Concatenates to StringBuffer
public static void concat3(StringBuffer s3)
{
s3.append("Debu");
}
public static void main(String[] args)
{
String s1 = "Paul";
concat1(s1); // s1 is not changed
System.out.println("String: " + s1);
StringBuilder s2 = new StringBuilder("Paul");
concat2(s2); // s2 is changed
System.out.println("StringBuilder: " + s2);
StringBuffer s3 = new StringBuffer("Paul");
concat3(s3); // s3 is changed
System.out.println("StringBuffer: " + s3);
}
}
Follow me on FaceBook: / techtalkdebu
Instagram: techtalk_debu
and LinkedIn : / debu-paul
if you like my video, please subscribe to my channel and share the video
Thanks & Regards,
Debu Paul
Sur cette page du site, vous pouvez voir la vidéo en ligne Core Java Interview Questions - Difference Between String, StringBuffer and StringBuilder class #4 durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur TechTalk Debu 06 June 2019, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 332 fois et il a aimé 5 téléspectateurs. Bon visionnage!