STRING CLASS in Java
STRING CLASS in Java
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
CONSTANT POOL /
{ STRING POOL
public static void main(String[] args)
{
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
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
String(String literals) Creates string object by initializing with string literals
String(char[] ch) Creates String by converting character array into string
s2 //String@0456
HEAP AREA
METHODS OF STRING CLASS
IMPORTANT METHODS :
RETURN
METHOD NAME DESCRIPTION
TYPE
String toUpperCase() Converts the specified string to Upper case
String toLowerCase() Converts the specified string to Lowercase
String concat(String s) joins the specified Strings
Remove the space present before and after
String trim()
the string
Extract a characters from a string object
String substring(int index) starts from specified index and ends at the
end of a string
substring(int start, int Extract a characters from a string starts from
String specified index and ends at end-1 index
end)
RETURN
METHOD NAME DESCRIPTION
TYPE
charAt(int index) Returns character of the specified index from
char
the string
indexOf(char ch) Return the index of the character specified if
int
not return -1
indexOf(char ch, int Return the index of the character specified by
int
Start_Index) searching from specified index if not return -1
indexOf(charSequence str) Return the index of the specified string index if
int
not return -1
indexOf(charSequence str,int Return the index of the specified string by
int
Start_Index) searching from specified index if not returns -1
lastIndexOf(char ch) Returns the index of the character which is
int
occurred at last in the original String
int length() Returns length of the string
RETURN
METHOD NAME DESCRIPTION
TYPE
boolean equals(Object o) Compares states of a two strings
Compares two strings by ignoring its
boolean equalsIgnoreCase(String s)
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
byte[] getBytes()
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 POOL
public static void main(String[] args)
{ s1 //String@0123 String@0123 String@0456
String s1, s2;
s1 = “Hello”; Hello HELLO
s1.toUpperCase();
System.out.println(s1);
// Hello
}
}
HEAP AREA
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 = “”;
for(int i=s1.length()-1; i>=0; i--)
{
reverse = reverse+s1.charAt(i);
}
System.out.println(reverse);
}
}
//String@0456
//String@0789
reverse //String@0101
//String@0102
Iteration
Iteration1:
Iteration 3:
2:
reverse
reverse===
reverse
reverse+S1.charAt(2)
reverse+S1.charAt(0)
reverse+S1.charAt(1) String@0101 String@0102
reverse
reverse===“”+t------>
reverse t
“ta”+C------>
“t”+a------>
taC
ta ta taC
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
CONSTRUCTORS
CONSTRUCTORS DESCRIPTION
StringBuffer() Creates empty String with initial capacity
16
StringBuffer(String str) Creates string buffer with the specified
string
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
IMPORTANT METHODS OF STRINGBUFFER
METHODS :
RETURN
METHOD NAME DESCRIPTION
TYPE
int capacity() Returns current capacity.
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("Cat");
StringBuffer reverse = new StringBuffer();
for(int i=sb1.length()-1; i>=0 ; i--)
{
reverse.append(sb1.charAt(i));
}
System.out.println(reverse);
}}
//String@0456
reverse
Iteration 2:
1:
3:
reverse.append(S1.charAt(1))---ta
reverse.append(S1.charAt(2))---t
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
At a time only one thread is allowed to At a time multiple thread is allowed to access
access String Buffer object. String Builder object.
Hence it is Thread safe. Hence it is Not Thread safe.
Threads are required to wait to operate a Threads are not required to wait to operate a
stringBuffer object. StringBuilder object.
Hence Relatively performance is low . Hence Relatively performance is high.
Less efficient than StringBuilder Efficiency is high compared to StringBuffer
Introduced in 1.0 v Introduced in 1.5v