SlideShare a Scribd company logo
5
Most read
6
Most read
8
Most read
String Handling Supported by :  java.lang.String java.lang.StringBuffer java.lang.StringBuilder
String Strings are immutable objects of type java.lang.String. Meaning of Immutablility : We can’t change the sequence of Characters encapsulated in a String Object. Each operation on that object will produce a new String object.
Ways of Creating String Objects : 2 Using new Keyword String s1 = new String(); String s2 = new String(“Java”); Using String literal String s3 = “J2EE”
Understanding the immutability : String s1 = new String(“Java”); String s2 = new String(“J2ee”); s2=s1.concat(s2); Java J2ee Heap s1 s2 Java J2ee Heap s1 s2 JavaJ2ee
Which one was immutable? String reference variable or String Object itself. Count the total no. of objects created. How many String objects can be referred to by our application in the last example? How many are missing?
String Literal & String Constant Pool String s1 = “microsoft”; String s2 = “IBM”; String s3 = “microsoft”; String s4 = new String(“microsoft:”); String s5=s4; How may String literal objects have been constructed? s1 String Constant Pool microsoft IBM s2 s3 Heap s4 microsoft s5
String Literal & String Constant Pool   It a special memory area set aside by JVM to make Java more memory efficient.
When a String literal is encountered, the pool is checked to see if an identical String already exists. If it’s there, no new String literal object is created, we’ll simply have an additional reference to the same existing object.
Now, if several reference variables refer to the same String object without even knowing it. It would be very bad if anyone could change the String’s value. So Strings have been made immutable and java.lang.String has been declared final.
Understanding the different ways to create String Objects String s1 = “Noida”; Noida s1 String Pool String s2 = new  String(“Delhi”); String s3 = new String(“Noida”); Heap Delhi s3 s2 Delhi Noida Note that String literal “Delhi” has been placed in the pool.
+ & += have been overloaded to work on Strings. int a=6; char c=‘a’; String s1 =“ is “; String s2 = new String(“ whole no.”); 6 a a c String Pool is whole no. Heap s1 s2 whole no. 6 is a whole no. System.out.println(a+s1+c+s2);
Working with Strings : Using public methods defined in the java.lang.String public char charAt(int index) returns the character with the index specified. public String concat(String s) concatenates s at the end of the string invoked & returns the reference of new String Object. public Boolean equalsIgnoreCase(String s) Compares 2 String Objects irrespective of their character case. public int length() returns the no. of characters of the character sequence encapsulated in the string object. length() of String class is different than the  length  that we use to get no. of elements in an array. String s=“winter”; char c[]=new char[]{‘s’,’u’,’m’,’m’,’e’,’r’}; s.length;  //invalid c.length(); //invalid
Shallow & Deep Comparison String s1 = “star”;  String s3 = new String(“star”); System.out.println( s1== s2 ); System.out.println( s1.equals(s2) ); System.out.println( s1 == s3 ); System.out.println( s2.equals(s3) ); java.lang.Object java.lang.String Output :  true true false true public boolean equals(java.lang.Object); public boolean equals(java.lang.Object); equals (java.lang.Object) of java.lang.Object is overridden by equals(java.lang.Object) of java.lang.String
Shallow & Deep Comparison  contd. == does shallow comparison,means calls equals() from Object class that compares the refrences not their character sequences. While equals() of String class doesn’t compares their refrences. Instead it compares their character sequences. String Pool Heap s1 s3 s2 star star
StringBuffer Objects of StringBuffer type are used when we have to do a lot of modification to strings of characters. StringBuffer objects are mutable. String s = “keyboard”; s.concat(“mouse”); System.out.println(s);  // output ? String s = “Tom” s=s.concat(“cat”); System.out.println(s);  // output ?
StringBuffer  contd. All of the StringBuffer methods operate on the content of the object invoked on.The methods won’t produce intermediate objects. StringBuffer sb = “micro”; sb.append(“soft”); System.out.println(sb);  // output ?
Important methods of StringBuffer public synchronized StringBuffer append(String s) public synchronized java.lang.StringBuffer append(char[]); public synchronized java.lang.StringBuffer append(char[], int, int); public synchronized java.lang.StringBuffer append(boolean); public synchronized java.lang.StringBuffer append(char); public synchronized java.lang.StringBuffer append(int); public synchronized java.lang.StringBuffer append(long); public synchronized java.lang.StringBuffer append(float); public synchronized java.lang.StringBuffer append(double); public synchronized java.lang.StringBuffer append(java.lang.Object); public synchronized java.lang.StringBuffer append(java.lang.StringBuffer); public java.lang.StringBuffer append(java.lang.CharSequence); StringBuffer sb = new StringBuffer(); sb.append(“5>6=“); sb.append(5>6);
Important methods of StringBuffer public synchronized StringBuffer insert(int offset, String s) public java.lang.StringBuffer insert(int, boolean); public synchronized java.lang.StringBuffer insert(int, char); public java.lang.StringBuffer insert(int, int); public java.lang.StringBuffer insert(int, long); public java.lang.StringBuffer insert(int, float); public java.lang.StringBuffer insert(int, double); StringBuffer sb = new StringBuffer(“abcdefg"); sb.insert(2, “  "); System.out.println( sb );  // output ab  cdefg public synchronized StringBuffer reverse() StringBuffer sb = new StringBuffer(“pooL”); sb.reverse(); System.out.println(sb);  //  output Loop public String toString()
StringBuffer  contd. StringBuffer sb1 = new StringBuffer(“microsoft”); StringBuffer sb2 = new StringBuffer(“microsoft”); System.out.println( sb1 == sb2 ); System.out.println( sb1.equals(sb2) ); Output : false false java.lang.Object java.lang.StringBuffer java.lang.AbstractStringBuilder public boolean equals(java.lang.Object);

