The StringBuffer and StringBuilder classes are used when there is a necessity to
make a lot of modifications to Strings of characters.
StringBuffer class is used to create a mutable string object. It represents growable
and writable character sequence. As we know that String objects are immutable,
Note: so if we do a lot of changes with String objects, we will end up with a lot of memory leak.
Note: so if we do a lot of changes with String objects, we will end up with a lot of memory leak.
So StringBuffer class is used when we have to make lot
of modifications to our string.
Note: Term Thread Safety--> It means to stay safe from Thread because of many threads come it will become unsafe.
Append in String Buffer
package mypackage;
import java.util.Random;
public class test{
public static void main(String[] args)
{
StringBuffer
sb=new StringBuffer();
sb.append("test");
sb.append("hello");
System.out.println(sb);
}
}
Output:
testhello
StringBuffer with parameter
package mypackage;
import java.util.Random;
public class test{
public static void main(String[] args)
{
StringBuffer
sb=new StringBuffer("test");//pass value in
parameter
sb.append("hello");
System.out.println(sb);
}
}
String Buffer insert function
package mypackage;
public class test{
public static void main(String[] args)
{
StringBuffer
sb=new StringBuffer("test");
sb.insert(3,
"hello");//3 is the position
on which you want to insert
System.out.println(sb);
}
}
Output:
teshellot
Delete character from String Buffer
package mypackage;
public class test{
public static void main(String[] args)
{
StringBuffer
sb=new StringBuffer("test ");
sb.deleteCharAt(1);
System.out.println(sb);
}
}
Output:
tst
Index of String
package mypackage;
public class test{
public static void main(String[] args)
{
StringBuffer
sb=new StringBuffer("test ");
int number=sb.indexOf("e");
System.out.println(number);
}
}
Output:
1
To replace in String Buffer
package mypackage;
public class test{
public static void main(String[] args)
{
StringBuffer
sb=new StringBuffer("test ");
sb.replace(1,
2, "hello");
System.out.println(sb);
}
}
Output:
thellost
String concat does not update the string
package mypackage;
public class test{
public static void main(String[] args)
{
String
name="neeraj";
name.concat("hello");//it will not make
name=neerajhello
System.out.println(name+"test");//we will have to
concat in this way using + operator
String
hello=name+"test";
System.out.println(hello);
}
}
Output:
neerajtest
neerajtest
String Builder
Difference between
StringBuffer and StringBuilder
StringBuffer StringBuilder
1) StringBuffer
is synchronized i.e. thread safe. It means two threads can't call the methods
of StringBuffer simultaneously.
StringBuilder
is non-synchronized i.e. not thread safe. It means two threads can call the
methods of StringBuilder simultaneously.
2) StringBuffer
is less efficient than StringBuilder.
StringBuilder
is more efficient than StringBuffer.
By thread safe in the general terms means that even if you run multiple threads simultaneously on the same resources even in such a condition those threads won’t get stuck in a deadlock. It is the state in which sometimes all the threads can come to a stand still because of an endless loop of not getting the required resource.
The basic fix which java uses is the keyword “synchronized” only methods or block of codes can be marked as synchronized. This makes sure that only a single thread is working on a particular object helping in avoiding the situation of a deadlock.
----------------------------------------------------------------------------------
String StringBuffer StringBuilder
----------------------------------------------------------------------------------
Storage Area | Constant String Pool Heap Heap
Modifiable | No (immutable) Yes( mutable ) Yes( mutable )
Thread Safe | Yes Yes No
Performance | Fast Very slow Fast
-----------------------------------------------------------------------------------
No comments:
Post a Comment