SlideShare a Scribd company logo
Java : Strings
Salman Khan
ID: 1309004
Session: 2013-14
Dept. of ICT
Comilla University
Outline
▪ What is string ?
▪ How to create String object?
▪ Some important operations of strings
▪ Some important string class methods
▪ StringBuffer class & StringBuilder class
▪ Difference between String and StringBuffer
▪ Difference between StringBuffer and StringBuilder
What is String ?
▪ Strings, which are widely used in Java
programming, are a sequence of characters. In
Java programming language, strings are
treated as objects.
▪ The Java platform provides the String class to
create and manipulate strings.
Important Notes about String
▪ String – Object or Primitive?
- Strings could be considered a primitive type in Java, but in fact they are not.
As a String is actually made up of an array of char primitives.
▪ String objects are immutable!
- That means once a string object is created it cannot be altered. For mutable
string, you can use StringBuffer and StringBuilder classes.
[An object whose state cannot be changed after it is created is known as an
Immutable object. String, Integer, Byte, Short, Float, Double and all other wrapper
class's objects are immutable.]
How to create String object?
There are two ways to create String object:
1.By string literal
For Example:
String s="welcome";
2.By new keyword
For Example:
String s=new String("Welcome");
Java String Example
Output :
java
strings
example
Some important operations of strings
▪ String Concatenation
▪ String Comparison
▪ Substring
▪ Length of String etc.
String Concatenation
▪ There are 2 methods to concatenate two or more string.
Concatenate
two or more
string.
Using
concat()
method
Using +
operator
String Comparison
▪ String comparison can be done in 3 ways.
String
Comparison
Using
equals()
method
Using ==
operator
By
CompareTo()
method
String Comparison Example
Substring in Java
▪ A part of string is called substring. In other words, substring is a subset of another string.
▪ We can get substring from the given string object by one of the two methods:
1. public String substring(int startIndex):
2. public String substring(int startIndex, int endIndex):
Length of String
▪ The java string length() method finds the length of the string. It returns count of total
number of characters.
Some more string class methods
▪ The java.lang.String class provides a lot of methods to work on string. By the help of
these methods, we can perform operations on strings.
▪ Here we will learn about the following methods :
1. charAt()
2. contains()
3. getChars()
4. indexOf()
5. replace()
6. toCharArray()
7. toLowerCase()
8. toUpperCase()
charAt() & contains()
 charAt() = returns a char value at the given index number.The index number starts from 0.
 contains() = searches the sequence of characters in this string. It returns true if sequence of char values
are found in this string otherwise returns false.
getChars()
 getChars() = copies the content of this string into specified char array.There are 4
arguments passed in getChars() method. It throws StringIndexOutOfBoundsException
if beginIndex is greater than endIndex.
indexOf()
 indexOf() = returns index
of given character value
or substring. If it is not
found, it returns -1. The
index counter starts from
zero.
replace()
 replace() = returns a string replacing all the old char or CharSequence to new char or
CharSequence.
toCharArray()
 toCharArray() = converts this string into character array.
toLowerCase() & toUpperCase()
 toLowerCase() = returns the string in lowercase letter. In other words, it converts all
characters of the string into lower case letter.
 toUpperCase() = returns the string in uppercase letter. In other words, it converts all
characters of the string into upper case letter.
Java StringBuffer class
▪ Java StringBuffer class is used to created mutable (modifiable) string.
▪ Important Constructors of StringBuffer class:
 StringBuffer(): creates an empty string buffer with the initial capacity of 16.
 StringBuffer(String str): creates a string buffer with the specified string.
 StringBuffer(int capacity): creates an empty string buffer with the specified capacity as length.
▪ Important methods of StringBuffer class:
 append(String s): is used to append the specified string with this string.The append() method is
overloaded like append(char), append(boolean), append(int), append(float), append(double) etc.
 insert(int offset, String s): is used to insert the specified string with this string at the specified position.
The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int,
float), insert(int, double) etc.
 replace(int startIndex, int endIndex, String str): is used to replace the string from specified startIndex