More Related Content

PDF
String handling(string class)
PPT
PPTX
Java Strings
PPTX
I/O Streams
PPTX
Java strings
PPTX
Strings in Java
PPTX
Java string handling
PPT
9. Input Output in java
String handling(string class)
Java Strings
I/O Streams
Java strings
Strings in Java
Java string handling
9. Input Output in java

What's hot (20)

PPTX
String Builder & String Buffer (Java Programming)
PDF
Java Collection framework
PPT
Java And Multithreading
ODP
Multithreading In Java
PDF
PPTX
File handling
PPT
Java multi threading
PDF
Constructors in Java (2).pdf
PPTX
Main method in java
PPTX
Multithreading in java
PPTX
Core java
PPTX
Data Types, Variables, and Operators
PPTX
PPTX
History Of JAVA
PPTX
Java Stack Data Structure.pptx
PDF
Enumeration in Java Explained | Java Tutorial | Edureka
PPTX
Applets in java
PDF
Java IO
PPTX
Arrays in java
PPTX
Java Method, Static Block
String Builder & String Buffer (Java Programming)
Java Collection framework
Java And Multithreading
Multithreading In Java
File handling
Java multi threading
Constructors in Java (2).pdf
Main method in java
Multithreading in java
Core java
Data Types, Variables, and Operators
History Of JAVA
Java Stack Data Structure.pptx
Enumeration in Java Explained | Java Tutorial | Edureka
Applets in java
Java IO
Arrays in java
Java Method, Static Block
Ad

Viewers also liked (20)

PPTX
Java String
PPTX
Introduction to Java Strings, By Kavita Ganesan
PPTX
Exceptional Handling in Java
PPTX
PPS
Java Exception handling
PDF
Exception handling
PPTX
Indian Property Lawyers .com - How It Works
PDF
StringTokenizer in java
PPTX
Exception handling in java
PPTX
Object Oriented Programing JAVA presentaion
PPTX
New presentation1
PDF
Case studies
PPTX
String c++
PPT
Strings v.1.1
PPTX
L13 string handling(string class)
PPTX
Final keyword in java
PPT
exception handling
PDF
April 2014
PDF
Strinng Classes in c++
PDF
String handling(string buffer class)
Java String
Introduction to Java Strings, By Kavita Ganesan
Exceptional Handling in Java
Java Exception handling
Exception handling
Indian Property Lawyers .com - How It Works
StringTokenizer in java
Exception handling in java
Object Oriented Programing JAVA presentaion
New presentation1
Case studies
String c++
Strings v.1.1
L13 string handling(string class)
Final keyword in java
exception handling
April 2014
Strinng Classes in c++
String handling(string buffer class)
Ad

Similar to String Handling (20)

