String,String Buffer and String Builder in JAVA | Basic IT Topic

String,String Buffer and String Builder in JAVA

String,String Buffer and String Builder in JAVA

String, String Buffer and String Builder in JAVA:-

String:-

String is predefined final class present in the default package.
As string is a final class it cannot be inherited.
String is purely immutable(contents cannot be changed) in nature that contents of the string cannot be changed once it assigns.

How string is immutable?

PROGRAM:-
class Test
{
public static void main(String[] args) 
{
String s=new String("Hello");
System.out.println(s);
System.out.println(s.concat("welcome"));
System.out.println(s);
s=new String("Java");
System.out.println(s);
s=s.concat(" is simple");
System.out.println(s);
}
}

O/P:-
Hello
Hellowelcome
Hello
Java
Java is simple

*String is not a character array like C or C++ in Java.
To create a manipulate for string java provide some predefined constructor and method

== ->doen’t check the contains only check the hash code.
equals() ->check the contains .

Note:-
String can be constructed by 2 different ways –>by literal –>by new.
If the string created with the help of string literal, it always allocates memory from string pool
String pool is a special memory which cannot store any duplicate string.

The following are the predefined method which always check the contains.
1.equals()
2.equalsIgnoreCase()
3.compareTo();
4.compareToIgnoreCase()

PROGRAM:-
class Equals
{
public static void main(String[] args) 
{
String s1="Hello"; //created with literal
String s2="Hello"; //created with literal
String s3=new String("Hello"); //by new
String s4=new String("Hello"); //by new
System.out.println(s1+"\t"+s2+"\t"+s3+"\t"+s4+"\t");

System.out.println(s1==s2); //check hashcode
System.out.println(s2==s3); //check hashcode
System.out.println(s3==s4); //check hashcode

System.out.println(s1.equals(s2)); //check contains
System.out.println(s2.equals(s3)); //check contains
System.out.println(s3.equals(s4)); //check contains
}
}

O/P:-
Hello Hello Hello Hello 
true
false
false
true
true
true

toCharArray()=> It is predefined method present in string class which is responsible to convert string to character array.
To convert a character array to string we have to pass the array name within the constructor of the string class.

PROGRAM:-
import java.util.*;
public class chararray 
{
public static void main(String[] args) 
{
String s1=new String("Hello");
System.out.println(s1);
char arr[]=s1.toCharArray(); //logic to convert string to char array
System.out.println(arr); //char arr

s1=new String(arr); //logic to convert chararray to string 
System.out.println(s1);
System.out.println(Arrays.toString(arr));
}
}
O/P:-
Hello
Hello
Hello
[H, e, l, l, o]

String Buffer:-

It is a predefined final class present in java.lang package.
StringBuffer is very similar to string but the difference is StringBuffer is faster and mutable

How StringBuffer is mutable?

PROGRAM:-
public class StringBuffer1 
{
public static void main(String[] args) 
{
StringBuffer sb1=new StringBuffer("Hello");
System.out.println(sb1);
System.out.println(sb1.append(" Welcome"));
System.out.println(sb1);
}
}
O/P:-
Hello
Hello Welcome
Hello Welcome

How String Buffer is faster than String?

PROGRAM:-
public class StringBuffer2 
{
public static void main(String[] args) 
{
String s1=new String("Hello");
StringBuffer sb1=new StringBuffer("Hello");

long start,stop;

start=System.currentTimeMillis();
for(int i=0;i<20000;i++)
{
sb1.append(" "+i);
}
stop=System.currentTimeMillis();
System.out.println("time taken by string buffer is"+(stop-start));

start=System.currentTimeMillis();
for(int i=0;i<20000;i++)
{
s1=s1.concat(" "+i);
}
stop=System.currentTimeMillis();
System.out.println("time taken by string is"+(stop-start));

}

}
O/P:-
time taken by string buffer is15
time taken by string is718

How StringBuffer is convert to string & vice versa?

PROGRAM:-
public class StringBuffer3 
{
public static void main(String[] args) 
{
String s1=new String("madam");
System.out.println("the original String is:"+s1);
StringBuffer sb1=new StringBuffer(s1); //convert string to stringBuffer
//sb1.reverse();
System.out.println("After Reverse:"+sb1);
String s2=new String(sb1);//string buffer converts to string

if(s1.equals(s2))

System.out.println("Yes Palindrome");
else
System.out.println("Not palindrome");
}
}

O/P:-
the original String is:madam
After Reverse:madam
Yes Palindrome

How to create an immutable class?

PROGRAM:-
public final class ImmutableClass
{
private final String name;
ImmutableClass(String name)
{
this.name=name;
}
public String toString()
{
return name;
}
public String concat(String s)
{
return name+s;
}
public static void main(String[] args) {
ImmutableClass i=new ImmutableClass("JAVA");
System.out.println(i);
System.out.println(i.concat("is"));
System.out.println(i);
i=new ImmutableClass("Simple");
System.out.println(i);
}
}

O/P:-
JAVA
JAVAis
JAVA
Simple
EXAMPLE OF STRINGBUFFER:-
PROGRAM:-
import java.util.Arrays;
public class StringBuffer4 
{
public static void main(String[] args) 
{
StringBuffer sb1=new StringBuffer("VACD");
char arr[]=new String(sb1).toCharArray();//CONVERTS STRINGBUFFER TO STRING AND STRING TO CHAR ARRAY
Arrays.sort(arr);
// System.out.println("after sort the array:"+arr);
System.out.println("after sort the array"+Arrays.toString(arr));

System.out.println("--------------------");

System.out.println(sb1.insert(0, "hi")); // insert at 0 position
System.out.println(sb1.codePointAt(0)); //Ascii value
System.out.println(sb1.replace(0, 3, "IND"));// Replace at position
System.out.println(sb1.indexOf("IND")); // number of position
System.out.println(sb1.lastIndexOf("IND")); //position of last Index
System.out.println(sb1.length());//length of the string
System.out.println(sb1.delete(1, 2)); //delete the string
System.out.println(sb1.substring(2));//also it means delete
System.out.println(sb1.reverse());


String s1=new String("java is simple");
String s2[]=s1.split(" ");
int count=0;
for(String x:s2)
{
if(!x.isEmpty())
{
System.out.println(x+"\t"+x.length());
count++;
}
}
System.out.println("No of Words:"+count);
}
}

O/P:-
after sort the array[A, C, D, V]
--------------------
hiVACD
104
INDACD
0
0
6
IDACD
ACD
DCADI
java 4
is 2
simple 6
No of Words:3

StringBuilder:-

StringBuilder is most probably the same as String and StringBuffer.
The only difference between StringBuffer and StringBuilder is StringBuffer cannot be used in multithreading whereas StringBuilder class is threadsafe. So it can be used in multithreading.
StringBuilder is synchronized whereas String and StringBuffer both are not synchronized.

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community