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

PDF
Wrapper classes
Ravi_Kant_Sahu
 
PDF
Arrays in Java
Naz Abdalla
 
PPTX
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
PPS
Wrapper class
kamal kotecha
 
PDF
Files in java
Muthukumaran Subramanian
 
PPTX
Java arrays
Jin Castor
 
PPTX
Applets in java
Wani Zahoor
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
Wrapper classes
Ravi_Kant_Sahu
 
Arrays in Java
Naz Abdalla
 
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Wrapper class
kamal kotecha
 
Java arrays
Jin Castor
 
Applets in java
Wani Zahoor
 
C# classes objects
Dr.Neeraj Kumar Pandey
 

What's hot (20)

PPTX
Strings in Java
Abhilash Nair
 
PDF
Set methods in python
deepalishinkar1
 
PPTX
Java constructors
QUONTRASOLUTIONS
 
PPTX
Access specifiers(modifiers) in java
HrithikShinde
 
PDF
Collections in Java Notes
Shalabh Chaudhary
 
PPT
Inheritance in java
Lovely Professional University
 
PPTX
Constructor in java
Madishetty Prathibha
 
PPTX
Access Modifier.pptx
Margaret Mary
 
PPTX
Core java complete ppt(note)
arvind pandey
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPTX
Java string handling
Salman Khan
 
PPTX
Fundamentals of OOP (Object Oriented Programming)
MD Sulaiman
 
PPTX
Inner classes in java
PhD Research Scholar
 
PPTX
Arrays in java
Arzath Areeff
 
PPSX
Data Types & Variables in JAVA
Ankita Totala
 
PPTX
Java package
CS_GDRCST
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PPT
Java Collections Framework
Sony India Software Center
 
PPTX
L21 io streams
teach4uin
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
Strings in Java
Abhilash Nair
 
Set methods in python
deepalishinkar1
 
Java constructors
QUONTRASOLUTIONS
 
Access specifiers(modifiers) in java
HrithikShinde
 
Collections in Java Notes
Shalabh Chaudhary
 
Inheritance in java
Lovely Professional University
 
Constructor in java
Madishetty Prathibha
 
Access Modifier.pptx
Margaret Mary
 
Core java complete ppt(note)
arvind pandey
 
Classes objects in java
Madishetty Prathibha
 
Java string handling
Salman Khan
 
Fundamentals of OOP (Object Oriented Programming)
MD Sulaiman
 
Inner classes in java
PhD Research Scholar
 
Arrays in java
Arzath Areeff
 
Data Types & Variables in JAVA
Ankita Totala
 
Java package
CS_GDRCST
 
Exception Handling in JAVA
SURIT DATTA
 
Java Collections Framework
Sony India Software Center
 
L21 io streams
teach4uin
 
Classes, objects in JAVA
Abhilash Nair
 
Ad

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
Java String
SATYAM SHRIVASTAV
 
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
 
Day_5.1.pptx
ishasharma835109
 
Java String
SATYAM SHRIVASTAV
 
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
 

Recently uploaded (20)

PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 

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