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)

Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair
 
String in java
String in javaString in java
String in java
Ideal Eyes Business College
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
shanmuga rajan
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
lalithambiga kamaraj
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Strings in java
Strings in javaStrings in java
Strings in java
Kuppusamy P
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Exception handling
Exception handlingException handling
Exception handling
PhD Research Scholar
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
myrajendra
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
Hitesh Kumar
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
SIVASHANKARIRAJAN
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
Ravi_Kant_Sahu
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
Madishetty Prathibha
 

Viewers also liked (20)

Strings
StringsStrings
Strings
naslin prestilda
 
Arrays string handling java packages
Arrays string handling java packagesArrays string handling java packages
Arrays string handling java packages
Sardar Alam
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
Ravi Kant Sahu
 
String handling session 5
String handling session 5String handling session 5
String handling session 5
Raja Sekhar
 
April 2014
April 2014April 2014
April 2014
Siddharth Pereira
 
Java String
Java String Java String
Java String
SATYAM SHRIVASTAV
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
teach4uin
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part V
Hari Christian
 
Indian Property Lawyers .com - How It Works
Indian Property Lawyers .com - How It WorksIndian Property Lawyers .com - How It Works
Indian Property Lawyers .com - How It Works
buzzfactory
 
Java strings
Java   stringsJava   strings
Java strings
Mohammed Sikander
 
Strings and common operations
Strings and common operationsStrings and common operations
Strings and common operations
TurnToTech
 
Java: strings e arrays
Java: strings e arraysJava: strings e arrays
Java: strings e arrays
Arthur Emanuel
 
Java file
Java fileJava file
Java file
sonnetdp
 
String java
String javaString java
String java
774474
 
String handling in_java
String handling in_javaString handling in_java
String handling in_java
774474
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
Open and Close Door ppt
 Open and Close Door ppt Open and Close Door ppt
Open and Close Door ppt
Devyani Vaidya
 
Java 06 Strings Arrays
Java 06 Strings ArraysJava 06 Strings Arrays
Java 06 Strings Arrays
Regis Magalhães
 
Indian food jnv bhiwani
Indian food jnv bhiwaniIndian food jnv bhiwani
Indian food jnv bhiwani
vermanamita
 
Arrays string handling java packages
Arrays string handling java packagesArrays string handling java packages
Arrays string handling java packages
Sardar Alam
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
Ravi Kant Sahu
 
String handling session 5
String handling session 5String handling session 5
String handling session 5
Raja Sekhar
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
teach4uin
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
JavabynataraJ
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part V
Hari Christian
 
Indian Property Lawyers .com - How It Works
Indian Property Lawyers .com - How It WorksIndian Property Lawyers .com - How It Works
Indian Property Lawyers .com - How It Works
buzzfactory
 
Strings and common operations
Strings and common operationsStrings and common operations
Strings and common operations
TurnToTech
 
Java: strings e arrays
Java: strings e arraysJava: strings e arrays
Java: strings e arrays
Arthur Emanuel
 
String java
String javaString java
String java
774474
 
String handling in_java
String handling in_javaString handling in_java
String handling in_java
774474
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
Open and Close Door ppt
 Open and Close Door ppt Open and Close Door ppt
Open and Close Door ppt
Devyani Vaidya
 
Indian food jnv bhiwani
Indian food jnv bhiwaniIndian food jnv bhiwani
Indian food jnv bhiwani
vermanamita
 
Ad

Similar to Java string handling (20)

javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string class
fedcoordinator
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
String handling
String handlingString handling
String handling
ssuser20c32b
 
Java Strings methods and operations.ppt
Java Strings  methods and operations.pptJava Strings  methods and operations.ppt
Java Strings methods and operations.ppt
JyothiAmpally
 
Day_5.1.pptx
Day_5.1.pptxDay_5.1.pptx
Day_5.1.pptx
ishasharma835109
 
String.ppt
String.pptString.ppt
String.ppt
ajeela mushtaq
 
package
packagepackage
package
sweetysweety8
 
3.7_StringBuilder.pdf
3.7_StringBuilder.pdf3.7_StringBuilder.pdf
3.7_StringBuilder.pdf
Ananthi68
 
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
21CS642 Module 3 Strings PPT.pptx VI SEM CSE21CS642 Module 3 Strings PPT.pptx VI SEM CSE
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIs
İbrahim Kürce
 
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVASTRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
Java
JavaJava
Java
JahnaviBhagat
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptx
VeenaNaik23
 
