String vs StringBuilder vs StringBuffer in Java




// Java program to demonstrate difference between
// String, StringBuilder and StringBuffer

// Main class
class GFG {

// Method 1
// Concatenates to String
public static void concat1(String s1)
{
s1 = s1 + "forgeeks";
}

// Method 2
// Concatenates to StringBuilder
public static void concat2(StringBuilder s2)
{
s2.append("forgeeks");
}

// Method 3
// Concatenates to StringBuffer
public static void concat3(StringBuffer s3)
{
s3.append("forgeeks");
}

// Method 4
// Main driver method
public static void main(String[] args)
{
// Custom input string
// String 1
String s1 = "Geeks";

// Calling above defined method
concat1(s1);

// s1 is not changed
System.out.println("String: " + s1);

// String 1
StringBuilder s2 = new StringBuilder("Geeks");

// Calling above defined method
concat2(s2);

// s2 is changed
System.out.println("StringBuilder: " + s2);

// String 3
StringBuffer s3 = new StringBuffer("Geeks");

// Calling above defined method
concat3(s3);

// s3 is changed
System.out.println("StringBuffer: " + s3);
}
}

Output
String: Geeks
StringBuilder: Geeksforgeeks
StringBuffer: Geeksforgeeks



 

Comments

Popular posts from this blog

Java 8 : Find the number starts with 1 from a list of integers

Junit Mockito and Power Mockito

Find Loop in a Linked List