SlideShare a Scribd company logo
7
Most read
10
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

What's hot (20)

PPT
Java static keyword
Lovely Professional University
 
PPTX
Type casting in java
Farooq Baloch
 
PPSX
Java String class
DrRajeshreeKhande
 
PPT
Java interfaces
Raja Sekhar
 
PPTX
Java String
SATYAM SHRIVASTAV
 
PPTX
Java string handling
Salman Khan
 
PPTX
Operator overloading
Burhan Ahmed
 
PDF
C++ OOPS Concept
Boopathi K
 
PPTX
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
PPTX
Two-dimensional array in java
Talha mahmood
 
PPS
String and string buffer
kamal kotecha
 
PPTX
Super Keyword in Java.pptx
KrutikaWankhade1
 
PPTX
Arrays in java
Arzath Areeff
 
PPTX
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
PPT
Method overriding
Azaz Maverick
 
PPT
C++ Function
Hajar
 
PDF
What is Serialization in Java? | Java Tutorial | Edureka
Edureka!
 
PPTX
Chapter 07 inheritance
Praveen M Jigajinni
 
ODP
Java Collections
parag
 
Java static keyword
Lovely Professional University
 
Type casting in java
Farooq Baloch
 
Java String class
DrRajeshreeKhande
 
Java interfaces
Raja Sekhar
 
Java String
SATYAM SHRIVASTAV
 
Java string handling
Salman Khan
 
Operator overloading
Burhan Ahmed
 
C++ OOPS Concept
Boopathi K
 
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Two-dimensional array in java
Talha mahmood
 
String and string buffer
kamal kotecha
 
Super Keyword in Java.pptx
KrutikaWankhade1
 
Arrays in java
Arzath Areeff
 
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
Method overriding
Azaz Maverick
 
C++ Function
Hajar
 
What is Serialization in Java? | Java Tutorial | Edureka
Edureka!
 
Chapter 07 inheritance
Praveen M Jigajinni
 
Java Collections
parag
 

Viewers also liked (14)

PPTX
Flag Registers (Assembly Language)
Anwar Hasan Shuvo
 
PPTX
String in java
Ideal Eyes Business College
 
PDF
String handling(string buffer class)
Ravi_Kant_Sahu
 
PDF
Pieter Viljoen CV 2016 2
Pieter Viljoen
 
PPTX
Presentación istagram
Nohemi Yohana Gonzalez de Guerra
 
PPT
Tema 5
Patricia Gil
 
DOCX
Christopher Stenum Resume
Christopher Stenum
 
PPTX
Presentació del projecte E-Historiadors
Àfrica Garcia Cañadas
 
PPTX
Venture capital
Maria Jacks
 
PPTX
Preliminary Task Evaluation
wjacca
 
PPTX
Rrpp y los modelos de sociedad
diaz30
 
PPTX
Bcp task 8
Norah Al Aslmey
 
PPTX
Mutual funds
Maria Jacks
 
Flag Registers (Assembly Language)
Anwar Hasan Shuvo
 
String handling(string buffer class)
Ravi_Kant_Sahu
 
Pieter Viljoen CV 2016 2
Pieter Viljoen
 
Presentación istagram
Nohemi Yohana Gonzalez de Guerra
 
Tema 5
Patricia Gil
 
Christopher Stenum Resume
Christopher Stenum
 
Presentació del projecte E-Historiadors
Àfrica Garcia Cañadas
 
Venture capital
Maria Jacks
 
Preliminary Task Evaluation
wjacca
 
Rrpp y los modelos de sociedad
diaz30
 
Bcp task 8
Norah Al Aslmey
 
Mutual funds
Maria Jacks
 
Ad

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

PPTX
Day_5.1.pptx
ishasharma835109
 
PPTX
Interview-QA.pptx
SharanabasavaSharanu1
 
PPTX
Introduction java string
sambhajimeher
 
PPTX
Session 05 - Strings in Java
PawanMM
 
PPSX
Strings in Java
Hitesh-Java
 
PPT
String Handling
Bharat17485
 
PDF
Java interview questions
Java2Blog
 
PPT
Strings, StringBuilder, StringBuffer.ppt
shahid sultan
 
PPTX
Java interview questions 2
Sherihan Anver
 
PPTX
javastringexample problems using string class
fedcoordinator
 
PPT
Java interview-questions-and-answers
bestonlinetrainers
 
PDF
Java String
Java2Blog
 
PPT
Advanced JavaScript
Fu Cheng
 
PPT
Wrapper class (130240116056)
Akshay soni
 
PDF
Mistakes made with string object in java
Tutors On Net
 
PDF
OOPs difference faqs- 4
Umar Ali
 
DOCX
Java67
Nagesh Halgonde
 
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
DOCX
Jist of Java
Nikunj Parekh
 
Day_5.1.pptx
ishasharma835109
 
Interview-QA.pptx
SharanabasavaSharanu1
 
Introduction java string
sambhajimeher
 
Session 05 - Strings in Java
PawanMM
 
Strings in Java
Hitesh-Java
 
String Handling
Bharat17485
 
Java interview questions
Java2Blog
 
Strings, StringBuilder, StringBuffer.ppt
shahid sultan
 
Java interview questions 2
Sherihan Anver
 
javastringexample problems using string class
fedcoordinator
 
Java interview-questions-and-answers
bestonlinetrainers
 
Java String
Java2Blog
 
Advanced JavaScript
Fu Cheng
 
Wrapper class (130240116056)
Akshay soni
 
Mistakes made with string object in java
Tutors On Net
 
OOPs difference faqs- 4
Umar Ali
 
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
Jist of Java
Nikunj Parekh
 
Ad

Recently uploaded (20)

PPTX
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
PPTX
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
 
PDF
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPTX
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
PDF
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
PDF
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
PPTX
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PDF
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
PDF
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
PPTX
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
PPTX
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
PDF
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
PDF
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
PDF
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
PPTX
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
 
How to Manage Wins & Losses in Odoo 18 CRM
Celine George
 
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
The Gift of the Magi by O Henry-A Story of True Love, Sacrifice, and Selfless...
Beena E S
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
I3PM Case study smart parking 2025 with uptoIP® and ABP
MIPLM
 
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
Indian National movement PPT by Simanchala Sarab, Covering The INC(Formation,...
Simanchala Sarab, BABed(ITEP Secondary stage) in History student at GNDU Amritsar
 
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
 

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