2.9 Strings
2.9 Strings
UNIT NO 2
INHERITANCE AND INTERFACES
2.9 STRINGS
II III
20CSPC301
OBJECT ORIENTED PROGRAMMING
(Common to CSE, EEE, EIE, ICE, IT)
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
Introduction
char[] ch={'j','a','v','a'};
is same as:
String s="java";
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
Definition of String
String Creation
● String s = new String("Welcome"); //creates two objects and one reference variable
● In such case, JVM will create a new string object in normal (non-pool) heap memory,
and the literal "Welcome" will be placed in the string constant pool.
● The variable s will refer to the object in a heap (non-pool).
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
11. String replace(char old, char new) replaces all occurrences of the
specified char value.
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
15. String[] split(String regex, int limit) returns a split string matching
regex and limit.
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
16. int indexOf(int ch) returns the specified char value index.
17. int indexOf(int ch, int fromIndex) returns the specified char value index
starting with given index.
19. int indexOf(String substring, int returns the specified substring index
fromIndex) starting with given index.
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
● The java string charAt() method returns a char value at the given index number.
● The index number starts from 0 and goes to n-1, where n is length of the string.
● It returns StringIndexOutOfBoundsException if given index number is greater than
or equal to this string length or a negative number.
Java String charAt() method example
public class CharAtExample{
public static void main(String args[]){
String name="java";
char ch=name.charAt(2);//returns the char value at the 2nd index
System.out.println(ch);
}}
Output:
v
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
● The java string compareTo() method compares the given string with current string
lexicographically. It returns positive number, negative number or 0.
● It compares strings on the basis of Unicode value of each character in the strings.
○ If first string is lexicographically greater than second string, it returns positive
number (difference of character value).
○ If first string is less than second string lexicographically, it returns negative
number and
○ if first string is lexicographically equal to second string, it returns 0.
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
Immutable String
● In java, string objects are immutable. Immutable simply means unmodifiable or
unchangeable.
● Once string object is created its data or state can't be changed but a new string
object is created.
Example:
class Testimmutablestring{
public static void main(String args[]){
String s="Sachin";
s.concat(" Tendulkar");//concat() method appends the string at the end
System.out.println(s);//will print Sachin because strings are immutable objects
s= s.concat(“Tendulkar”);
System.out.println(s);
}
}
Output:Sachin
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
● As you can see in the above figure that two objects are created but s reference
variable still refers to "Sachin" not to "Sachin Tendulkar".
● But if we explicitly assign it to the reference variable, it will refer to "Sachin
Tendulkar" object.
For example:
class Testimmutablestring1{
public static void main(String args[]){
String s="Sachin";
s=s.concat(" Tendulkar"); // explicit assignment
System.out.println(s);
} }
Output:Sachin Tendulkar
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
Constructor Description
StringBuffer() creates an empty string buffer with the initial capacity of 16.
StringBuffer(int capacity) creates an empty string buffer with the specified capacity
as length.
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
Method Description
append(String s) is used to append the specified string with this string. The
append() method is overloaded like append(char),
append(boolean), append(int), append(float), append(double)
etc.
insert(int offset, String s) is used to insert the specified string with this string at the
specified position. The insert() method is overloaded like
insert(int, char), insert(int, boolean), insert(int, int), insert(int,
float), insert(int, double) etc.
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
Method Description
delete(int startIndex, int is used to delete the string from specified startIndex and
endIndex) endIndex.
replace(int startIndex, int is used to replace the string from specified startIndex
endIndex, String str) and endIndex.
length() is used to return the length of the string i.e. total number
of characters.
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
StringBuilder class
Constructor Description
Method Description
public StringBuilder is used to append the specified string with this string.
append(String s) The append() method is overloaded like append(char),
append(boolean), append(int), append(float),
append(double) etc.
public StringBuilder insert(int is used to insert the specified string with this string at
offset, String s) the specified position. The insert() method is
overloaded like insert(int, char), insert(int, boolean),
insert(int, int), insert(int, float), insert(int, double) etc.
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
Method Description
public StringBuilder delete(int startIndex, is used to delete the string from specified
int endIndex) startIndex and endIndex.
● The StringBuilder insert() method inserts the given string with this string at the given
position.
class StringBuilderExample2{
public static void main(String args[]){
StringBuilder sb=new StringBuilder("Hello ");
sb.insert(1,"Java"); // now original string is changed -- Java inserted at index 1
System.out.println(sb);
}
}
output
HJavaello
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
String StringBuffer
String is slow and consumes more memory StringBuffer is fast and consumes less
when you concat too many strings because memory when you concat strings.
every time it creates new instance.
String class overrides the equals() method StringBuffer class doesn't override the
of Object class. So you can compare the equals() method of Object class.
contents of two strings by equals() method.
20CSPC301
Object Oriented Programming (Common to CSE, EEE, EIE, ICE, IT)
StringBuffer StringBuilder
It means two threads can't call the It means two threads can call the
methods of StringBuffer simultaneously. methods of StringBuilder simultaneously.
https://youtu.be/xcEe9GYEcA4
https://youtu.be/zS6oYgIrnd8
https://youtu.be/ZhQxRyyqiI8
`