PPTX
Java Strings.pptxJava Strings.pptxJava Strings.pptx
PPTX
Day_5.1.pptx
PDF
PPTX
Java String Handling
PPTX
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
PPT
Text processing
PDF
Module-1 Strings Handling.ppt.pdf
PDF
Java - Strings Concepts
PPS
String and string buffer
PPTX
10619416141061941614.106194161fff4..pptx
PDF
Java Presentation on the topic of string
PPT
13 Strings and text processing
PPTX
String in java, string constructors and operations
PDF
M251_Meeting 2_updated_2.pdf(M251_Meeting 2_updated_2.pdf)
PDF
LectureNotes-04-DSA
PPTX
javastringexample problems using string class
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
PPT
07slide
PPT
8. String
PPTX
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
Java Strings.pptxJava Strings.pptxJava Strings.pptx
Day_5.1.pptx
Java String Handling
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
Text processing
Module-1 Strings Handling.ppt.pdf
Java - Strings Concepts
String and string buffer
10619416141061941614.106194161fff4..pptx
Java Presentation on the topic of string
13 Strings and text processing
String in java, string constructors and operations
M251_Meeting 2_updated_2.pdf(M251_Meeting 2_updated_2.pdf)
LectureNotes-04-DSA
javastringexample problems using string class
Strings in javamnjn ijnjun oinoin oinoi .ppt
07slide
8. String
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA

More from Bharat17485 (12)

PPT
Channel Based Io
PPT
Core Java
PPT
Developing Multithreaded Applications
PPT
Interfaces & Abstract Classes
PPT
PPT
Exceptions & Its Handling
PPT
Jstl & El
PPT
Primitive Wrappers
PPT
Regular Expression
PPT
Stream Based Input Output
PPT
PPT
Applying Generics
Channel Based Io
Core Java
Developing Multithreaded Applications
Interfaces & Abstract Classes
Exceptions & Its Handling
Jstl & El
Primitive Wrappers
Regular Expression
Stream Based Input Output
Applying Generics

Recently uploaded (20)

PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Event Presentation Google Cloud Next Extended 2025
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PDF
Google’s NotebookLM Unveils Video Overviews
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Reimagining Insurance: Connected Data for Confident Decisions.pdf
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
PDF
Modernizing your data center with Dell and AMD
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Dell Pro 14 Plus: Be better prepared for what’s coming
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Event Presentation Google Cloud Next Extended 2025
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
Google’s NotebookLM Unveils Video Overviews
Transforming Manufacturing operations through Intelligent Integrations
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Smarter Business Operations Powered by IoT Remote Monitoring
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Understanding_Digital_Forensics_Presentation.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Reimagining Insurance: Connected Data for Confident Decisions.pdf
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
Modernizing your data center with Dell and AMD
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Chapter 2 Digital Image Fundamentals.pdf
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
NewMind AI Weekly Chronicles - August'25 Week I
Dell Pro 14 Plus: Be better prepared for what’s coming