and endIndex.
 delete(int startIndex, int endIndex): is used to delete the string from specified startIndex and endIndex.
 reverse(): is used to reverse the string.
 capacity(): is used to return the current capacity.
Java StringBuilder class
▪ Java StringBuilder class is used to create mutable (modifiable)
string.The Java StringBuilder class is same as StringBuffer class
except that it is non-synchronized.
Example of using StringBuffer & StringBuilder class
Difference between String and StringBuffer
No. String StringBuffer
1) String class is immutable. StringBuffer class is mutable.
2) String is slow and consumes more memory when
you concat too many strings because every time
it creates new instance.
StringBuffer is fast and consumes less memory
when you cancat strings.
3) String class overrides the equals() method of
Object class. So you can compare the contents of
two strings by equals() method.
StringBuffer class doesn't override the equals()
method of Object class.
Difference between StringBuffer and StringBuilder
No. StringBuffer StringBuilder
1) StringBuffer is synchronized i.e. thread safe. It
means two threads can't call the methods of
StringBuffer simultaneously.
StringBuilder is non-synchronized i.e. not
thread safe. It means two threads can call the
methods of StringBuilder simultaneously.
2) StringBuffer is less efficient than StringBuilder. StringBuilder is more efficient than
StringBuffer.
Thanks to all

More Related Content

What's hot (20)

PPT
Abstract class in java
Lovely Professional University
 
PDF
Arrays in Java
Naz Abdalla
 
PPS
String and string buffer
kamal kotecha
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPS
Wrapper class
kamal kotecha
 
PPTX
Type casting in java
Farooq Baloch
 
PPT
Java interfaces
Raja Sekhar
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Interface in java
PhD Research Scholar
 
PPTX
JAVA AWT
shanmuga rajan
 
PPTX
Arrays in java
Arzath Areeff
 
PPTX
Polymorphism in java
Elizabeth alexander
 
PPTX
Java String
SATYAM SHRIVASTAV
 
PPTX
Threads in JAVA
Haldia Institute of Technology
 
PDF
Class and Objects in Java
Spotle.ai
 
PPTX
Java Data Types
Spotle.ai
 
PDF
Threads concept in java
Muthukumaran Subramanian
 
PPTX
Static keyword ppt
Vinod Kumar
 
PPTX
Arrays in Java
Abhilash Nair
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
Abstract class in java
Lovely Professional University
 
Arrays in Java
Naz Abdalla
 
String and string buffer
kamal kotecha
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Wrapper class
kamal kotecha
 
Type casting in java
Farooq Baloch
 
Java interfaces
Raja Sekhar
 
Constructor in java
Pavith Gunasekara
 
Interface in java
PhD Research Scholar
 
JAVA AWT
shanmuga rajan
 
Arrays in java
Arzath Areeff
 
Polymorphism in java
Elizabeth alexander
 
Java String
SATYAM SHRIVASTAV
 
Class and Objects in Java
Spotle.ai
 
Java Data Types
Spotle.ai
 
Threads concept in java
Muthukumaran Subramanian
 
Static keyword ppt
Vinod Kumar
 
Arrays in Java
Abhilash Nair
 
Exception Handling in JAVA
SURIT DATTA
 

Viewers also liked (20)

PPT
Strings
naslin prestilda
 
PDF
Arrays string handling java packages
Sardar Alam
 
PDF
String handling(string class)
Ravi Kant Sahu
 
PPS
Java Exception handling
kamal kotecha
 
PPT
String handling session 5
Raja Sekhar
 
PDF
April 2014
Siddharth Pereira
 
PPTX
L13 string handling(string class)
teach4uin
 
PDF
Java exception handling ppt
JavabynataraJ
 
PPTX
05 Java Language And OOP Part V
Hari Christian
 
PPTX
Indian Property Lawyers .com - How It Works
buzzfactory
 
PPTX
Java strings
Mohammed Sikander
 
