Java — Difference between String VS StringBuffer | Code Factory

Index Page : Link

Donate : Link

  • String objects are immutable, not changed
  • StringBuffer objects are mutable, changed
  • After creating String object, it can not be changed but in StringBuffer it can be easily changed.
package com.example.string;public class Test {
public static void main(String... args) {
String s = new String("Code");
s.concat("Factory");
System.out.println(s); // Code

String s1 = s.concat("Factory");
System.out.println(s1); // CodeFactory
}
}
package com.example.string;public class Test {
public static void main(String... args) {
StringBuffer sb = new StringBuffer("Code");
sb.append("Factory");
System.out.println(sb); // CodeFactory
}
}