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
javastringexample problems using string class
fedcoordinator
 
PPT
Strings, StringBuilder, StringBuffer.ppt
shahid sultan
 
PPTX
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
PDF
String.ppt
ajeela mushtaq
 
PPT
Java Strings methods and operations.ppt
JyothiAmpally
 
PPTX
Java string , string buffer and wrapper class
SimoniShah6
 
PPTX
Day_5.1.pptx
ishasharma835109
 
PDF
3.7_StringBuilder.pdf
Ananthi68
 
PPTX
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
PPTX
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
PPT
String Handling
Bharat17485
 
PPTX
Strings in Java
Abhilash Nair
 
PDF
Java Presentation on the topic of string
RajTangadi
 
PDF
Java R20 - UNIT-5.pdf
Pamarthi Kumar
 
DOCX
Java R20 - UNIT-5.docx
Pamarthi Kumar
 
PPTX
Java Strings
RaBiya Chaudhry
 
PPT
Text processing
Icancode
 
PPTX
String in JAVA --------------------------
2003sayanch
 
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
PPTX
StringBuffer.pptx
meenakshi pareek
 
javastringexample problems using string class
fedcoordinator
 
Strings, StringBuilder, StringBuffer.ppt
shahid sultan
 
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
String.ppt
ajeela mushtaq
 
Java Strings methods and operations.ppt
JyothiAmpally
 
Java string , string buffer and wrapper class
SimoniShah6
 
Day_5.1.pptx
ishasharma835109
 
3.7_StringBuilder.pdf
Ananthi68
 
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
String Handling
Bharat17485
 
Strings in Java
Abhilash Nair
 
Java Presentation on the topic of string
RajTangadi
 
Java R20 - UNIT-5.pdf
Pamarthi Kumar
 
Java R20 - UNIT-5.docx
Pamarthi Kumar
 
Java Strings
RaBiya Chaudhry
 
Text processing
Icancode
 
String in JAVA --------------------------
2003sayanch
 
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
StringBuffer.pptx
meenakshi pareek
 
Ad

Recently uploaded (20)

PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PDF
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
PPTX
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
 
PPTX
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PPTX
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
PDF
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
PPTX
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
PDF
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PPTX
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
PPTX
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
PPTX
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
PPTX
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
 
PPTX
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
PDF
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
PDF
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
PDF
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
AI-assisted IP-Design lecture from the MIPLM 2025
MIPLM
 
MATH 8 QUARTER 1 WEEK 1 LESSON 2 PRESENTATION
JohnGuillerNestalBah1
 
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
grade 8 week 2 ict.pptx. matatag grade 7
VanessaTaberlo
 
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 

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