Wednesday, 9 April 2014

Difference between Concat And Append methods

class StringMethods
{
public static void main(String[] args)
{//immutable
String s1=new String("hello");
String s2=new String("java");
System.out.println(s1.concat(s2));
       System.out.println(s1.length());
  System.out.println(s2.length());
//mutable
StringBuffer sb1=new StringBuffer("hello");
StringBuffer sb2=new StringBuffer(" core");
System.out.println(sb1.append(sb2));
System.out.println(sb1.length());
System.out.println(sb2.length());
System.out.println("sb1 object capacity"+sb1.capacity());
System.out.println("sb2 object capacity"+sb2.capacity());
//sb.append("java");
//sb.insert(5,"---new---");
//sb.delete(2,3);
//System.out.println(sb.capacity());
//System.out.println(sb.reverse());
//System.out.println(sb.length());
}
}

D:\anvesh_java>javac StringMethods.java

D:\anvesh_java>java StringMethods
hellojava
5
4
hello core
10
5
sb1 object capacity21
sb2 object capacity21

1 comment: