SlideShare a Scribd company logo
1 of 20
Packages
Creating package
• include a package command as the first statement
in a Java source file.
– the classes declared within that file will belong to the
specified package.
• The package statement defines a name space in
which classes are stored.
– the class names are put into the default package, which
has no name.
package pkg;
package pkg1[.pkg2[.pkg3]];
e.g., package java.awt.image;
Access Protection
Strings
Strings
• Strings are fundamental part of all computing
languages.
• At the basic level, they are just a data structure
that can hold a series of characters
• However, strings are not implemented as a
character array in Java as in other languages.
Strings in Java
• Strings are implemented as two classes in Java
• java.lang.String provides an unchangeable
String object
• java.lang.StringBuffer provides a String object
that can be amended
Basic String Methods
• length() returns the length of the string
• toLowerCase() converts the string to lower
case
• toUpperCase() converts the string to upper
case
• replace(char, char) replaces occurrences of
one character with another character
Basic Strings continued
• Basic strings are not meant to change
frequently so there are no add or append
methods
• However the concat(String) method does
allow two strings to be concatenated together
Basic Strings continued
• Substrings of a String object can also be
accessed
• A portion of String object can be copied to a
character array using the getChars() method
• The substring() method can return substring
beginning at a specified offset
Searching a string
• Methods for searching strings
– indexOf(x) searches for the first occurrence of x
– indexOf(x, y) searches for the first occurrence of x
after the offset of y
– lastIndexOf(x) searches backwards for the first
occurrence of x
– lastIndexOf(x, y) searches backwards for the first
occurrence of x after the offset of y
Example of string search
• indexOf(x) and indexOf(x, y) can find all occurrences of a
character(s) in a string
public void paint(Graphics g) {
String str = new String("Wish You Were Here");
int count = 0;
int fromIndex = 0;
while(fromIndex != -1) {
fromIndex = str.indexOf("er", fromIndex);
if (fromIndex != -1) {
count++;
fromIndex++;
}
}
g.drawString(String.valueOf(count), 10, 10); }
Parsing Strings
• Strings can be parsed with the StringTokenizer class
• The default delimiters (space, tab, newline and carriage
return) can be used for parsing sentences
• By specifying different delimiters, a wide variety of
strings may be parsed
Parsing Strings continued
• Different default constructors are provided
– Tokenize the string based on the default
delimiters
– Tokenize the string based on a specified set of
delimiters
– Tokenize the string based on a specified set of
delimiters with a boolean flag to specify whether
the delimiters should also be returned as tokens
StringBuffer class
• The StringBuffer class is provided for strings
that need may need to be changed
• The StringBuffer class contains methods for
both inserting and appending text
• An object created as a StringBuffer can easily
be converted to an object of the String class if
needed
More on StringBuffer Class
• Conversion may be needed because many Java
library methods expect a string
• The toString() method is used for converting a
StringBuffer object to a String object
• Example of converting a StringBuffer to a String:
public void paint(Graphics g) {
StringBuffer buf = new StringBuffer("Hello, World");
g.drawString(buf.toString(), 10, 10);
}
More on StringBuffer Class
• StringBuffer objects are mutable and capacity
& length affect performance
• If the StringBuffer object needs to be
expanded during an append or insert, a new
array is created and the old data copied to it
• Use capacity() and ensureCapacity(int)
methods to minimize expansions
Length v. Capacity
• The length() method returns the length of the string
in the StringBuffer
• The capacity() method returns the total “space” in a
StringBuffer
• The ensureCapacity(int) method insures the
StringBuffer has at least the specified amount of
capacity remaining
Length v. Capacity con’t
• Examples of length() and capacity() methods
StringBuffer buf = new StringBuffer(25);// creates
StringBuffer with length 25
buf.append("13 Characters"); // appends 13
characters
int len = buf.length(); // length() returns 13
int cap = buf.capacity(); // capacity returns 25
Bibliography
• http://www.eimc.brad.ac.uk/java/tutorial/Pro
ject/4/string.htm
• http://www.scism.sbu.ac.uk/jfl/Appa/appa5.h
tml
• http://docs.rinet.ru:8080/WebJPP/contents.ht
m
• http://www.javaworld.com/javaworld/jw-10-
2002/jw-1004-java101guide.html

More Related Content

What's hot

Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python Home
 
Stl Containers
Stl ContainersStl Containers
Stl Containersppd1961
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python TutorialSimplilearn
 
Friend function
Friend functionFriend function
Friend functionzindadili
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONLalitkumar_98
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaRadhika Talaviya
 
Python Debugging Fundamentals
Python Debugging FundamentalsPython Debugging Fundamentals
Python Debugging Fundamentalscbcunc
 
Strings in C language
Strings in C languageStrings in C language
Strings in C languageP M Patil
 

What's hot (20)

Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Array in c++
Array in c++Array in c++
Array in c++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Polymorphism in Python
Polymorphism in Python Polymorphism in Python
Polymorphism in Python
 
Stl Containers
Stl ContainersStl Containers
Stl Containers
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
 
Friend function
Friend functionFriend function
Friend function
 
file handling c++
file handling c++file handling c++
file handling c++
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
C functions
C functionsC functions
C functions
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Python Debugging Fundamentals
Python Debugging FundamentalsPython Debugging Fundamentals
Python Debugging Fundamentals
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 

Similar to Strings In OOP(Object oriented programming)

Fundamental classes in java
Fundamental classes in javaFundamental classes in java
Fundamental classes in javaGaruda Trainings
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)teach4uin
 
Module-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdfModule-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdflearnEnglish51
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanKavita Ganesan
 
Strings and common operations
Strings and common operationsStrings and common operations
Strings and common operationsTurnToTech
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...gjcross
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesPrabu U
 
