04/01/2020

String in java | StringBuffer in java | and Difference :Studywow


Q1. What is a StringBuffer in java? How does it differ from a string? Give three ways to create a string object?

Ans. StringBuffer in Java: StringBuffer is a peer class of String that provides much of the functionality of strings. String represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences.

StringBuffer may have characters and substrings inserted in the middle or appended to the end. It will automatically grow to make room for such additions and often has more characters preallocated than are actually needed, to allow room for growth.

Differences between String and StringBuffer in java

There are many differences between String and StringBuffer in java. A list of difference between String and StringBuffer are given ahead:
String Type: String class is immutable.
StringBuffer Type: StringBuffer class is mutable.

String Speed: String is slow and consumes more memory when we concat too many strings because every time it creates new instance.
StringBuffer Speed: StringBuffer is fast and consumes less  memory when we concat strings.

String Overriding: String class overrides the equals() method of Object class. So we can compare the contents of two strings by equals() method.
StringBuffer Overriding: StringBuffer class doesn't override the equals() method of object class.

Ways to Create a String Object in java

There are various ways to create a string object in Java:
(a) Using String Literal: String objects can be created using string literal.
String Str = "Hello !";

(b) Using New Keyword: This is the common way to create a string object in Java.
String Str 1 = new string ("Hello !");

(c) Using Character Array: We can convert character array into string.
char ch[] = { 'H', 'e', 'l', 'l', 'o', ''!", };
String str 1 = new string (ch);

Q2. Discuss the difference between "String" and “String Buffer" class as defined in Java ?

Ans. Differences between String and StringBuffer in java are:

'String' class type: This class is immutable.
'StringBuffer' class type: This class is mutable.

'String' memory: String is slow and consumes more memory.
'StringBuffer' memory: String Buffer is fast and consumes less memory.

'String' overriding: This class has override equals() from object class.
'StringBuffer' overriding: This class has not override equals().

'String' Pool: It supports string pool concepts.
'StringBuffer' Pool: It does not support string pool concept.

'String' performance: Low, due to unnecessary object created.
'StringBuffer' performance: High performance.

'String' Synchronisation: It is not synchronised.
'StringBuffer' Synchronisation: It does synchronised.

'String' Example: public class StringExample{
public static void main(String args[]) {
String s1 = "java";  //creating string by java string literal
Char ch[] = {'s','t', 'r','i','n','g, 's'};
String s2 = new String
(ch);  //converting char array to string
String s3 = new String ("example");
//creating java string by new keyword
System.out.println(sl);
System.out.println (s2);
System.out.println(53);
} }

'StringBuffer' Example: public class Test {
public static void main (String args[]) {
StringBuffer sBuffer = new StringBuffer ("test");
sBuffer. append ("StringBuffer");
System.out.println (sBuffer);
} }




Join us on Facebook and Twitter  to get the latest study material. You can also ask us any questions.
Facebook = @ studywow
(click on it or search "studywow" on facebook)
Twitter = @ studywow
(click on it or search "studywow" on Twitter)
Email= studywow.com@gmail.com
Send us your query anytime!