Strings
Strings
Sysout(S); S.concat("Sathvik");
} Sysout(S);
} }
}
Output : Sai Output : SAI Sathvik
Ø Once we create string object we cannot change its
content that’s why String is always immutable
Ø String is immutable but String Buffer is mutable
Ø Equals()
Ø Public class Rajesh{ Ø Public class Rajesh{
Main(){ Main(){
String S1 = new StringBuffer S1 = new
String("Sai"); StringBuffer("Sai");
String S2 = new StringBuffer S2 =
String("Sai"); new StringBuffer("Sai");
Sysout(S1==s2); Sysout(S1==s2);
Sysout(S1.equals(s2)); Sysout(S1.equals(s2));
} }
} }
Output : false Output : false
true false
"==" always meant for reference comparison
String in String class equals() meant for content
comparison whereas in StringBuffer it meant
reference comparison
String Object creation : Heap and
String Constant pool (SCP)
String s = new String("Sai"); String s = "Sai"
heap scp
Maintained by JVM
S= Sai Sai
whenever we are using new operator, compulsory new
object will be created in the heap area that's why
there may be a chance of existing 2 objects with same
content in heap area.
There is no chance of existing 2 objects with same
content in string constant pool area, means if the
content is same then the object is reused multiple
times so that internal memory will be saved and
utilization will be improved.
String S = new String("sai "); heap scp
S.concat("Sathvik");
Sai Sai
S = S.concat("Rajesh"); GC
Sai Sathvi
GC sathvik k
S Sai Rajesh
Rajesh
Advantages or Importance of SCP:
objects can be reused multiple times instead of
creating the new object
Memory will be saved performance will be improved
Disadvantage:
One reference, if any person trying to change then all
the remaining references will be effected to prevent
that immutability concept is introduced.
1. Why SCP concept is available only for String
object but not for StringBuffer
A)-Ex. If one person is regular customer to some shop
then special privileges given to him but if this
person go only once a year or month then
special privileges not given to him. Thats why SCP
is available to String but not for StringBuffer.
2. Why String objects are immutable where as
StringBuffer objects are mutable?
For String object SCP conecpt is there so reusability
of same object is there. So if any one object’s value
change then all the references are affected so
immutability is required.
In StringBuffer for every reference different object is
there because reusing same object is not there, If
we change any StringBuffer object it will not affect
other objects because there is no SCP
3. In addition to String objects any other
objects are immutable in Java?
All wrapper class objects
Byte, Short, Integer, Long, Float, Double, Character,
Boolean
Important constructors in Java
String s = new String();
To create an empty String
Ø String s = new String(String Literal);
For the given String literal to create an equivalent
String
Ø String s = new String(StringBuffer);
For the given StringBuffer to create an equivalent
String
Ø String s = new String(StringBuilder);
For the given StringBuilder to create an equivalent
String
Ø String s = new String(Char[]ch);
For the given Char to create an equivalent String
Output : Manohar
heap scp
S1=
S4 = CHETAN S2= chetan
S3=
Creation of our own immutable class
T1=
10 T2= 100
T3 =
Final vs Immutability
Output: UtpalPhatak
Even the reference variable is final, still in this string
buffer object we are allowed to change the content
means that by declaring reference as "final", we are
not going to get immutability nature
Means we cannot reassign the ref variable to new
object
Needs of StringBuffer
If the content is not fixed and keep on changing,
String concept is not recommended to use
StringBuffer:
All the required changes will be perform in existing
object only
StringBuffer Class Constructor:
StringBuffer sb=New StringBuffer();
Default capacity =>16
For extra capacity
New capacity=(cc+1)*2=34
For more capacity=(34+1)*2=70
1) public class refere {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity()); //16 default
System.out.println(sb.length());//0
sb.append("abcdefghijklmnopqrst");
System.out.println(sb.capacity());//34
}
}
2) public class refere {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer(100);
System.out.println(sb.capacity()); //100
}
3) StringBuffer sb=new StringBuffer(String s);
For the given equivalent StringBuffer object will be
created.
4) StringBuffer sb=new StringBuffer(“Sathvik”);
Capacity=s.length(); 16+7
Sysout(capacity) /23
Important method of StringBuffer class:
1. append() : The append() method concatenates the
given argument with this String.
2. Length //charAt//setCharAt//Capacity
3. Insert(): The insert() method the given String with
this at the give position.
4. Replace (): The replace() method replace the given
string from the specified beginIndext and endIndex.
5. Delete charAt(): Delete at particular index
6. Reverse(): is used to reverse the string.
7. Set length(): It is used to
specify startIndex and endIndex.
8. Ensure capacity: It is used to ensure the capacity at
least equal to the given minimum.
9. Trim to size(): for trimming
10. Delete(): It is used to delete the string from specified
startIndex and endIndex.
Method Chaining:
Method chaining is a common syntax for invoking
multiple method calls in object-oriented programming
languages. Each method returns an object, allowing
the calls to be chained together in a single statement
without requiring variables to store the intermediate
results.
public class chaining {
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
sb.append("Sai").append("Sathvik").replace(2, 4,
"hhh").reverse();
System.out.println(sb); //kivhtahhhaS
}
String: if the content is fixed if it doesn’t change frequently
highly recommend to go for String concept and hence it is
also thread safe.
StringBuffer: if the content is not fixed and keep on changing
highly recommend to go for StringBuffer but thread safety
required , means only one thread required at a time.
StringBuilder: content is not fixed and keep on changing if
we don’t want thread safety ,multiple thread are allowed to
operate at a time on object then recommend to go for
StringBuilder.