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

String

The document provides an overview of the String class in Java, including string literals, characteristics, constructors, and methods. It also discusses the StringBuffer class, its methods, and the differences between StringBuffer and StringBuilder. Additionally, it highlights the immutability of strings and the performance implications of using String versus StringBuffer.

Uploaded by

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

String

The document provides an overview of the String class in Java, including string literals, characteristics, constructors, and methods. It also discusses the StringBuffer class, its methods, and the differences between StringBuffer and StringBuilder. Additionally, it highlights the immutability of strings and the performance implications of using String versus StringBuffer.

Uploaded by

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

ALPHA

CORE JAVA - SESSION 30


INDEX
● String literal
● String class
CORE JAVA ● String pool
SESSION 30 ● Characteristics of String class
● Constructor
● Methods
STRING

STRING :
String is a literal (data). It is a group of character that is enclosed within the
double quote “ ”.
● It is a Non primitive data.
● In java we can store a string by creating instance of the following classes.
▪ java.lang.String
▪ java.lang.StringBuffer
▪ java.lang.StringBuilder
● In java, whenever we create a string compiler implicitly create an instance for
java.lang.string in string pool area / string constant pool (scp).
STRING LITERAL

STRING LITERAL:
Anything enclosed within the double quote “ ” in java is considered as String
literal.
Characteristics of String Literal :
● When a String literals is used in a java program, an instance of
java.lang.String class is created inside a String pool.
● For the given String literal, If the instance of a String is already present, then
new instance is not created instead the reference of a existing instance is
given.
EXAMPLE:1
Class Demo
{
public static void main(String[] args) CONSTANT POOL / STRING
POOL
{
System.out.println(“Hello”);//String@0123 java.lang.String@0123
System.out.println(“Hello”);//String@0123
Hello
}
}

HEAP AREA
EXAMPLE:2
Class Demo
{
public static void main(String[] args) CONSTANT POOL / STRING
{ POOL
String s1, s2; S1
java.lang.String@0123
s1 = “Hello”;
s2 = “Hello”; Hello
S2
System.out.println(s1);
System.out.println(s2);
System.out.println(s1==s2);//true
System.out.println(s1.equals(s2));//true
}
}

HEAP AREA
EXAMPLE:3
Class Demo
{
public static void main(String[] args) CONSTANT POOL / STRING
POOL
{
String s1, s2; s1 //String@0123 String@0123
s1 = “Hello”;
Hello
s2 = new String(“Hello”);
System.out.println(s1);
System.out.println(s2);
System.out.println(s1==s2);//false
System.out.println(s1.equals(s2));//true
System.out.println(s1.hashCode()==s2.hashCode());//true String@0456
}
Hello
}

s2 //String@0456

HEAP AREA
STRING CLASS

java.lang.String :
● String is a inbuilt class defined in a java.lang package.
● It is a final class.
● It inherits java.lang.Object
● In a String class toString(), equals(), hashCode() methods of java.lang.Object
class are overridden.
It implements :
● Comparable
● Serializable
● CharSequence
CONSTRUCTOR IN STRING CLASS

CONSTRUCTORS :

CONSTRUCTORS DESCRIPTION
String() Creates an empty string object
Creates string object by initializing with string
String(String literals)
literals
Creates String by converting character array into
String(char[] ch)
string
String(byte[] b) Creates String by converting byte array into string
String(StringBuffer sb) Creates String by converting StringBuffer to string
String(StringBuilder sb) Creates String by converting StringBuilder to string
METHODS OF STRING CLASS

IMPORTANT METHODS :
RETURN
METHOD NAME DESCRIPTION
TYPE
Converts the specified string to Upper
String toUpperCase()
case
Converts the specified string to
String toLowerCase()
Lowercase
String concat(String s) joins the specified Strings
Remove the space present before and
String trim()
after the string
Extract a characters from a string
String substring(int index) object starts from specified index and
ends at the end of a string
Extract a characters from a string starts
substring(int start, int
String from specified index and ends at
end)
end-1 index
RETURN
METHOD NAME DESCRIPTION
TYPE
charAt(int index) Returns character of the specified index
char
from the string
indexOf(char ch) Return the index of the character
int
specified if not return -1
indexOf(char ch, int Return the index of the character
int Start_Index) specified by searching from specified
index if not return -1
indexOf(charSequence str) Return the index of the specified string
int
index if not return -1
indexOf(charSequence Return the index of the specified string by
int str,int Start_Index) searching from specified index if not
returns -1
lastIndexOf(char ch) Returns the index of the character which
int
is occurred at last in the original String
int length() Returns length of the string
RETURN TYPE METHOD NAME DESCRIPTION

