String 1.1
String 1.1
1
Agenda
• Basic Concepts
Immutable and Mutable strings
Two ways to create string objects
• String Class Methods (immutable)
• StringBuffer Constructors & Methods (mutable)
• StringBuilder Constructors &Methods (mutable)
• String class Vs StringBuffer class Vs
2
Basic Concepts
3
Basic Concepts
• Java supports two types of strings
1. Immutable String
• Once a string is created as an object, it requires explicit processing
for changes, implicit processing never changes the content
• Example
• String class is immutable (available in <java.lang.String>)
• String str1 = “Welcome to String Processing!”;
• System.out.println(str1); // displays <Welcome to String Processing>
• System.out.println(str1.concat(“ in Java”)); // Implicit processing, no changes
• // only displays, never updates <Welcome to String Processing in Java>
• str1 = str1.concat(“ in Java”); // Explicit assignment, changes str1
4
Basic Concepts
2. Mutable String
• It accepts changes in content even for implicit processing, so
no need of explicit notations
• Example – (1) StringBuffer Class
• StringBuffer class is mutable & synchronized <java.lang.StringBuffer>
• StringBuffer str1 = new StringBuffer(“Mutable String Processing!”);
• System.out.println(str1); // displays <Mutable String Processing!>
• System.out.println(str1.append(“ in Java”));
• // Implicit processing is enough for change
• // updates and displays < Mutable String Processing in Java>
5
Basic Concepts
2. Mutable String
• Example – (2) StringBuilder Class
• StringBuilder class is mutable & non-synchronized <java.lang.StringBuilder>
• StringBuilder str1 = new StringBuilder(“Mutable String Processing!”);
• System.out.println(str1); // displays <Mutable String Processing!>
• System.out.println(str1.append(“ in Java”));
• // Implicit processing is enough for change
• // updates and displays < Mutable String Processing in Java>
Demo Program
6
Basic Concepts
• Two ways to create string object
By string literal
char[] gr1 = {‘w’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’} ; // user-defined
char[] ca1 = {‘s’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’} ; // user-defined
String str = “Welcome to String!” // built-in
By new keyword
String gr2 = new String(gr1) ; // built-in
String ca2 = new String(ca1) ; // built-in
String gr3 = new String(“Hello World!”) ; // built-in
• Source Code with Output
7
Basic Concepts
• Why avoid String literal method for String object creation?
• Example
oString s1=“Welcome";
oString s2=“Welcome";
• Here the compiler will create only
a single String object which will
store the assigned literal
• If the object already exists in the memory then the new object of
String class will not be created instead the value will be assigned
to the existing object
8
Basic Concepts
• Why avoid String literal method for String object
creation?
• Using new operator
• Example
• String str1=new String ("Welcome");
• String str2=new String ("Welcome");
• Here the two different String objects str1 and str2 will
have the same value as “Welcome” and it will be stored
in the memory.
9
String Class Methods (java.lang.String)
• String class supports the following methods
1. length (final variable for char array and method for string object)
2. concat 9. format 16. indexOf 23. getBytes
3. trim 10. charAt 17. contains 24. getChars
4. valueOf 11. substring 18. join 25. compareTo
5. toLowerCase 12. isEmpty 19. startsWith 26. lastIndexOf
6. toUpperCase 13. replace 20. endsWith
7. equals 14. replaceAll 21. intern
8. equalsIgnoreCase 15. split 22. toCharArray
Output/Return
Method Purpose Example
value
5
length() To find the numbers of characters in a String name=“KUMAR”;
string name.length() ; Demo Program
12
valueOf() To convert different types of values int a = 12;
into string String.valueOf(a); Demo Program
11
Output /
Method Purpose Example Return
value
String s1=“Good”; G
charAt() To extract a character at the given index s1.charAt(0);
Demo Program
To extract a part of the string at the given range String s2=“Nice Day”; Day
substring() or at the given index to the end s2.substring(5);
Demo Program
Good
To separate the given string into individual String s3=“Good Day!”; Day!
split() words by using a delimiter String[] s4=s3.split(“\\s”);
// delimiter is blank space Demo Program
String s1=“Testing…”; 2
indexOf (char) To know the position of a character s1.indexOf(‘s’); // char
Demo Program
String s3=“Testing…”; 2
indexOf (str) To know the position of a string s3.indexOf(“s”); // str
Demo Program
String s1=“Hai”;
String s2=new String(“Hai”);
String s3=s1.intern(); // s3=pool value of s1
To create a duplicate copy String s4=s2.intern(); // s4=pool value of s2
of an existing string which
if (s1==s2) false
is in string constant pool
intern() if (s1==s3) true
if (s1==s4) true
Demo Program
if (s2==s3) false
if (s2==s4) false
if (s3==s4) true
17
Method Purpose Example
20
StringBuffer Class Constructors
Constructor Purpose Example
StringBuffer(String str) To create a string buffer with the StringBuffer sb2=new StringBuffer(“Hai”);
given string // creates a string buffer with <Hai>
StringBuilder(String str) To create a string builder with the StringBuilder sb2=new StringBuilder(“Hai”);
given string // creates a string builder with <Hai>
Immutable Mutable
(if we try to change, a new object gets created) (allows changes)
Overrides the equals() of Object class Doesn’t override the equals() of Object class
(which allows us to compare strings) (so we cannot compare strings)
The length of the string object is fixed The length of the StringBuffer can be increased
34