7
Most read
11
Most read
13
Most read
STRING BUILDER & STRING BUFFER
Java Programming
Anwar Hasan Shuvo
Daffodil International University
1
Introduction
 Before getting into this topic we should learn somethings which are really needed.So we will
learn about Heap and Stack.
 Concept of Heap and Stack is very important in this topics.
 We are going to learn about String, StringBuffer & StringBuilder class.
We are starting our presentation with the concept of Heap and Stack
2
Heap & Stack
 Basically objects are stored in heap and local variables, methods are stored in stack
3
Heap
Objects
Stack
Local variables
&
Primitive Datatypes
String
 String is immutable object . (immutable means once created can not be changed )
 The object created as a String is stored in the Constant String Pool.
Every immutable object in Java is thread safe ,that implies String is also thread safe . String can
not be used by two threads simultaneously.
String once assigned can not be changed.
 String a = " Hello World " ;
// The above object is stored in constant string pool and its value can not be modified
 a += “Java Programming" ;
“Java Programming" string is created in constant pool and referenced by the variable named a.
“Hello World" string still exists in string constant pool and its value is not overrided but we
lost reference to the “Hello World“ String. 4
Example with CODE
Heap
Hello World Java Programming
Hello World
Heap
Hello World
5
Example with CODE
6
:String
Life is a tale told by an idiot
:String
Shakespeare
Life is a tale told by an idiot
It can no longer be accessed
String Buffer
 StringBuffer is mutable. ( mutable means one can change the value of the object)
 The object created through StringBuffer is stored in the heap . StringBuffer has the same
methods as the StringBuilder , but each method in StringBuffer is synchronized that
is StringBuffer is thread safe .
 Due to this it does not allow two threads to simultaneously access the same method . Each
method can be accessed by one thread at a time .
But being thread safe has disadvantages too as the performance of the StringBuffer hits due to
thread safe property . Thus StringBuilder is faster than the StringBuffer when calling the same
methods of each class.
StringBuffer value can be changed , it means it can be assigned to the new value .
 String Buffer can be converted to the string by using
toString() method.
7
EXAMPLE
 StringBuffer s = new StringBuffer("Hello World") ;