boolean equals(Object o) Compares states of a two strings


Compares two strings by
boolean equalsIgnoreCase(String s)
ignoring its case
Returns true if specified String is
boolean contains(String str)
present else it returns false
Returns true if string is empty else
boolean isEmpty()
return false
Converts the specified string into
char[] toCharArray(String str)
character array
Break the specified string into
string[] split(String str) multiple string and returns String
array
Converts the specified string to
byte[] getBytes() byte value and returns byte
array
CHARACTERISTICS OF STRING

CHARACTERISTIC :
● Instance of String class is immutable in nature. (Once the object is created
then the state is not modified)
● If we try to manipulate (Modify) the state/data then new object is created
and reference is given
EXAMPLE:1
Class Demo
{ CONSTANT POOL / STRING
public static void main(String[] args) POOL
{ s1 //String@0123 String@0123 String@0456
String s1, s2;
HELLO
s1 = “Hello”; Hello

s1.toUpperCase();
System.out.println(s1); // Hello
}
}

HEAP AREA
ALPHA
CORE JAVA - SESSION 31
INDEX
● Comparison of String
CORE JAVA ● Disadvantage of java.lang.String
SESSION 31
COMPARISON OF STRING

COMPARISON OF STRING :
● == ------------------------>Compares the reference
● equals() ----------------->Compares state/data of the object
● equalsIgnoreCase()--->Compares the state/data of the object by ignoring its
case
● compareTo()------------>Compares two string and returns integer value
Syntax: “String1”.compareTo(“String2”)
▪ string1==string2 ---> 0
▪ string1>string2-------> +ve Integer
▪ string1<string2--------> -ve Integer
DISADVANTAGE OF java.lang.String

DISADVANTAGES :
Immutability, because for every modification separate object is get
created in a memory, it reduces the performance.
NOTE :
To overcome the disadvantage of String class we can go for StringBuffer and
StringBuilder
EXAMPLE:2 Reversing of a String
Class Demo
{
public static void main(String[] args)
{
String s1=”Cat”;
String reverse = “”; // use "" ( because default value of String is null,
for(int i=s1.length()-1; i>=0; i--) eg-) null +2 = null
{
reverse = reverse+s1.charAt(i);
}
System.out.println(reverse);
}
}
//String@0456
//String@0789
reverse //String@0101
//String@0102

s1 //String@0123 String@0123 String@0456 String@0789

Cat t

see page no: 39 & 23

Iteration
Iteration
Iteration1:
3:
2:
reverse
reverse
reverse===reverse+S1.charAt(2)
reverse+S1.charAt(0)
reverse+S1.charAt(1)
reverse
reverse == “”+t------>
“ta”+C------>
reverse = “t”+a------>t tataC String@0101 String@0102

ta taC

CONSTANT POOL / STRING POOL

HEAP AREA
ALPHA
CORE JAVA - SESSION 32
INDEX
● StringBuffer
● Characteristics of StringBuffer
● Constructor
● Methods
CORE JAVA ● Comparison of StringBuffer
SESSION 32 ● Difference between StringBuffer and StringBuilder
STRINGBUFFER CLASS

java.lang.StringBuffer :
● It is a inbuilt class defined in java.lang package.
● It is a final class.
● It helps to create mutable instance of String.
● StringBuffer does not have SCP.
● It inherits java.lang.Object class.
● In StringBuffer equals(), hashcode() methods of java.lang.Object class are not
overridden.
It implements :
Serializable
CharSequence
EXAMPLE : 1
Class Demo
{ sb2
public static void main(String[] args)
{
StringBuffer sb1, sb2; sb1 StringBuffer@0123 StringBuffer@0456
sb1 = new StringBuffer(“Hello”);
sb2 = new StringBuffer(“Hello”); Hello Hello

System.out.println(sb1);
System.out.println(sb2);
System.out.println(sb1==sb2);//false
System.out.println(sb1.equals(sb2));//false
}
}

HEAP AREA
EXAMPLE : 2
Class Demo
{ sb2
public static void main(String[] args)
{
StringBuffer sb1, sb2; sb1 StringBuffer@0123
sb1 = new StringBuffer(“Hello”);
sb2 = sb1; Hello

System.out.println(sb1);
System.out.println(sb2);
System.out.println(sb1==sb2);//true
System.out.println(sb1.equals(sb2));//true
}
}

