SlideShare a Scribd company logo
6
Most read
7
Most read
19
Most read
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); }
Strings In OOP(Object oriented programming)
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

DOC
AUTOMATA THEORY - SHORT NOTES
PPTX
Polymorphism presentation in java
PPTX
Implementation of lexical analyser
PPTX
Scope of parallelism
PDF
Object Oriented Paradigm
PDF
Bottom up parser
PPT
Radial Basis Function and Splines.
PPTX
IEEE STANDARDS 802.3,802.4,802.5
AUTOMATA THEORY - SHORT NOTES
Polymorphism presentation in java
Implementation of lexical analyser
Scope of parallelism
Object Oriented Paradigm
Bottom up parser
Radial Basis Function and Splines.
IEEE STANDARDS 802.3,802.4,802.5

What's hot (20)

PPT
C++ classes tutorials
PPTX
knowledge representation using rules
PPT
Command line arguments.21
PDF
Dbms 14: Relational Calculus
PPTX
Single source Shortest path algorithm with example
PPTX
The role of the parser and Error recovery strategies ppt in compiler design
PDF
Problem Characteristics in Artificial Intelligence
PPTX
Directed Acyclic Graph Representation of basic blocks
PDF
Class and Objects in Java
PPTX
Scope - Static and Dynamic
PPTX
Database Connectivity in PHP
PPTX
Relationship Among Token, Lexeme & Pattern
PPT
POST’s CORRESPONDENCE PROBLEM
PPTX
[OOP - Lec 18] Static Data Member
PDF
Target language in compiler design
DOCX
Graphics User Interface Lab Manual
PPT
Bottom - Up Parsing
PPTX
Recognition-of-tokens
PPTX
Introduction to Dynamic Programming, Principle of Optimality
PPTX
weak slot and filler structure
C++ classes tutorials
knowledge representation using rules
Command line arguments.21
Dbms 14: Relational Calculus
Single source Shortest path algorithm with example
The role of the parser and Error recovery strategies ppt in compiler design
Problem Characteristics in Artificial Intelligence
Directed Acyclic Graph Representation of basic blocks
Class and Objects in Java
Scope - Static and Dynamic
Database Connectivity in PHP
Relationship Among Token, Lexeme & Pattern
POST’s CORRESPONDENCE PROBLEM
[OOP - Lec 18] Static Data Member
Target language in compiler design
Graphics User Interface Lab Manual
Bottom - Up Parsing
Recognition-of-tokens
Introduction to Dynamic Programming, Principle of Optimality
weak slot and filler structure
Ad

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

PPTX
Java string handling
PPTX
javastringexample problems using string class
PPTX
Strings in Java
PPTX
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
PPTX
L13 string handling(string class)
PPTX
Java Strings
PPTX
String Handling, Inheritance, Packages and Interfaces
PPTX
Java String
PPTX
In the given example only one object will be created. Firstly JVM will not fi...
PPTX
Day_5.1.pptx
PPT
Java Strings methods and operations.ppt
PPT
String and string manipulation
PPSX
String and string manipulation x
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
PDF
String.ppt
PPTX
Java string , string buffer and wrapper class
PPT
PPTX
Java string handling
PDF
Module-1 Strings Handling.ppt.pdf
PPTX
STRING FUNCTIONS IN JAVA BY N SARATH KUMAR
Java string handling
javastringexample problems using string class
Strings in Java
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
L13 string handling(string class)
Java Strings
String Handling, Inheritance, Packages and Interfaces
Java String
In the given example only one object will be created. Firstly JVM will not fi...
Day_5.1.pptx
Java Strings methods and operations.ppt
String and string manipulation
String and string manipulation x
Charcater and Strings.ppt Charcater and Strings.ppt
String.ppt
Java string , string buffer and wrapper class
Java string handling
Module-1 Strings Handling.ppt.pdf
STRING FUNCTIONS IN JAVA BY N SARATH KUMAR
Ad

Recently uploaded (20)

PDF
English Language Teaching from Post-.pdf
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Onica Farming 24rsclub profitable farm business
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Cell Structure & Organelles in detailed.
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
English Language Teaching from Post-.pdf
Cardiovascular Pharmacology for pharmacy students.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pharma ospi slides which help in ospi learning
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Open Quiz Monsoon Mind Game Prelims.pptx
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
Renaissance Architecture: A Journey from Faith to Humanism
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Onica Farming 24rsclub profitable farm business
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Insiders guide to clinical Medicine.pdf
Anesthesia in Laparoscopic Surgery in India
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Cell Structure & Organelles in detailed.
Pharmacology of Heart Failure /Pharmacotherapy of CHF
human mycosis Human fungal infections are called human mycosis..pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
How to Manage Starshipit in Odoo 18 - Odoo Slides

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); }
  • 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