What is difference between String & StringBuffer?

What is difference between String & StringBuffer?

String class provides the immutable character sequence whereas StringBuffer class objects are mutable characters sequence.

 The StringBuffer is faster than String when performing simple concatenations.

 Example:String s=new String(\"Hello\");s.toUpperCase();The String object s is still pointing to \"Hello\" instead of \"HELLO\". Here a new string \"HELLO\" is created but did not assigned to any reference variable.But:StringBuffer sb=new StringBuffer(\"Hello\");sb.toUpperCase();The StringBuffer object sb is now pointing to the \"HELLO\".This is the main difference that string object are immutable and string buffer objects are mutable.


By:Jalees
Date:2014-01-09 00:00:00

String class provides the immutable character sequence whereas StringBuffer class objects are mutable characters sequence.

 The StringBuffer is faster than String when performing simple concatenations.

 Example:String s=new String(\"Hello\");s.toUpperCase();The String object s is still pointing to \"Hello\" instead of \"HELLO\". Here a new string \"HELLO\" is created but did not assigned to any reference variable.But:StringBuffer sb=new StringBuffer(\"Hello\");sb.toUpperCase();The StringBuffer object sb is now pointing to the \"HELLO\".This is the main difference that string object are immutable and string buffer objects are mutable.


By:Sumit Tripathi
Date:2019-05-15 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher