Java: Strings in Java

By Xah Lee. Date: . Last updated: .

Java provides two data types for strings. “String” and “StringBuffer”. Use the “StringBuffer” if you need your string to be modifiable. Variables declared as “String” type cannot be changed. (there's also “StringBuilder”, but you don't have to worry about it now.)

public class S1 {
     public static void main(String[] args) {
         String s1 = "some str";
         StringBuffer s2 = new StringBuffer(9);
         s2.replace(0,0,s1);
         System.out.println(s2.toString());
     }
}

The replace() is a method of “StringBuffer” classs. It replaces parts (or all) of the “StringBuffer” object by a “String” object.

Reference