PPTX
String in java
Ideal Eyes Business College
 
PPTX
Strings and common operations
TurnToTech
 
PPTX
Java: strings e arrays
Arthur Emanuel
 
PPTX
Java file
sonnetdp
 
PPTX
String java
774474
 
PPTX
String handling in_java
774474
 
PPTX
L21 io streams
teach4uin
 
PPTX
Open and Close Door ppt
Devyani Vaidya
 
ODP
Java 06 Strings Arrays
Regis Magalhães
 
Arrays string handling java packages
Sardar Alam
 
String handling(string class)
Ravi Kant Sahu
 
Java Exception handling
kamal kotecha
 
String handling session 5
Raja Sekhar
 
April 2014
Siddharth Pereira
 
L13 string handling(string class)
teach4uin
 
Java exception handling ppt
JavabynataraJ
 
05 Java Language And OOP Part V
Hari Christian
 
Indian Property Lawyers .com - How It Works
buzzfactory
 
Java strings
Mohammed Sikander
 
Strings and common operations
TurnToTech
 
Java: strings e arrays
Arthur Emanuel
 
Java file
sonnetdp
 
String java
774474
 
String handling in_java
774474
 
L21 io streams
teach4uin
 
Open and Close Door ppt
Devyani Vaidya
 
Java 06 Strings Arrays
Regis Magalhães
 
Ad

Similar to Java string handling (20)

PPTX
javastringexample problems using string class
fedcoordinator
 
PPTX
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
PPT
Java Strings methods and operations.ppt
JyothiAmpally
 
PPTX
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
PDF
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
PPTX
Day_5.1.pptx
ishasharma835109
 
PPTX
Java string , string buffer and wrapper class
SimoniShah6
 
PPTX
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
meganath16032003
 
PPT
Text processing
Icancode
 
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
PPT
Strings power point in detail with examples
rabiyanaseer1
 
PPTX
Java string handling
GaneshKumarKanthiah
 
PDF
String.ppt
ajeela mushtaq
 
PPT
String and string manipulation
Shahjahan Samoon
 
PPTX
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
PPSX
String and string manipulation x
Shahjahan Samoon
 
PPTX
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
PPTX
Java Strings
RaBiya Chaudhry
 
PPTX
Java String Handling
Infoviaan Technologies
 
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
javastringexample problems using string class
fedcoordinator
 
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
Java Strings methods and operations.ppt
JyothiAmpally
 
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
Day_5.1.pptx
ishasharma835109
 
Java string , string buffer and wrapper class
SimoniShah6
 
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
meganath16032003
 
Text processing
Icancode
 
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
Strings power point in detail with examples
rabiyanaseer1
 
Java string handling
GaneshKumarKanthiah
 
String.ppt
ajeela mushtaq
 
String and string manipulation
Shahjahan Samoon
 
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
String and string manipulation x
Shahjahan Samoon
 
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
Java Strings
RaBiya Chaudhry
 
Java String Handling
Infoviaan Technologies
 
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
Ad

Recently uploaded (20)

PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PDF
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Executive Business Intelligence Dashboards
vandeslie24
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 

