0% found this document useful (0 votes)
1 views22 pages

Strings

The document provides an overview of Strings in Java, explaining their immutability, creation methods, and differences from StringBuffer. It highlights the importance of the String Constant Pool for memory efficiency and discusses key methods and constructors for both String and StringBuffer classes. Additionally, it covers the use cases for String, StringBuffer, and StringBuilder based on content mutability and thread safety.

Uploaded by

stunning sathvik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views22 pages

Strings

The document provides an overview of Strings in Java, explaining their immutability, creation methods, and differences from StringBuffer. It highlights the importance of the String Constant Pool for memory efficiency and discusses key methods and constructors for both String and StringBuffer classes. Additionally, it covers the use cases for String, StringBuffer, and StringBuilder based on content mutability and thread safety.

Uploaded by

stunning sathvik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

STRINGS

NAME : VUPPUSETTI SAI SATHVIK


MAILID : [email protected]
String:
In Java, String is an object that represents a sequence
of characters.
Generally, there are two ways to create the string
object in java. They are:
 By string literal
 By new keyword
String literal: String literal is created by using
double quotes.
String s = "Hello";
New keyword: It is the second way of creating string
object in java. It is just like creating an object of any
class. We can declare it as:
String s = new String("Hello");
• Difference between String and
String Buffer
1. Mutability
String String Buffer
 Public class Rajesh{ Ø Public class Rajesh{
Main(){ Main(){
String S = new StringBuffer S = new
String("Sai");
S.concat("Sathvik"); StringBuffer("Sai");

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

Important methods of String Class:


charAt() Returns the character at the specified index (position) char
concat() Appends a string to the end of another string String
equals() Compares two strings. Returns true if the strings are equal, boolean
and false if not
equalsIgnoreCase() Compares two strings, ignoring case considerations boolean
indexOf() Returns the position of the first found occurrence of int
specified characters in a string
isEmpty() Checks whether a string is empty or not boolean
lastIndexOf() Returns the position of the last found occurrence of int
specified characters in a string
length() Returns the length of a specified string int
replace() Searches a string for a specified value, and returns a new String
string where the specified values are replaced
substring() Returns a new string which is the substring of a specified String
string
trim() Removes white spaces from both ends of a string String
toLowerCase() Converts a string to lower case letters String
toUpperCase() Converts a string to upper case letters String
Important conclusions of String Immutability
String s1 = "chetan";
String s2 = s1.toString();
String s3 = s1.toLowerCase();
String s4 = s1.toUpperCase();
Sop(s1==s2); //true
Sop(s1==s3); //true
Sop(s1==s4); //false

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.

You might also like