StringBuilder sb = new StringBuilder(); sb.append("Hello"); sb.append(" "); sb.append("world"); String str = sb.toString(); System.out.println(str);
StringBuilder sb = new StringBuilder(); for(int i=1; i<=10; i++){ sb.append(i); sb.append(" "); } String str = sb.toString(); System.out.println(str);In this example, a StringBuilder object is created and then a for loop is used to append the numbers 1 through 10 to it along with a space character. The toString() method is then called on the StringBuilder object to return the final string "1 2 3 4 5 6 7 8 9 10". The package library for this example is also java.lang.