Loading

Pages

Tuesday, January 31, 2012

How To Concatenate Strings In Java

StringBuilder and StringBuffer classes can be used to concatenate strings. The difference between the two is that StringBuffer is thread-safe while
StringBuilder is not. In practice you will be using StringBuilder most of the
time.

public static void main(String[] args) {

StringBuilder builder=new StringBuilder();
builder.append("ONE");
builder.append(" TWO");
builder.append(" THREE");

System.out.println("Concated :"+builder.toString());

StringBuffer buffer=new StringBuffer();
buffer.append("FOUR");
buffer.append(" FIVE");
buffer.append(" SIX");

System.out.println("Concated2 :"+buffer.toString());

}