HEAP AREA
EXAMPLE : 3
Class Demo
{ sb2
public static void main(String[] args)
{
StringBuffer sb1, sb2; sb1 StringBuffer@0123
sb1 = new StringBuffer(“Hello”);
sb2 = sb1; Hello World

System.out.println(sb1);//Hello
System.out.println(sb2);//Hello
sb1.append(“ World”);
System.out.println(sb1);//Hello World
System.out.println(sb2);//Hello World
System.out.println(sb1==sb2);//true
System.out.println(sb1.equals(sb2));//true
}
}
HEAP AREA
CONSTRUCTORS

CONSTRUCTORS DESCRIPTION
StringBuffer() Creates empty String with initial
capacity 16
StringBuffer(String str) Creates string buffer with the
specified string
IMPORTANT METHODS OF STRINGBUFFER

METHODS :
RETURN
METHOD NAME DESCRIPTION
TYPE
int capacity() Returns current capacity.

int length() Returns length of the string.


Returns the character of the specified
char charAt(int index)
index.
StringBuffer append(String s) Join strings. (Overloaded method).

Insert a specified string into original String


StringBuffer insert(int index, String s) of specified index. (Overloaded method)

Delete String from specified beginning


StringBuffer delete(int begin, int end) index to end-1 index.
RETURN
METHOD NAME DESCRIPTION
TYPE
Delete the character present in the
StringBuffer deleteCharAt(int index)
specified index.
StringBuffer reverse() Reverse the string
Only specified length in a string is exist
StringBuffer setLength(int length)
remaining get removed.
Returns the substring from the
StringBuffer substring(int begin)
specified beginning index
Returns substring from specified
StringBuffer substring(int begin, int end)
beginning index to end-1 index.
Replace a specified string from the
StringBuffer replace(int begin,int end,String s)
beginning index to end-1 index
Removes the unused capacity or set
void trimToSize()
the capacity till length of the string
setCharAt(int index, char new Replace the new character in a string
void
char) of specified index.
void ensureCapacity(int capacity) Sets capacity for storing a string.
CHARACTERISTICS OF STRINGBUFFER

CHARACTERISTICS :
It is mutable.
NOTE :
String constant pool is not applicable to String Buffer.
EXAMPLE :
class StringBufferReverseDemo
{
public static void main(String[] args)
{
StringBuffer sb1=new StringBuffer("Qsp");
StringBuffer reverse = new StringBuffer();
for(int i=sb1.length()-1; i>=0 ; i--)
{
reverse.append(sb1.charAt(i));
}
System.out.println(reverse);
}}
reverse //String@0456
see page no : 23 also
sb1 //String@0123 StringBuffer@0123 String@0456

Cat taC

Iteration 3:
1:
2:
reverse.append(S1.charAt(2))---t
reverse.append(S1.charAt(1))---ta
reverse.append(S1.charAt(0))---taC

HEAP AREA
COMPARISON OF STRINGBUFFER

COMPARISON OF STRINGBUFFER :
● == ------------------------>Compares the reference.
● equals() ----------------->Compares reference of the object.
DISADVANTAGE OF STRINGBUFFER

DISADVANTAGES :
Multiple thread can’t execute the StringBuffer object simultaneously because
all the methods are synchronized. So, Execution time is more. In order to
overcome this problem we will go for String Builder.
NOTE :
The characteristics of StringBuffer and StringBuilder are same
DIFFERENCE BETWEEN STRINGBUFFER AND
STRINGBUILDER

STRING BUFFER STRING BUILDER


All the method present in StringBuffer All the method present in StringBuilder
is synchronized. is non synchronized.
At a time only one thread is allowed At a time multiple thread is allowed to
to access String Buffer object. access String Builder object.
Hence it is Thread safe. Hence it is Not Thread safe.
Threads are required to wait to
Threads are not required to wait to
operate a stringBuffer object.
operate a StringBuilder object.
Hence Relatively performance is low
Hence Relatively performance is high.
.
Efficiency is high compared to
Less efficient than StringBuilder
StringBuffer
Introduced in 1.0 v Introduced in 1.5v
PROGRAMMING SESSION ON
DAY 10
STRING CLASS
SESSION 4

You might also like