// The above object stored in heap and its value can be changed .
 s.append(“ Java Programming");
// Above statement is right as it modifies the value which is allowed
in the StringBuffer
8
Example with CODE
Heap
Hello World
Heap
Hello World Java Programming
9
String Builder
 StringBuilder is same as the StringBuffer , that is it stores the object in heap and it can also be
modified . The main difference between the StringBuffer and StringBuilder is that StringBuilder
is also not thread safe.
StringBuilder is fast as it is not thread safe .
 StringBuilder object= new StringBuilder("Hello");
// The above object too is stored in the heap and its value can be modified
object=new StringBuilder("Bye");
// Above statement is right as it modifies the value which is allowed in the
StringBuilder 10
Overview
String StringBuffer StringBuilder
Storage Area Constant String Pool Heap Heap
Thread Safe Yes Yes No
Modifiable immutable mutable mutable
11
Concatenation Of String
 String can be concatenated by using “+” operator or .concat() method.
 StringBuffer can be concatenated by using append().
 While concatenating StringBuffer is faster than String.
Because
String is converted into StringBuffer and then it use append() method to concatenate.
After that it use toString() method to be converted into String.
 StringBuffer uses append() method to concatenate.
12
Process of Concatenation of String And
StringBuffer
13
String
StringBuffer
append()
toString
String
StringBuffer
append()
toString
String StringBuffer

More Related Content

PPTX
String, string builder, string buffer
PPT
Oop java
PPTX
Java - Collections framework
PPTX
Strings in Java
PPTX
Two-dimensional array in java
PPS
String and string buffer
PPTX
Super Keyword in Java.pptx
PPTX
Data types in java
String, string builder, string buffer
Oop java
Java - Collections framework
Strings in Java
Two-dimensional array in java
String and string buffer
Super Keyword in Java.pptx
Data types in java

What's hot (20)

PPTX
Multi threading
PPTX
Java Beans
PDF
Java Thread Synchronization
PPTX
Collections and its types in C# (with examples)
PPT
Collection Framework in java
PPTX
History Of JAVA
PPTX
Java exception handling
PPTX
Java string handling
PPTX
Static Members-Java.pptx
PPTX
Classes objects in java
ODP
Multithreading In Java
PPTX
Inheritance in java
PPTX
Java Data Types
PPT
Final keyword in java
PPT
Applet life cycle
PDF
Java collections
PPS
Wrapper class
PDF
Java threads
PPT
Java collections concept
PDF
Arrays in Java
Multi threading
Java Beans
Java Thread Synchronization
Collections and its types in C# (with examples)
Collection Framework in java
History Of JAVA
Java exception handling
Java string handling
Static Members-Java.pptx
Classes objects in java
Multithreading In Java
Inheritance in java
Java Data Types
Final keyword in java
Applet life cycle
Java collections
Wrapper class
Java threads
Java collections concept
Arrays in Java
Ad

Viewers also liked (14)

PPTX
Flag Registers (Assembly Language)
PPTX
PDF
String handling(string buffer class)
PDF
Pieter Viljoen CV 2016 2
PPTX
Presentación istagram
PPT
Tema 5
DOCX
Christopher Stenum Resume
PPTX
Presentació del projecte E-Historiadors
PPTX
Venture capital
PPTX
Preliminary Task Evaluation
PPTX
Rrpp y los modelos de sociedad
PPTX
Bcp task 8
PPTX
Mutual funds
Flag Registers (Assembly Language)
String handling(string buffer class)
Pieter Viljoen CV 2016 2
Presentación istagram
Tema 5
Christopher Stenum Resume
Presentació del projecte E-Historiadors
Venture capital
Preliminary Task Evaluation
Rrpp y los modelos de sociedad
Bcp task 8
Mutual funds
Ad

Similar to String Builder & String Buffer (Java Programming) (20)

PPTX
Day_5.1.pptx
PPTX
Java String
PPTX
Interview-QA.pptx
PPTX
Introduction java string
PPTX
Session 05 - Strings in Java
PPSX
Strings in Java
PPT
String Handling
PPTX
Java Strings.pptxJava Strings.pptxJava Strings.pptx
PDF
Java interview questions
PPT
Strings, StringBuilder, StringBuffer.ppt
PPTX
Java interview questions 2
PPTX
javastringexample problems using string class
PPT
Java interview-questions-and-answers
PDF
Java String
PPT
Advanced JavaScript
PPT
Wrapper class (130240116056)
PDF
Mistakes made with string object in java
PDF
OOPs difference faqs- 4
DOCX
Day_5.1.pptx
Java String
Interview-QA.pptx
Introduction java string
Session 05 - Strings in Java
Strings in Java
String Handling
Java Strings.pptxJava Strings.pptxJava Strings.pptx
Java interview questions
Strings, StringBuilder, StringBuffer.ppt
Java interview questions 2
javastringexample problems using string class
Java interview-questions-and-answers
Java String
Advanced JavaScript
Wrapper class (130240116056)
Mistakes made with string object in java
OOPs difference faqs- 4

Recently uploaded (20)

PDF
GSA-Past-Papers-2010-2024-2.pdf CSS examination
PDF
IS1343_2012...........................pdf
PPTX
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
PDF
2003-theological-education-v39-n1-tai lieu
PPTX
UCSP Section A - Human Cultural Variations,Social Differences,social ChangeCo...
PDF
HSE 2022-2023.pdf الصحه والسلامه هندسه نفط
PDF
horaris de grups del curs 2025-2026 de l'institut
PDF
gsas-cvs-and-cover-letters jhvgfcffttfghgvhg.pdf
PDF
BÀI GIẢNG POWER POINT TIẾNG ANH 6 - I LEARN SMART WORLD - CẢ NĂM - NĂM 2025 (...
PDF
HSE and their team are going through the hazards of the issues with learning ...
PDF
Jana-Ojana Finals 2025 - School Quiz by Pragya - UEMK Quiz Club
PDF
3-Elementary-Education-Prototype-Syllabi-Compendium.pdf
PDF
Global strategy and action plan on oral health 2023 - 2030.pdf
PDF
BA-1ST(Education)-Education and Society.pdf
DOCX
HELMET DETECTION AND BIOMETRIC BASED VEHICLESECURITY USING MACHINE LEARNING.docx
PDF
The 10 Most Inspiring Education Leaders to Follow in 2025.pdf
PDF
Jana Ojana 2025 Prelims - School Quiz by Pragya - UEMK Quiz Club
PPTX
Entrepreneurship Management and Finance - Module 1 - PPT
PDF
Financial Reporting and Analysis Using Financial Accounting Information by Ch...
GSA-Past-Papers-2010-2024-2.pdf CSS examination
IS1343_2012...........................pdf
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
2003-theological-education-v39-n1-tai lieu
UCSP Section A - Human Cultural Variations,Social Differences,social ChangeCo...
HSE 2022-2023.pdf الصحه والسلامه هندسه نفط
horaris de grups del curs 2025-2026 de l'institut
gsas-cvs-and-cover-letters jhvgfcffttfghgvhg.pdf
BÀI GIẢNG POWER POINT TIẾNG ANH 6 - I LEARN SMART WORLD - CẢ NĂM - NĂM 2025 (...
HSE and their team are going through the hazards of the issues with learning ...
Jana-Ojana Finals 2025 - School Quiz by Pragya - UEMK Quiz Club
3-Elementary-Education-Prototype-Syllabi-Compendium.pdf
Global strategy and action plan on oral health 2023 - 2030.pdf
BA-1ST(Education)-Education and Society.pdf
HELMET DETECTION AND BIOMETRIC BASED VEHICLESECURITY USING MACHINE LEARNING.docx
The 10 Most Inspiring Education Leaders to Follow in 2025.pdf
Jana Ojana 2025 Prelims - School Quiz by Pragya - UEMK Quiz Club
Entrepreneurship Management and Finance - Module 1 - PPT
Financial Reporting and Analysis Using Financial Accounting Information by Ch...

String Builder & String Buffer (Java Programming)

  • 1. STRING BUILDER & STRING BUFFER Java Programming Anwar Hasan Shuvo Daffodil International University 1
  • 2. Introduction  Before getting into this topic we should learn somethings which are really needed.So we will learn about Heap and Stack.  Concept of Heap and Stack is very important in this topics.  We are going to learn about String, StringBuffer & StringBuilder class. We are starting our presentation with the concept of Heap and Stack 2
  • 3. Heap & Stack  Basically objects are stored in heap and local variables, methods are stored in stack 3 Heap Objects Stack Local variables & Primitive Datatypes
  • 4. String  String is immutable object . (immutable means once created can not be changed )  The object created as a String is stored in the Constant String Pool. Every immutable object in Java is thread safe ,that implies String is also thread safe . String can not be used by two threads simultaneously. String once assigned can not be changed.  String a = " Hello World " ; // The above object is stored in constant string pool and its value can not be modified  a += “Java Programming" ; “Java Programming" string is created in constant pool and referenced by the variable named a. “Hello World" string still exists in string constant pool and its value is not overrided but we lost reference to the “Hello World“ String. 4
  • 5. Example with CODE Heap Hello World Java Programming Hello World Heap Hello World 5
  • 6. Example with CODE 6 :String Life is a tale told by an idiot :String Shakespeare Life is a tale told by an idiot It can no longer be accessed
  • 7. String Buffer  StringBuffer is mutable. ( mutable means one can change the value of the object)  The object created through StringBuffer is stored in the heap . StringBuffer has the same methods as the StringBuilder , but each method in StringBuffer is synchronized that is StringBuffer is thread safe .  Due to this it does not allow two threads to simultaneously access the same method . Each method can be accessed by one thread at a time . But being thread safe has disadvantages too as the performance of the StringBuffer hits due to thread safe property . Thus StringBuilder is faster than the StringBuffer when calling the same methods of each class. StringBuffer value can be changed , it means it can be assigned to the new value .  String Buffer can be converted to the string by using toString() method. 7
  • 8. EXAMPLE  StringBuffer s = new StringBuffer("Hello World") ; // The above object stored in heap and its value can be changed .  s.append(“ Java Programming"); // Above statement is right as it modifies the value which is allowed in the StringBuffer 8
  • 9. Example with CODE Heap Hello World Heap Hello World Java Programming 9
  • 10. String Builder  StringBuilder is same as the StringBuffer , that is it stores the object in heap and it can also be modified . The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread safe. StringBuilder is fast as it is not thread safe .  StringBuilder object= new StringBuilder("Hello"); // The above object too is stored in the heap and its value can be modified object=new StringBuilder("Bye"); // Above statement is right as it modifies the value which is allowed in the StringBuilder 10
  • 11. Overview String StringBuffer StringBuilder Storage Area Constant String Pool Heap Heap Thread Safe Yes Yes No Modifiable immutable mutable mutable 11
  • 12. Concatenation Of String  String can be concatenated by using “+” operator or .concat() method.  StringBuffer can be concatenated by using append().  While concatenating StringBuffer is faster than String. Because String is converted into StringBuffer and then it use append() method to concatenate. After that it use toString() method to be converted into String.  StringBuffer uses append() method to concatenate. 12
  • 13. Process of Concatenation of String And StringBuffer 13 String StringBuffer append() toString String StringBuffer append() toString String StringBuffer