Stringbuilder and Stringbuffer Flashcards
In Java, the ________ class is used to
represent a group of characters.
String
When a new string is created,
_________ is allocated for it.
memory
In Java, immutability ensures __________
and ___________, especially when multiple
parts of a program use the same text.
predictable and safe behavior
__________ ensures that multiple threads can
access shared data without causing errors or
corruption.
Thread safety
______-Threaded Programs:
1. Perform one task at a time.
2. Slower for multiple tasks.
Single
_____-Threaded Programs
1. Perform multiple tasks simultaneously.
2. Faster and more efficient.
Multi
Use _____________ for simple programs (e.g.,
basic calculations).
single-thread
Use _____________ for complex programs (e.g.,
video streaming, chat apps, web servers).
multi-thread
In Java, a __________ is a special method used
for initialization.
constructor
A constructor sets up the
initial state by assigning ________ or
allocating __________.
A constructor sets up the
initial state by assigning VALUES or
allocating MEMORY.
Visualize what the constructor look like in Java:
Creates an empty StringBuilder with an initialize capacity of 16 characters.
StringBuilder()
Visualize what the constructor look like in Java:
Creates an empty StringBuilder with an specified capacity (number of characters it can hold before resizing).
StringBuilder(int capacity)
Visualize what the constructor look like in Java:
Creates a StringBuilder initialized with the given string value.
The initial capacity is 16 + length of the string.
StringBuilder(String str)
Visualize what the constructor look like in Java:
This constructor allows initializing a StringBuilder using a
StringBuffer or another CharSequence.
StringBuilder(CharSequence seq)
Adds the specified text at the end of
the existing StringBuilder content.
append(String s)
Description: Replaces the characters between start and end
indexes with the specified string.
replace(int start, int end, String str)
Description: Deletes characters between the specified
start and end indexes.
delete(int start, int end)
Description: Removes a single character at the
specified index.
deleteCharAt(int index)
The delete() method __________ the existing StringBuilder
without creating a new one.
modifies
Description: Reverses the sequence of characters in
the StringBuilder.
reverse()
Description: Returns the current capacity (storage size) of
the StringBuilder.
capacity()
Unlike length(), which gives the actual number of
characters stored, ___________ tells how much
space is allocated.
capacity()
Description: Ensures that the StringBuilder has at least
minCapacity of storage, growing if necessary.
ensureCapacity(int minCapacity)
Description: Returns the number of characters in the
StringBuilder.
length()
Description: Returns the character at the specified
index.
charAt(int index)
Description: Changes the character at the specified index to
the given char c.
setCharAt(int index, char c)
Description: Returns a new String starting from start
index to the end.
substring(int start)
Description: Extracts a portion of the string between the
given start (inclusive) and end (exclusive) indexes.
substring(int start, int end)
Description: Converts the StringBuilder data into a
String.
toString()
Use _____________ if multiple people
(threads) will be working on the same
text at the same time.
StringBuffer
Use _____________ if only one person
(thread) is working on it for speed.
StringBuilder
Since StringBuffer and StringBuilder are
mutable, converting them to String gives an
____________ version.
immutable