String Handling
String Handling
Agend
1. Introduction
1. Java.lang.Object class
2. java.lang.String class
2. Importance of String constant pool (SCP)
3. Interning of String objects
4. String class constructors
5. Important methods of String class
3. StringBuffer
6. Constructors
7. Important methods of StringBuffer
4. StringBuilder (1.5v)
8. StringBuffer Vs StringBuilder
9. String vs StringBuffer vs StringBuilder
5. Wrapper classes
10. Constructors
11. Wrapper class Constructor summery
6. Utility methods
12. valueOf() method
13. xxxValue() method
14. parseXxx() method
15. toString() method
7. Dancing between String, wrapper object and primitive
8. Partial Hierarchy of java.lang package
9. Autoboxing and Autounboxing
16. Autoboxing
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
17. Autounboxing
18. Conclusions
1. Object class
2. String class
3. StringBuffer class
5. Wrapper Classes
For writing any java program the most commonly required classes
and
java.lang package.
to import.
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
Java.lang.Object class:
object class.
2. As object class acts as a root (or) parent (or) super for all java
classes, by default
3. Note : If our class doesn't extends any other class then it is the
direct child class
of object
If our class extends any other class then it is the indirect child class of
Object.
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
7. public final void wait() throws InterruptedException;
toString( ) method :
will be executed.
4. Example:
6. Example 1:
7. class Student
8. {
9. String name;
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
12. {
13. this.name=name;
14. this.rollno=rollno;
15. }
19. System.out.println(s1);
20. System.out.println(s1.toString());
21. System.out.println(s2);
22. }
23. }
24. Output:
25. Student@3e25a5
26. Student@3e25a5
27. Student@19821f
28.
in our class.
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
Ex : For example whenever we are try to print student reference to
print his a
38. }
40.
41. Example 2:
42.
46. }
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
50. Test t=new Test();
51. System.out.println(i);
52. System.out.println(s);
53. System.out.println(t);
54. }
55. }
56. Output:
57. 10
58. ashok
59. Test
hashCode() method :
hashCode.
2. Jvm will using hashCode while saving objects into hashing related
data
on hashCode).
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
4. If we didn't override hashCode() method then Object class
hashCode() method
equals() method:
Example 5:
class Student
String name;
int rollno;
this.name=name;
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
this.rollno=rollno;
Student s4=s1;
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equals(s4));
}}
Output:
False
False
True
Diagram:
In
the
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
above program Object class .equals() method got executed which is
always
reference comparison.
Diagram:
system
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
Note : In String class , Wrapper classes and all collection classes
.equals( ) method is
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
4. If r1.equals(r2) is false then r1==r2 is always false.
StringBuffer
System.out.println(s.equals(sb)); //false
Note:
return false;
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
}
return false;
System.out.println(p1.equals(p2));
System.out.println(p1.equals(i));
Output:
True
False
Clone () method:
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
protected native object clone() throws CloneNotSupportedException;
Example:
int i=10;
int j=20;
CloneNotSupportedException
Test t2=(Test)t1.clone();
t2.i=888;
t2.j=999;
System.out.println(t1.i+"---------------"+t1.j);
System.out.println(t2.i+"---------------"+t2.j);
Output:
10---------------20
888---------------999
Diagram:
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
We can perform cloning only for Cloneable objects.
java.lang.String class :
Case 1:
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
Case 2 :
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
Note :
1. Object creation in SCP is always optional 1st JVM will check is any
object
with same content on SCP that is duplicate objects are not allowed in
SCP.
2. Garbage collector can't access SCP area hence even though object
doesn't have
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
Example 1:
String s3="bhaskar";
String s4="bhaskar";
Note :
When ever we are using new operator compulsory a new object will
be created on the
Diagram :
Example 2:
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
s.concat("software");
s=s.concat("solutions");
s="bhaskarsoft";
Diagram :
For every String Constant one object will be created in SCP. Because
of runtime
Example 3:
s1.concat("fall");
s1=s1+"winter";
String s2=s1.concat("summer");
System.out.println(s1);
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
System.out.println(s2);
Diagram :
Example:
class StringDemo
System.out.println(s1==s2);//false
System.out.println(s1==s3);//false
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
System.out.println(s3==s4);//true
System.out.println(s3==s5);//true
System.out.println(s3==s7);//false
System.out.println(s3==s9);//true
System.out.println(s6==s8);//true
Diagram:
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
Diagram:
2. We can create only one copy and we can reuse the same object for
every
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
4. According to this once we creates a String object we can't perform
any changes
of scp.
FAQS :
6. What is SCP?
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
one object and we can reuse same object for every requirement. This
approach
9. Why SCP like concept available only for the String but not for the
StringBuffer?
performance.
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
In the case of String as several references pointing to the same object,
by using
11. Similar to String objects any other objects are immutable in java?
Yes.
13. Explain the process of creating our own immutable class with an
example?
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
15. What is interning of String objects?
Example 1:
class StringDemo {
String s2=s1.intern();
System.out.println(s1==s2); //false
String s3="bhaskar";
System.out.println(s2==s3);//true
Diagram:
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
If the corresponding object is not there in SCP then intern() method
itself will create
Example 2:
class StringDemo {
String s2=s1.concat("software");
String s3=s2.intern();
String s4="bhaskarsoftware";
System.out.println(s3==s4);//true
Diagram 2:
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
Creates an empty String Object.
Example:
class StringDemo {
char[] ch={'a','b','c'} ;
System.out.println(ch);//abc
Example:
class StringDemo {
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
byte[] b={100,101,102};
System.out.println(s);//def
Example:
class StringDemo {
String s="ashok";
System.out.println(s.charAt(3));//o
System.out.println(s.charAt(100));// RE :
StringIndexOutOfBoundsException
3. Example:
4. class StringDemo {
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
5. public static void main(String[] args) {
6. String s="ashok";
7. s=s.concat("software");
8. //s=s+"software";
9. //s+="software";
10. System.out.println(s);//ashoksoftware
11. }
12. }
The overloaded "+" and "+=" operators also meant for concatenation
purpose
only.
Example:
class StringDemo {
String s="java";
System.out.println(s.equals("JAVA"));//false
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
System.out.println(s.equalsIgnoreCase("JAVA"));//true
Example:
class StringDemo {
String s="ashoksoft";
System.out.println(s.substring(5));//soft
Example:
class StringDemo {
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
public static void main(String[] args) {
String s="ashoksoft";
System.out.println(s.substring(5));//soft
System.out.println(s.substring(3,7));//okso
Example:
class StringDemo {
String s="jobs4times";
System.out.println(s.length());//10
/*
CE :
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
*/
Example:
class StringDemo {
String s="ababab";
System.out.println(s.replace('a','b'));//bbbbbb
Example:
class StringDemo {
String s="ASHOK";
System.out.println(s.toLowerCase());//ashok
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
}
Example :
class StringDemo {
String s="ashok";
System.out.println(s.toUpperCase());//ASHOK
the string but not blank spaces present at middle of the String.
Example:
class StringDemo {
System.out.println(s.trim());//sai charan
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
22. public int indexOf(char ch);
Example:
class StringDemo {
String s="saicharan";
System.out.println(s.indexOf('c')); // 3
System.out.println(s.indexOf('z')); // -1
Example:
class StringDemo {
String s="arunkumar";
System.out.println(s.lastIndexOf('a'));//7
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
System.out.println(s.indexOf('z'));//-1
Note :
reused.
Example 1 :
class StringDemo {
String s1="bhaskar";
String s2=s1.toUpperCase();
String s3=s1.toLowerCase();
System.out.println(s1==s2);//false
System.out.println(s1==s3);//true
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
Diagram :
Example 2:
class StringDemo {
String s1="bhaskar";
String s2=s1.toString();
System.out.println(s1==s2);//true
Diagram :
class StringDemo {
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
public static void main(String[] args) {
String s2=s1.toString();
String s3=s1.toUpperCase();
String s4=s1.toLowerCase();
String s5=s1.toUpperCase();
String s6=s3.toLowerCase();
System.out.println(s1==s6); //false
System.out.println(s3==s5); //false
Final vs immutability :
for objects
object.
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
3. That is by declaring a reference variable as final we won't get any
immutability
nature .
Example:
class Test
sb.append("software");
System.out.println(sb);//ashoksoftware
In the above example even though "sb" is final we can perform any
type of change in
immutability nature.
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
1. final variable (valid)
StringBuffer :
new object)
Constructors :
Newcapacity=(currentcapacity+1)*2.
Example:
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
class StringBufferDemo {
System.out.println(sb.capacity());//16
sb.append("abcdefghijklmnop");
System.out.println(sb.capacity());//16
sb.append("q");
System.out.println(sb.capacity());//34
Example:
class StringBufferDemo {
System.out.println(sb.capacity());//19
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
Creates an equivalent StringBuffer object for the given String with
capacity=s.length()+16;
Example:
class StringBufferDemo {
System.out.println(sb.capacity());//21
Example:
class StringBufferDemo {
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
System.out.println(sb.length());//18
System.out.println(sb.capacity());//34
System.out.println(sb.charAt(14));//e
System.out.println(sb.charAt(30));//RE :
StringIndexOutofBoundsException
Example:
class StringBufferDemo {
sb.setCharAt(8,'A');
System.out.println(sb);
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
8. public StringBuffer append(boolean b); All these are overloaded
methods.
12. Example:
17. sb.append(3.14);
19. sb.append(true);
true
21. }
22. }
23.
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
27. public StringBuffer insert(int index,double d); All are
overloaded methods
32. Example :
37. sb.insert(11,"9");
38. System.out.println(sb);//abxyzcdefgh9
39. }
40. }
Example:
class StringBufferDemo {
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
public static void main(String[] args) {
System.out.println(sb);//saicharankumar
sb.delete(6,13);
System.out.println(sb);//saichar
sb.deleteCharAt(5);
System.out.println(sb);//saichr
44. Example :
48. System.out.println(sb);//ashokkumar
49. System.out.println(sb.reverse());//ramukkohsa
50. }
51. }
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
characters.
Example:
class StringBufferDemo {
sb.setLength(6);
System.out.println(sb);//ashokk
To deallocate the extra allocated free memory such that capacity and
size are
equal.
Example:
class StringBufferDemo {
System.out.println(sb.capacity());//1000
sb.append("ashok");
System.out.println(sb.capacity());//1000
sb.trimToSize();
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
System.out.println(sb.capacity());//5
Example:
class StringBufferDemo {
System.out.println(sb.capacity());//16
sb.ensureCapacity(1000);
System.out.println(sb.capacity());//1000
Note :
StringBuilder.
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
StringBuilder (1.5v)
system.
StringBuffer Vs StringBuilder
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]
1. If the content is fixed and won't change frequently then we should
go for String.
50, Lakhanpur Housing Society, Near Lucky Restaurant, Vikas Nagar 208024
Contact: 9794687277, 8707276645
Web: www.bytecode.co.in, email: [email protected]