String Handling

  • 1. String Handling Supported by : java.lang.String java.lang.StringBuffer java.lang.StringBuilder
  • 2. String Strings are immutable objects of type java.lang.String. Meaning of Immutablility : We can’t change the sequence of Characters encapsulated in a String Object. Each operation on that object will produce a new String object.
  • 3. Ways of Creating String Objects : 2 Using new Keyword String s1 = new String(); String s2 = new String(“Java”); Using String literal String s3 = “J2EE”
  • 4. Understanding the immutability : String s1 = new String(“Java”); String s2 = new String(“J2ee”); s2=s1.concat(s2); Java J2ee Heap s1 s2 Java J2ee Heap s1 s2 JavaJ2ee
  • 5. Which one was immutable? String reference variable or String Object itself. Count the total no. of objects created. How many String objects can be referred to by our application in the last example? How many are missing?
  • 6. String Literal & String Constant Pool String s1 = “microsoft”; String s2 = “IBM”; String s3 = “microsoft”; String s4 = new String(“microsoft:”); String s5=s4; How may String literal objects have been constructed? s1 String Constant Pool microsoft IBM s2 s3 Heap s4 microsoft s5
  • 7. String Literal & String Constant Pool It a special memory area set aside by JVM to make Java more memory efficient.
  • 8. When a String literal is encountered, the pool is checked to see if an identical String already exists. If it’s there, no new String literal object is created, we’ll simply have an additional reference to the same existing object.
  • 9. Now, if several reference variables refer to the same String object without even knowing it. It would be very bad if anyone could change the String’s value. So Strings have been made immutable and java.lang.String has been declared final.
  • 10. Understanding the different ways to create String Objects String s1 = “Noida”; Noida s1 String Pool String s2 = new String(“Delhi”); String s3 = new String(“Noida”); Heap Delhi s3 s2 Delhi Noida Note that String literal “Delhi” has been placed in the pool.
  • 11. + & += have been overloaded to work on Strings. int a=6; char c=‘a’; String s1 =“ is “; String s2 = new String(“ whole no.”); 6 a a c String Pool is whole no. Heap s1 s2 whole no. 6 is a whole no. System.out.println(a+s1+c+s2);
  • 12. Working with Strings : Using public methods defined in the java.lang.String public char charAt(int index) returns the character with the index specified. public String concat(String s) concatenates s at the end of the string invoked & returns the reference of new String Object. public Boolean equalsIgnoreCase(String s) Compares 2 String Objects irrespective of their character case. public int length() returns the no. of characters of the character sequence encapsulated in the string object. length() of String class is different than the length that we use to get no. of elements in an array. String s=“winter”; char c[]=new char[]{‘s’,’u’,’m’,’m’,’e’,’r’}; s.length; //invalid c.length(); //invalid
  • 13. Shallow & Deep Comparison String s1 = “star”; String s3 = new String(“star”); System.out.println( s1== s2 ); System.out.println( s1.equals(s2) ); System.out.println( s1 == s3 ); System.out.println( s2.equals(s3) ); java.lang.Object java.lang.String Output : true true false true public boolean equals(java.lang.Object); public boolean equals(java.lang.Object); equals (java.lang.Object) of java.lang.Object is overridden by equals(java.lang.Object) of java.lang.String
  • 14. Shallow & Deep Comparison contd. == does shallow comparison,means calls equals() from Object class that compares the refrences not their character sequences. While equals() of String class doesn’t compares their refrences. Instead it compares their character sequences. String Pool Heap s1 s3 s2 star star
  • 15. StringBuffer Objects of StringBuffer type are used when we have to do a lot of modification to strings of characters. StringBuffer objects are mutable. String s = “keyboard”; s.concat(“mouse”); System.out.println(s); // output ? String s = “Tom” s=s.concat(“cat”); System.out.println(s); // output ?
  • 16. StringBuffer contd. All of the StringBuffer methods operate on the content of the object invoked on.The methods won’t produce intermediate objects. StringBuffer sb = “micro”; sb.append(“soft”); System.out.println(sb); // output ?
  • 17. Important methods of StringBuffer public synchronized StringBuffer append(String s) public synchronized java.lang.StringBuffer append(char[]); public synchronized java.lang.StringBuffer append(char[], int, int); public synchronized java.lang.StringBuffer append(boolean); public synchronized java.lang.StringBuffer append(char); public synchronized java.lang.StringBuffer append(int); public synchronized java.lang.StringBuffer append(long); public synchronized java.lang.StringBuffer append(float); public synchronized java.lang.StringBuffer append(double); public synchronized java.lang.StringBuffer append(java.lang.Object); public synchronized java.lang.StringBuffer append(java.lang.StringBuffer); public java.lang.StringBuffer append(java.lang.CharSequence); StringBuffer sb = new StringBuffer(); sb.append(“5>6=“); sb.append(5>6);
  • 18. Important methods of StringBuffer public synchronized StringBuffer insert(int offset, String s) public java.lang.StringBuffer insert(int, boolean); public synchronized java.lang.StringBuffer insert(int, char); public java.lang.StringBuffer insert(int, int); public java.lang.StringBuffer insert(int, long); public java.lang.StringBuffer insert(int, float); public java.lang.StringBuffer insert(int, double); StringBuffer sb = new StringBuffer(“abcdefg"); sb.insert(2, “ "); System.out.println( sb ); // output ab cdefg public synchronized StringBuffer reverse() StringBuffer sb = new StringBuffer(“pooL”); sb.reverse(); System.out.println(sb); // output Loop public String toString()
  • 19. StringBuffer contd. StringBuffer sb1 = new StringBuffer(“microsoft”); StringBuffer sb2 = new StringBuffer(“microsoft”); System.out.println( sb1 == sb2 ); System.out.println( sb1.equals(sb2) ); Output : false false java.lang.Object java.lang.StringBuffer java.lang.AbstractStringBuilder public boolean equals(java.lang.Object);