String.pptx
String.pptxString.pptx
String.pptx
RanjithKumar742256
 
07slide
07slide07slide
07slide
Aboudi Sabbah
 
Module 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfModule 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdf
MegMeg17
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptxDOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
meganath16032003
 
M C6java7
M C6java7M C6java7
M C6java7
mbruggen
 
javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string class
fedcoordinator
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
Java Strings methods and operations.ppt
Java Strings  methods and operations.pptJava Strings  methods and operations.ppt
Java Strings methods and operations.ppt
JyothiAmpally
 
3.7_StringBuilder.pdf
3.7_StringBuilder.pdf3.7_StringBuilder.pdf
3.7_StringBuilder.pdf
Ananthi68
 
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
21CS642 Module 3 Strings PPT.pptx VI SEM CSE21CS642 Module 3 Strings PPT.pptx VI SEM CSE
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIs
İbrahim Kürce
 
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVASTRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptx
VeenaNaik23
 
Module 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfModule 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdf
MegMeg17
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptxDOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
meganath16032003
 
Ad

Recently uploaded (20)

iOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod KumariOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod Kumar
Pramod Kumar
 
Bonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdfBonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdf
Herond Labs
 
Generative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its ApplicationsGenerative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its Applications
SandeepKS52
 
Scaling FME Flow on Demand with Kubernetes: A Case Study At Cadac Group SaaS ...
Scaling FME Flow on Demand with Kubernetes: A Case Study At Cadac Group SaaS ...Scaling FME Flow on Demand with Kubernetes: A Case Study At Cadac Group SaaS ...
Scaling FME Flow on Demand with Kubernetes: A Case Study At Cadac Group SaaS ...
Safe Software
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdfHow to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
QuickBooks Training
 
Top 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdfTop 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdf
Trackobit
 
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
SheenBrisals
 
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The SequelMarketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
BradBedford3
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdfThe Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
Varsha Nayak
 
Best Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small BusinessesBest Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small Businesses
TheTelephony
 
Providing Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better DataProviding Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better Data
Safe Software
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA TechnologiesAI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI SearchAgentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
 
Automating Map Production With FME and Python
Automating Map Production With FME and PythonAutomating Map Production With FME and Python
Automating Map Production With FME and Python
Safe Software
 
IBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - IntroductionIBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - Introduction
Gaurav Sharma
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlowDevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentricIntegration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Natan Silnitsky
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FMEIntegrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
iOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod KumariOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod Kumar
Pramod Kumar
 
Bonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdfBonk coin airdrop_ Everything You Need to Know.pdf
Bonk coin airdrop_ Everything You Need to Know.pdf
Herond Labs
 
Generative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its ApplicationsGenerative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its Applications
SandeepKS52
 
Scaling FME Flow on Demand with Kubernetes: A Case Study At Cadac Group SaaS ...
Scaling FME Flow on Demand with Kubernetes: A Case Study At Cadac Group SaaS ...Scaling FME Flow on Demand with Kubernetes: A Case Study At Cadac Group SaaS ...
Scaling FME Flow on Demand with Kubernetes: A Case Study At Cadac Group SaaS ...
Safe Software
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdfHow to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
How to Generate Financial Statements in QuickBooks Like a Pro (1).pdf
QuickBooks Training
 
Top 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdfTop 11 Fleet Management Software Providers in 2025 (2).pdf
Top 11 Fleet Management Software Providers in 2025 (2).pdf
Trackobit
 
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
Eliminate the complexities of Event-Driven Architecture with Domain-Driven De...
SheenBrisals
 
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The SequelMarketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
BradBedford3
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdfThe Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
Varsha Nayak
 
Best Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small BusinessesBest Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small Businesses
TheTelephony
 
Providing Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better DataProviding Better Biodiversity Through Better Data
Providing Better Biodiversity Through Better Data
Safe Software
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA TechnologiesAI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI SearchAgentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
 
Automating Map Production With FME and Python
Automating Map Production With FME and PythonAutomating Map Production With FME and Python
Automating Map Production With FME and Python
Safe Software
 
IBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - IntroductionIBM Rational Unified Process For Software Engineering - Introduction
IBM Rational Unified Process For Software Engineering - Introduction
Gaurav Sharma
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlowDevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentricIntegration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Integration Ignited Redefining Event-Driven Architecture at Wix - EventCentric
Natan Silnitsky
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FMEIntegrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 

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.