Java string handling

  • 1. Java : Strings Salman Khan ID: 1309004 Session: 2013-14 Dept. of ICT Comilla University
  • 2. Outline ▪ What is string ? ▪ How to create String object? ▪ Some important operations of strings ▪ Some important string class methods ▪ StringBuffer class & StringBuilder class ▪ Difference between String and StringBuffer ▪ Difference between StringBuffer and StringBuilder
  • 3. What is String ? ▪ Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects. ▪ The Java platform provides the String class to create and manipulate strings.
  • 4. Important Notes about String ▪ String – Object or Primitive? - Strings could be considered a primitive type in Java, but in fact they are not. As a String is actually made up of an array of char primitives. ▪ String objects are immutable! - That means once a string object is created it cannot be altered. For mutable string, you can use StringBuffer and StringBuilder classes. [An object whose state cannot be changed after it is created is known as an Immutable object. String, Integer, Byte, Short, Float, Double and all other wrapper class's objects are immutable.]
  • 5. How to create String object? There are two ways to create String object: 1.By string literal For Example: String s="welcome"; 2.By new keyword For Example: String s=new String("Welcome");
  • 6. Java String Example Output : java strings example
  • 7. Some important operations of strings ▪ String Concatenation ▪ String Comparison ▪ Substring ▪ Length of String etc.
  • 8. String Concatenation ▪ There are 2 methods to concatenate two or more string. Concatenate two or more string. Using concat() method Using + operator
  • 9. String Comparison ▪ String comparison can be done in 3 ways. String Comparison Using equals() method Using == operator By CompareTo() method
  • 11. Substring in Java ▪ A part of string is called substring. In other words, substring is a subset of another string. ▪ We can get substring from the given string object by one of the two methods: 1. public String substring(int startIndex): 2. public String substring(int startIndex, int endIndex):
  • 12. Length of String ▪ The java string length() method finds the length of the string. It returns count of total number of characters.
  • 13. Some more string class methods ▪ The java.lang.String class provides a lot of methods to work on string. By the help of these methods, we can perform operations on strings. ▪ Here we will learn about the following methods : 1. charAt() 2. contains() 3. getChars() 4. indexOf() 5. replace() 6. toCharArray() 7. toLowerCase() 8. toUpperCase()
  • 14. charAt() & contains()  charAt() = returns a char value at the given index number.The index number starts from 0.  contains() = searches the sequence of characters in this string. It returns true if sequence of char values are found in this string otherwise returns false.
  • 15. getChars()  getChars() = copies the content of this string into specified char array.There are 4 arguments passed in getChars() method. It throws StringIndexOutOfBoundsException if beginIndex is greater than endIndex.
  • 16. indexOf()  indexOf() = returns index of given character value or substring. If it is not found, it returns -1. The index counter starts from zero.
  • 17. replace()  replace() = returns a string replacing all the old char or CharSequence to new char or CharSequence.
  • 18. toCharArray()  toCharArray() = converts this string into character array.
  • 19. toLowerCase() & toUpperCase()  toLowerCase() = returns the string in lowercase letter. In other words, it converts all characters of the string into lower case letter.  toUpperCase() = returns the string in uppercase letter. In other words, it converts all characters of the string into upper case letter.
  • 20. Java StringBuffer class ▪ Java StringBuffer class is used to created mutable (modifiable) string. ▪ Important Constructors of StringBuffer class:  StringBuffer(): creates an empty string buffer with the initial capacity of 16.  StringBuffer(String str): creates a string buffer with the specified string.  StringBuffer(int capacity): creates an empty string buffer with the specified capacity as length. ▪ Important methods of StringBuffer class:  append(String s): is used to append the specified string with this string.The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc.  insert(int offset, String s): is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc.  replace(int startIndex, int endIndex, String str): is used to replace the string from specified startIndex and endIndex.  delete(int startIndex, int endIndex): is used to delete the string from specified startIndex and endIndex.  reverse(): is used to reverse the string.  capacity(): is used to return the current capacity.
  • 21. Java StringBuilder class ▪ Java StringBuilder class is used to create mutable (modifiable) string.The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized.
  • 22. Example of using StringBuffer & StringBuilder class
  • 23. Difference between String and StringBuffer No. String StringBuffer 1) String class is immutable. StringBuffer class is mutable. 2) String is slow and consumes more memory when you concat too many strings because every time it creates new instance. StringBuffer is fast and consumes less memory when you cancat strings. 3) String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method. StringBuffer class doesn't override the equals() method of Object class.
  • 24. Difference between StringBuffer and StringBuilder No. StringBuffer StringBuilder 1) StringBuffer is synchronized i.e. thread safe. It means two threads can't call the methods of StringBuffer simultaneously. StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously. 2) StringBuffer is less efficient than StringBuilder. StringBuilder is more efficient than StringBuffer.