String, String Buffer and String Builder:
package DateBasics;
public class StringBasics {
public
static void main(String[] args) {
//
String : collection of characters . must
be enclosed in dbl quotes
//
ex: "Ram", "abc123",
"12.45(*"
// created in 2 ways
//1.
created by using literal (=) directly storing the value using = operator
//
Declate String variable "Ram";
String
s = "Ram";
System.out.println("s="+s);//
Ram
//
2 .new operator
String
s2 = new String("Sita"); //1 PC
System.out.println("s2="+s2);//
Sita
//
String class objects are immmutable
// mutable - changeable
// immutable -
-- not changeable
//
once we create obj for String class,
whose content cannot be modified in the same obj.
// if
we try to change content of String obj, it always creates new obj
s=
s.concat("Raju");
// "Ram" Raju
//
s= RamRaju
System.out.println("s="+s);
//
String objects get created in 2 places
//
1. String constant pool-
//
2. heap area
String
s3 ="Ram"; // String constant
pool-
String
s4 = new String("Ramu");
// heap area
System.out.println("s4="+
s4); // Ramu
//
when content is fixed, we will go for String class
}
}
FAQ Diff b/w == and Equals ()?
10 == 20 to compare 2 numbers --> false
10 == 10 true
s1.Equals(s2);
"Ram" "Ram" --> true
"Ram" "Sita"--> false
"Ram" == "Ram" --> ???
package DateBasics;
public class StringBasics2 {
public
static void main(String[] args) {
String
s1 = "ram";
String
s2 = "ram";
String
s3 = "sita";
System.out.println(s1.equals(s2));// to compare content of String or val of String
// "ram" "ram"
//
System.out.println(s1.equals(s3));
// ram sita
//
System.out.println(10==10);//
true
System.out.println(10==20);//
false
System.out.println(s1==s2);//
true
// 102 102
// == to
compare the reference addresses of s1 and s2 but not String content
}
}
Ex2:
package DateBasics;
public class Diff2 {
public
static void main(String[] args) {
String
s1 = "ram";// -- string const
pool area addd 101
String s2 = new String("ram");// creates
new obj - in heap area -- 102
// new address of s2 //
102
String
s3 = "sita";
//
to compare numbers -
if(s1==s2)
{// wont compare content or val "ram" "ram" if we use ==
// compare reference address of s1 and S2
// 101 ==
102 // false
System.out.println("first
part");
}
else
{
System.out.println("
== to compare refer add of s1 and
s2 but not content of String");
}
}
}
o/p:
== to
compare refer add of s1 and s2 but not
content of String
Ex3:
package DateBasics;
public class Diff2 {
public
static void main(String[] args) {
String
s1 = "ram";// -- string const
pool area -addd 101
String
s4 = "ram";// it will not
create obj and will not assign new add
// first it checks in Strig Cont pool area - if
same content is there, it will point to the same
// store addr of content -- 101
//
to compare numbers -
if(s1==s4)
{//
101 == 101 // tue
System.out.println("Address
of s1 and s4 are equal");
}
else
{
System.out.println("
== to compare refer add of s1 and
s2 but not content of String");
}
}
}
o/p:
Address of s1 and s4 are equal
Stringbuffer :
·
StringBuffer is a predefined class in Java
·
part of the java.lang package.
·
StringBuffer also can be used to store String
values
//
String class objects - immutable
// not changeable -once we create obj for String
Class
·
String buffer class objects - Mutable
// once we create object for
StringBuffer class, whose content can be modified/ can be changed.
------------------------------------------
package DateBasics;
public class StringBufferBasics {
public
static void main(String[] args) {
//
String s = "Ram";
//
StringBuffer sb = "Ram";
//StringBuffer
sb = "ram";// invalid
//
Type mismatch: cannot convert from String to StringBuffer
//Create StringBufferclass obj using new operator and Store "Ram"
StringBuffer sb = new
StringBuffer("Ram");
System.out.println("sb="
+ sb);// sb= Ram
//
HW check all methods and display o/p
//
1.Length
//
Replace
//
CharAt()
//
indexof()
//
Lastindexof()
//sb.substring(0)
//
no concat() in StringBuffer Class
//sb.append
sb.append("Sita");
//
"ram" "Sita"
//
System.out.println("sb="+
sb);// sb=ramSita
//
reverse
//
//s1.reverse();//
Reverse() is not available in String -class
sb.reverse();// available in stringbuffer class
System.out.println("sb="+
sb);// sb=atiSmar
}
}
StringBuilder :
·
StringBuilder is a predefined class in Java,
part of the java.lang package.
·
Used to store and manipulate strings.
·
It is similar to StringBuffer but without synchronization.
·
StringBuilder: Mutable. The content of a
StringBuilder object can be modified without creating a new object.
·
StringBuilder is not synchronized, making it
faster than StringBuffer in scenarios where thread safety is not a concern.
package DateBasics;
public class
StringBuilderClassBasics {
public
static void main(String[] args) {
//
String -Immutable, Stringbuffer (Mutable
// StringBuilder sbld = "ram";
// Type mismatch: cannot convert from String to
StringBuilder
// is predefined class
//
can be used to string values
//
Are mutable -- once we create obj.,whose
content can be changed/modified the same obj
//
create
StringBuilder
sbld2 = new
StringBuilder("ram");
//
class in java
System.out.println("sbld
="+ sbld2);/// ram
//
HW Check all methods and display the o/p
// length
// char at
// indexof
//
last ondex
//
}
}
FAQ Difference Between String, StringBuffer, and StringBuilder Classes
1. String Objects are immutable. Once object is
created whose content cannot be modified in the same object.
If we try to change/ modify the
content, it always creates a new object and modifies the content.
Stringbuffer class objects are mutable, whose content can be
changed/modified in the same object only.
StringBuilder class objects are mutable,
whose content can be changed/modified in the same object only.
2 .Objects created :
String :
String s = "ram";
String s1 = new
String("sam");
Stringbuffer:
StringBuffer sb1 = new
Stringbuffer("Sita");
StringBuffer sb1 = "Raju";// invalid
StringBuilder :
StringBuilder sbld1= new
StringBuilder("Swamy");
StringBuilder sbld1= "Sowmya";//
invalid
3. String: objects created in
String constant pool area and Heap area
String s = "ram";
// create obj in String const pool area
String s1 = new
String("sam"); /// create
object in heap area
Stringbuffer: create obj in heap area
StringBuilder : create obj in heap area
4. String : class objects are Synchronised.
At a time it allows only one thread to operate on object
it increases waiting time
Thread safe -- we will get proper
results.
StringBuffer :
class objects are Synchronised.
At a time it allows only one thread to operate on object
it increases waiting time
thread safe -- we will get proper
results.
StringBuilder:
class objects are not synchronised .
At a time , it allows multiple thread to operate on String builder obj.
It does not increase waiting time - performance wise - it is fast
-- not synchronised - not a
thread safe-- it may give improper
results/ corrupt results
5. If the content is fixed, we
will go String class
If content is varying, we will go for String buffer Class
If content is varying, we will go for String Builder Class
6. Performance:
String : slower as it is synchronised
Sbuffer: moderate
in speed
String builder : faster than String , String buffer
7.String : concat ()
check - no reverse()
String buffer : no concat()
append()
reverse() check ?
Stringbuilder : no concat()
append()
|
Aspect |
String |
StringBuffer |
StringBuilder |
|
Immutability |
Immutable: Once created, the content cannot be modified. Any
modification creates a new object. |
Mutable: The content can be modified in the same object. |
Mutable: The content can be modified in the same object. |
|
Object Creation |
java<br>String s = "ram";<br>String s1 = new
String("sam");<br> |
java<br>StringBuffer sb1 = new
StringBuffer("Sita");<br>StringBuffer sb2 = "Raju";
// Invalid<br> |
java<br>StringBuilder sbld1 = new
StringBuilder("Swamy");<br>StringBuilder sbld2 =
"Sowmya"; // Invalid<br> |
|
Memory Allocation |
- String constants are stored in the String Constant Pool. |
- Objects are created in the Heap. |
- Objects are created in the Heap. |
|
Synchronization |
Synchronized: Ensures thread safety but can lead to performance
overhead due to waiting time. |
Synchronized: Ensures thread safety but can lead to performance
overhead due to waiting time. |
Not Synchronized: Multiple threads can operate on it simultaneously.
Faster but not thread-safe. |
|
Usage |
Use when content is fixed and immutable. |
Use when content changes frequently and thread safety is needed. |
Use when content changes frequently and thread safety is not a
concern. |
|
Performance |
Slower due to synchronization overhead. |
Moderate speed, synchronized operations. |
Faster than String and StringBuffer due to lack of synchronization
overhead. |
|
Methods |
- concat() |
- No concat() |
- No concat() |
FAQ : Difference b/w String Buffer and String Builder?
No comments:
Post a Comment