Module 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfModule 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfMegMeg17
 
PPT on Python - illustrating Python for BBA, B.Tech
PPT on Python - illustrating Python for BBA, B.TechPPT on Python - illustrating Python for BBA, B.Tech
PPT on Python - illustrating Python for BBA, B.Techssuser2678ab
 

Similar to Strings In OOP(Object oriented programming) (20)

StringBuffer.pptx
StringBuffer.pptxStringBuffer.pptx
StringBuffer.pptx
 
Java
JavaJava
Java
 
Fundamental classes in java
Fundamental classes in javaFundamental classes in java
Fundamental classes in java
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
 
Module-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdfModule-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdf
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
LectureNotes-04-DSA
LectureNotes-04-DSALectureNotes-04-DSA
LectureNotes-04-DSA
 
Strings and common operations
Strings and common operationsStrings and common operations
Strings and common operations
 
Strings in java
Strings in javaStrings in java
Strings in java
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and Interfaces
 
Java Day-4
Java Day-4Java Day-4
Java Day-4
 
Module 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfModule 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdf
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
Strings
StringsStrings
Strings
 
PPT on Python - illustrating Python for BBA, B.Tech
PPT on Python - illustrating Python for BBA, B.TechPPT on Python - illustrating Python for BBA, B.Tech
PPT on Python - illustrating Python for BBA, B.Tech
 
Java String
Java String Java String
Java String
 
Java Unit 2(Part 1)
Java Unit 2(Part 1)Java Unit 2(Part 1)
Java Unit 2(Part 1)
 

Recently uploaded

KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 

Recently uploaded (20)

KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 

Strings In OOP(Object oriented programming)

  • 2. Creating package • include a package command as the first statement in a Java source file. – the classes declared within that file will belong to the specified package. • The package statement defines a name space in which classes are stored. – the class names are put into the default package, which has no name. package pkg; package pkg1[.pkg2[.pkg3]]; e.g., package java.awt.image;
  • 5. Strings • Strings are fundamental part of all computing languages. • At the basic level, they are just a data structure that can hold a series of characters • However, strings are not implemented as a character array in Java as in other languages.
  • 6. Strings in Java • Strings are implemented as two classes in Java • java.lang.String provides an unchangeable String object • java.lang.StringBuffer provides a String object that can be amended
  • 7. Basic String Methods • length() returns the length of the string • toLowerCase() converts the string to lower case • toUpperCase() converts the string to upper case • replace(char, char) replaces occurrences of one character with another character
  • 8. Basic Strings continued • Basic strings are not meant to change frequently so there are no add or append methods • However the concat(String) method does allow two strings to be concatenated together
  • 9. Basic Strings continued • Substrings of a String object can also be accessed • A portion of String object can be copied to a character array using the getChars() method • The substring() method can return substring beginning at a specified offset
  • 10. Searching a string • Methods for searching strings – indexOf(x) searches for the first occurrence of x – indexOf(x, y) searches for the first occurrence of x after the offset of y – lastIndexOf(x) searches backwards for the first occurrence of x – lastIndexOf(x, y) searches backwards for the first occurrence of x after the offset of y
  • 11. Example of string search • indexOf(x) and indexOf(x, y) can find all occurrences of a character(s) in a string public void paint(Graphics g) { String str = new String("Wish You Were Here"); int count = 0; int fromIndex = 0; while(fromIndex != -1) { fromIndex = str.indexOf("er", fromIndex); if (fromIndex != -1) { count++; fromIndex++; } } g.drawString(String.valueOf(count), 10, 10); }
  • 12.
  • 13. Parsing Strings • Strings can be parsed with the StringTokenizer class • The default delimiters (space, tab, newline and carriage return) can be used for parsing sentences • By specifying different delimiters, a wide variety of strings may be parsed
  • 14. Parsing Strings continued • Different default constructors are provided – Tokenize the string based on the default delimiters – Tokenize the string based on a specified set of delimiters – Tokenize the string based on a specified set of delimiters with a boolean flag to specify whether the delimiters should also be returned as tokens
  • 15. StringBuffer class • The StringBuffer class is provided for strings that need may need to be changed • The StringBuffer class contains methods for both inserting and appending text • An object created as a StringBuffer can easily be converted to an object of the String class if needed
  • 16. More on StringBuffer Class • Conversion may be needed because many Java library methods expect a string • The toString() method is used for converting a StringBuffer object to a String object • Example of converting a StringBuffer to a String: public void paint(Graphics g) { StringBuffer buf = new StringBuffer("Hello, World"); g.drawString(buf.toString(), 10, 10); }
  • 17. More on StringBuffer Class • StringBuffer objects are mutable and capacity & length affect performance • If the StringBuffer object needs to be expanded during an append or insert, a new array is created and the old data copied to it • Use capacity() and ensureCapacity(int) methods to minimize expansions
  • 18. Length v. Capacity • The length() method returns the length of the string in the StringBuffer • The capacity() method returns the total “space” in a StringBuffer • The ensureCapacity(int) method insures the StringBuffer has at least the specified amount of capacity remaining
  • 19. Length v. Capacity con’t • Examples of length() and capacity() methods StringBuffer buf = new StringBuffer(25);// creates StringBuffer with length 25 buf.append("13 Characters"); // appends 13 characters int len = buf.length(); // length() returns 13 int cap = buf.capacity(); // capacity returns 25
  • 20. Bibliography • http://www.eimc.brad.ac.uk/java/tutorial/Pro ject/4/string.htm • http://www.scism.sbu.ac.uk/jfl/Appa/appa5.h tml • http://docs.rinet.ru:8080/WebJPP/contents.ht m • http://www.javaworld.com/javaworld/jw-10- 2002/jw-1004-java101guide.html