SlideShare a Scribd company logo
17
Most read
18
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

What's hot (20)

DOC
Error Handling in Compiler Design.typeso
BhuvaneswariR27
 
PPT
Python modules
Shanmugapriya Dineshbabu
 
PPTX
Array sorting
ALI RAZA
 
PPTX
What is Gray Code?
Mukesh Tekwani
 
PPTX
Code Converters & Parity Checker
.AIR UNIVERSITY ISLAMABAD
 
PPTX
Method overloading
Lovely Professional University
 
PPTX
Scope rules : local and global variables
sangrampatil81
 
PPTX
Pointers in c language
Tanmay Modi
 
PDF
Python strings
Mohammed Sikander
 
PDF
Tuples in Python
DPS Ranipur Haridwar UK
 
PDF
Strings in Python
nitamhaske
 
PDF
03 Linear Arrays Memory Representations .pdf
KkSingh64
 
PPT
Comments in C Programming
programming9
 
PPTX
Doubly linked list (animated)
DivyeshKumar Jagatiya
 
PDF
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
PPTX
Circular link list.ppt
Tirthika Bandi
 
PPTX
Strings in Python
Amisha Narsingani
 
PPT
Python Built-in Functions and Use cases
Srajan Mor
 
PPTX
Modules in Python Programming
sambitmandal
 
Error Handling in Compiler Design.typeso
BhuvaneswariR27
 
Python modules
Shanmugapriya Dineshbabu
 
Array sorting
ALI RAZA
 
What is Gray Code?
Mukesh Tekwani
 
Code Converters & Parity Checker
.AIR UNIVERSITY ISLAMABAD
 
Method overloading
Lovely Professional University
 
Scope rules : local and global variables
sangrampatil81
 
Pointers in c language
Tanmay Modi
 
Python strings
Mohammed Sikander
 
Tuples in Python
DPS Ranipur Haridwar UK
 
Strings in Python
nitamhaske
 
03 Linear Arrays Memory Representations .pdf
KkSingh64
 
Comments in C Programming
programming9
 
Doubly linked list (animated)
DivyeshKumar Jagatiya
 
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
Circular link list.ppt
Tirthika Bandi
 
Strings in Python
Amisha Narsingani
 
Python Built-in Functions and Use cases
Srajan Mor
 
Modules in Python Programming
sambitmandal
 

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

PPT
Java Strings methods and operations.ppt
JyothiAmpally
 
PPTX
StringBuffer.pptx
meenakshi pareek
 
PPTX
Java
JahnaviBhagat
 
DOCX
Fundamental classes in java
Garuda Trainings
 
PPTX
L14 string handling(string buffer class)
teach4uin
 
PPTX
String in JAVA --------------------------
2003sayanch
 
PDF
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
PPT
Strings power point in detail with examples
rabiyanaseer1
 
PPTX
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
 
PPTX
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
PPTX
Strings in Java
Abhilash Nair
 
PDF
LectureNotes-04-DSA
Haitham El-Ghareeb
 
PPTX
Strings and common operations
TurnToTech
 
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
PDF
Strings in java
Kuppusamy P
 
PPTX
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
PPTX
Java string handling
GaneshKumarKanthiah
 
PDF
Processing data with Python, using standard library modules you (probably) ne...
gjcross
 
PPTX
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
PDF
Java Day-4
People Strategists
 
Java Strings methods and operations.ppt
JyothiAmpally
 
StringBuffer.pptx
meenakshi pareek
 
Fundamental classes in java
Garuda Trainings
 
L14 string handling(string buffer class)
teach4uin
 
String in JAVA --------------------------
2003sayanch
 
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
Strings power point in detail with examples
rabiyanaseer1
 
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
 
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
Strings in Java
Abhilash Nair
 
LectureNotes-04-DSA
Haitham El-Ghareeb
 
Strings and common operations
TurnToTech
 
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
Strings in java
Kuppusamy P
 
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
Java string handling
GaneshKumarKanthiah
 
Processing data with Python, using standard library modules you (probably) ne...
gjcross
 
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
Java Day-4
People Strategists
 
Ad

Recently uploaded (20)

PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Controller Request and Response in Odoo18
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
epi editorial commitee meeting presentation
MIPLM
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Ad

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