SlideShare a Scribd company logo
2
Most read
3
Most read
7
Most read
String Builder 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.
Constructor Description
StringBuilder() It creates an empty String Builder with the initial capacity of
16.
StringBuilder(String str) It creates a String Builder with the specified string.
StringBuilder(int length) It creates an empty String Builder with the specified
capacity as length.
Method Description
public StringBuilder append(String s) It 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.
public StringBuilder insert(int offset, String s) It 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.
public StringBuilder replace(int startIndex, int
endIndex, String str)
It is used to replace the string from specified startIndex and
endIndex.
public StringBuilder delete(int startIndex, int
endIndex)
It is used to delete the string from specified startIndex and
endIndex.
public StringBuilder reverse() It is used to reverse the string.
public int capacity() It is used to return the current capacity.
public void ensureCapacity(int
minimumCapacity)
It is used to ensure the capacity at least equal to the
given minimum.
public char charAt(int index) It is used to return the character at the specified
position.
public int length() It is used to return the length of the string i.e. total
number of characters.
public String substring(int beginIndex) It is used to return the substring from the specified
beginIndex.
public String substring(int beginIndex, int
endIndex)
It is used to return the substring from the specified
beginIndex and endIndex.
class StringBuilderExample
{
public static void main(String args[])
{
StringBuilder sb=new StringBuilder("Hello ");
sb.append("Java"); //now original string is changed
System.out.println(sb); //prints Hello Java
}
}
class StringBuilderExample2
{
public static void main(String args[])
{
StringBuilder sb=new StringBuilder("Hello ");
sb.insert(1,"Java"); //now original string is changed
System.out.println(sb); //prints HJavaello
}
}
No. String StringBuffer
1) The String class is immutable. The StringBuffer class is mutable.
2) String is slow and consumes more memory when
we concatenate too many strings because every
time it creates new instance.
StringBuffer is fast and consumes less memory when
we concatenate t 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.
4) String class is slower while performing
concatenation operation.
StringBuffer class is faster while performing
concatenation operation.
5) String class uses String constant pool. StringBuffer uses Heap memory
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.
3) StringBuffer was introduced in Java 1.0 StringBuilder was introduced in Java 1.5

More Related Content

PDF
String.ppt
PPTX
javastringexample problems using string class
PPTX
Java string , string buffer and wrapper class
PPTX
Java string handling
PPTX
String Handling, Inheritance, Packages and Interfaces
PPT
Java Strings methods and operations.ppt
PPTX
StringBuffer.pptx
PPTX
Day_5.1.pptx
String.ppt
javastringexample problems using string class
Java string , string buffer and wrapper class
Java string handling
String Handling, Inheritance, Packages and Interfaces
Java Strings methods and operations.ppt
StringBuffer.pptx
Day_5.1.pptx

Similar to 3.7_StringBuilder.pdf (20)

PPTX
Strings in Java
PPTX
Fileoperations.pptx
PPTX
L14 string handling(string buffer class)
PPTX
In the given example only one object will be created. Firstly JVM will not fi...
PPTX
package
PPTX
String in java, string constructors and operations
PPT
String classes and its methods.20
PPTX
Java String
PPTX
String handling
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
PPTX
PDF
Module-1 Strings Handling.ppt.pdf
DOCX
Java R20 - UNIT-5.docx
PPTX
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
PDF
Java R20 - UNIT-5.pdf
PPT
JAVA CONCEPTS
PPTX
Java Strings.pptxJava Strings.pptxJava Strings.pptx
PPTX
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
PPS
String and string buffer
PPTX
Introduction to Java Strings, By Kavita Ganesan
Strings in Java
Fileoperations.pptx
L14 string handling(string buffer class)
In the given example only one object will be created. Firstly JVM will not fi...
package
String in java, string constructors and operations
String classes and its methods.20
Java String
String handling
Charcater and Strings.ppt Charcater and Strings.ppt
Module-1 Strings Handling.ppt.pdf
Java R20 - UNIT-5.docx
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
Java R20 - UNIT-5.pdf
JAVA CONCEPTS
Java Strings.pptxJava Strings.pptxJava Strings.pptx
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
String and string buffer
Introduction to Java Strings, By Kavita Ganesan
Ad

Recently uploaded (20)

PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Artificial Intelligence
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Sustainable Sites - Green Building Construction
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
737-MAX_SRG.pdf student reference guides
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
Total quality management ppt for engineering students
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
DOCX
573137875-Attendance-Management-System-original
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Artificial Intelligence
additive manufacturing of ss316l using mig welding
Sustainable Sites - Green Building Construction
Foundation to blockchain - A guide to Blockchain Tech
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
737-MAX_SRG.pdf student reference guides
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Total quality management ppt for engineering students
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Geodesy 1.pptx...............................................
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
573137875-Attendance-Management-System-original
Embodied AI: Ushering in the Next Era of Intelligent Systems
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Ad

3.7_StringBuilder.pdf

  • 1. String Builder 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. Constructor Description StringBuilder() It creates an empty String Builder with the initial capacity of 16. StringBuilder(String str) It creates a String Builder with the specified string. StringBuilder(int length) It creates an empty String Builder with the specified capacity as length.
  • 2. Method Description public StringBuilder append(String s) It 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. public StringBuilder insert(int offset, String s) It 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. public StringBuilder replace(int startIndex, int endIndex, String str) It is used to replace the string from specified startIndex and endIndex. public StringBuilder delete(int startIndex, int endIndex) It is used to delete the string from specified startIndex and endIndex. public StringBuilder reverse() It is used to reverse the string.
  • 3. public int capacity() It is used to return the current capacity. public void ensureCapacity(int minimumCapacity) It is used to ensure the capacity at least equal to the given minimum. public char charAt(int index) It is used to return the character at the specified position. public int length() It is used to return the length of the string i.e. total number of characters. public String substring(int beginIndex) It is used to return the substring from the specified beginIndex. public String substring(int beginIndex, int endIndex) It is used to return the substring from the specified beginIndex and endIndex.
  • 4. class StringBuilderExample { public static void main(String args[]) { StringBuilder sb=new StringBuilder("Hello "); sb.append("Java"); //now original string is changed System.out.println(sb); //prints Hello Java } }
  • 5. class StringBuilderExample2 { public static void main(String args[]) { StringBuilder sb=new StringBuilder("Hello "); sb.insert(1,"Java"); //now original string is changed System.out.println(sb); //prints HJavaello } }
  • 6. No. String StringBuffer 1) The String class is immutable. The StringBuffer class is mutable. 2) String is slow and consumes more memory when we concatenate too many strings because every time it creates new instance. StringBuffer is fast and consumes less memory when we concatenate t 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. 4) String class is slower while performing concatenation operation. StringBuffer class is faster while performing concatenation operation. 5) String class uses String constant pool. StringBuffer uses Heap memory
  • 7. 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. 3) StringBuffer was introduced in Java 1.0 StringBuilder was introduced in Java 1.5