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
PDF
Polymorphism in Java
PPTX
MULTI THREADING IN JAVA
PPT
Java Arrays
PPS
Wrapper class
PPTX
Java abstract class & abstract methods
PPTX
Java string handling
PDF
Java String
String, string builder, string buffer
Polymorphism in Java
MULTI THREADING IN JAVA
Java Arrays
Wrapper class
Java abstract class & abstract methods
Java string handling
Java String

What's hot (20)

PPTX
Strings in Java
PPTX
Java exception handling
PPTX
Java awt (abstract window toolkit)
PPS
String and string buffer
PPTX
Inter Thread Communicationn.pptx
PPT
Inheritance in java
PPTX
Constructor in java
PPTX
Inheritance in c++
PPTX
java interface and packages
PPSX
Exception Handling
PPTX
Arrays in Java
PPTX
Java Method, Static Block
PPTX
Access specifiers(modifiers) in java
PPT
String classes and its methods.20
PPT
Java collection
PPT
Collection Framework in java
PPTX
Methods in java
PPTX
Super keyword in java
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
Static Members-Java.pptx
Strings in Java
Java exception handling
Java awt (abstract window toolkit)
String and string buffer
Inter Thread Communicationn.pptx
Inheritance in java
Constructor in java
Inheritance in c++
java interface and packages
Exception Handling
Arrays in Java
Java Method, Static Block
Access specifiers(modifiers) in java
String classes and its methods.20
Java collection
Collection Framework in java
Methods in java
Super keyword in java
Basic Concepts of OOPs (Object Oriented Programming in Java)
Static Members-Java.pptx
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
PPT
Advanced JavaScript
PPT
Wrapper class (130240116056)
PDF
Mistakes made with string object in java
PDF
OOPs difference faqs- 4
DOCX
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
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
Advanced JavaScript
Wrapper class (130240116056)
Mistakes made with string object in java
OOPs difference faqs- 4
Strings in javamnjn ijnjun oinoin oinoi .ppt

Recently uploaded (20)

PDF
Design and Evaluation of a Inonotus obliquus-AgNP-Maltodextrin Delivery Syste...
PPTX
climate change of delhi impacts on climate and there effects
PPSX
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
PPTX
INTRODUCTION TO PHILOSOPHY FULL SEM - COMPLETE.pptxINTRODUCTION TO PHILOSOPHY...
PDF
New_Round_Up_6_SB.pdf download for free, easy to learn
PDF
Kalaari-SaaS-Founder-Playbook-2024-Edition-.pdf
PPTX
GW4 BioMed Candidate Support Webinar 2025
PPTX
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
PDF
FAMILY PLANNING (preventative and social medicine pdf)
PPTX
Approach to a child with acute kidney injury
PDF
Physical pharmaceutics two in b pharmacy
PDF
CHALLENGES FACED BY TEACHERS WHEN TEACHING LEARNERS WITH DEVELOPMENTAL DISABI...
PDF
Unleashing the Potential of the Cultural and creative industries
PDF
GSA-Past-Papers-2010-2024-2.pdf CSS examination
PDF
IS1343_2012...........................pdf
PDF
3-Elementary-Education-Prototype-Syllabi-Compendium.pdf
PPTX
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
PPTX
FILIPINO 8 Q2 WEEK 1(DAY 1).power point presentation
PDF
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
PDF
Jana-Ojana Finals 2025 - School Quiz by Pragya - UEMK Quiz Club
Design and Evaluation of a Inonotus obliquus-AgNP-Maltodextrin Delivery Syste...
climate change of delhi impacts on climate and there effects
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
INTRODUCTION TO PHILOSOPHY FULL SEM - COMPLETE.pptxINTRODUCTION TO PHILOSOPHY...
New_Round_Up_6_SB.pdf download for free, easy to learn
Kalaari-SaaS-Founder-Playbook-2024-Edition-.pdf
GW4 BioMed Candidate Support Webinar 2025
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
FAMILY PLANNING (preventative and social medicine pdf)
Approach to a child with acute kidney injury
Physical pharmaceutics two in b pharmacy
CHALLENGES FACED BY TEACHERS WHEN TEACHING LEARNERS WITH DEVELOPMENTAL DISABI...
Unleashing the Potential of the Cultural and creative industries
GSA-Past-Papers-2010-2024-2.pdf CSS examination
IS1343_2012...........................pdf
3-Elementary-Education-Prototype-Syllabi-Compendium.pdf
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
FILIPINO 8 Q2 WEEK 1(DAY 1).power point presentation
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
Jana-Ojana Finals 2025 - School Quiz by Pragya - UEMK Quiz Club

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