SlideShare a Scribd company logo
Java : Strings
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 andStringBuffer
▪ Difference between StringBuffer andStringBuilder
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=newString("Welcome");
Java StringExample
Output :
java
strings
example
javastringexample problems using string class
javastringexample problems using string class
Some important operations ofstrings
▪ StringConcatenation
▪ StringComparison
▪ Substring
▪ Length ofString
▪ String copy etc.
StringConcatenation
▪ There are 2 methods to concatenate two or more string.
Concatenate
two or more
string.
Using
concat()
method
Using +
operator
StringComparison
▪ 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(intstartIndex):
2. public String substring(int startIndex, int endIndex):
Length ofString
finds the length of the string. It returns count of total
▪ The java string length() method
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.
▪ ImportantConstructors 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.TheJavaStringBuilder 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 Objectclass.
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 thanStringBuilder. StringBuilder
StringBuffer.
is more efficient than
Thanks to all

More Related Content

Similar to javastringexample problems using string class (20)

Strings in Java
Strings in JavaStrings in Java
Strings in Java
Abhilash Nair
 
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
 
Text processing
Text processingText processing
Text processing
Icancode
 
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
 
Charcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.pptCharcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
Strings power point in detail with examples
Strings power point in detail with examplesStrings power point in detail with examples
Strings power point in detail with examples
rabiyanaseer1
 
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptxDOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
meganath16032003
 
Java Strings
Java StringsJava Strings
Java Strings
RaBiya Chaudhry
 
Strings
StringsStrings
Strings
naslin prestilda
 
Java string handling
Java string handlingJava string handling
Java string handling
GaneshKumarKanthiah
 
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
 
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
Kavita Ganesan
 
3.7_StringBuilder.pdf
3.7_StringBuilder.pdf3.7_StringBuilder.pdf
3.7_StringBuilder.pdf
Ananthi68
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
Shahjahan Samoon
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
Infoviaan Technologies
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
Danial Virk
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation x
Shahjahan Samoon
 
Java
JavaJava
Java
JahnaviBhagat
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
OUM SAOKOSAL
 
Text processing
Text processingText processing
Text processing
Icancode
 
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
 
Charcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.pptCharcater and Strings.ppt Charcater and Strings.ppt
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
Strings power point in detail with examples
Strings power point in detail with examplesStrings power point in detail with examples
Strings power point in detail with examples
rabiyanaseer1
 
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptxDOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
meganath16032003
 
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
 
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
Kavita Ganesan
 
3.7_StringBuilder.pdf
3.7_StringBuilder.pdf3.7_StringBuilder.pdf
3.7_StringBuilder.pdf
Ananthi68
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
Shahjahan Samoon
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
Danial Virk
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation x
Shahjahan Samoon
 

Recently uploaded (20)

Leveraging Foundation Models to Infer Intents
Leveraging Foundation Models to Infer IntentsLeveraging Foundation Models to Infer Intents
Leveraging Foundation Models to Infer Intents
Keheliya Gallaba
 
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
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
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
 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdfdp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
 
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
 
Essentials of Resource Planning in a Downturn
Essentials of Resource Planning in a DownturnEssentials of Resource Planning in a Downturn
Essentials of Resource Planning in a Downturn
OnePlan Solutions
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
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
 
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps CyclesFrom Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
Marjukka Niinioja
 
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink TemplateeeeeeeeeeeeeeeeeeeeeeeeeeNeuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
alexandernoetzold
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
Insurance Tech Services
 
Generative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its ApplicationsGenerative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its Applications
SandeepKS52
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
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
 
Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025
Orangescrum
 
Who will create the languages of the future?
Who will create the languages of the future?Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
 
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
 
Plooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your wayPlooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your way
Plooma
 
Leveraging Foundation Models to Infer Intents
Leveraging Foundation Models to Infer IntentsLeveraging Foundation Models to Infer Intents
Leveraging Foundation Models to Infer Intents
Keheliya Gallaba
 
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
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
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
 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdfdp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
 
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
 
Essentials of Resource Planning in a Downturn
Essentials of Resource Planning in a DownturnEssentials of Resource Planning in a Downturn
Essentials of Resource Planning in a Downturn
OnePlan Solutions
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
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
 
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps CyclesFrom Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
Marjukka Niinioja
 
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink TemplateeeeeeeeeeeeeeeeeeeeeeeeeeNeuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
alexandernoetzold
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
How Insurance Policy Administration Streamlines Policy Lifecycle for Agile Op...
Insurance Tech Services
 
Generative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its ApplicationsGenerative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its Applications
SandeepKS52
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
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
 
Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025Top 5 Task Management Software to Boost Productivity in 2025
Top 5 Task Management Software to Boost Productivity in 2025
Orangescrum
 
Who will create the languages of the future?
Who will create the languages of the future?Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
 
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
 
Plooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your wayPlooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your way
Plooma
 
Ad

javastringexample problems using string class

  • 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 andStringBuffer ▪ Difference between StringBuffer andStringBuilder
  • 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=newString("Welcome");
  • 9. Some important operations ofstrings ▪ StringConcatenation ▪ StringComparison ▪ Substring ▪ Length ofString ▪ String copy etc.
  • 10. StringConcatenation ▪ There are 2 methods to concatenate two or more string. Concatenate two or more string. Using concat() method Using + operator
  • 11. StringComparison ▪ String comparison can be done in 3 ways. String Comparison Using equals() method Using== operator By CompareTo() method
  • 13. 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(intstartIndex): 2. public String substring(int startIndex, int endIndex):
  • 14. Length ofString finds the length of the string. It returns count of total ▪ The java string length() method number of characters.
  • 15. 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()
  • 16. 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.
  • 17. 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.
  • 18. indexOf()  indexOf() = returns index of given character value or substring. If it is not found, it returns -1. The index counter starts from zero.
  • 19. replace()  replace() = returns a string replacing all the old char or CharSequence to new char or CharSequence.
  • 20. toCharArray()  toCharArray() = converts this string into character array.
  • 21. 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.
  • 22. Java StringBuffer class ▪ Java StringBuffer class is used to created mutable (modifiable) string. ▪ ImportantConstructors 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.
  • 23. Java StringBuilder class ▪ Java StringBuilder class is used to create mutable (modifiable) string.TheJavaStringBuilder class is same as StringBuffer class except that it is non-synchronized.
  • 24. Example of using StringBuffer & StringBuilder class
  • 25. 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 Objectclass.
  • 26. 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 thanStringBuilder. StringBuilder StringBuffer. is more efficient than