JAVA STRING Flashcards
In Java, the ____ class is used to
represent a ___________.
String, group of characters
It is one of the most frequently
used classes in Java programming.
Java String or String
When a new ____ is created,
________ is allocated for it.
string, memory
It is created using double quotes and it can store in the String Pool
String Literals
This can be created using the new keyword or concatenation.
Dynamically Constructed Strings
What is String Pool
a special memory area.
What is Heap
general memory area for objects.
The _____ is like a __________ where Java stores data
Heap, large warehouse
What are the two key features of Heap Memory?
Dynamic and Garbage Collection
Java automatically removes unused data to free up memory.
Garbage Collection
A special area within the Heap for storing strings.
String Pool (String Constant Pool)
What is the Purpose of String Pool?
Saves memory by reusing identical strings and it Improves performance by avoiding duplicate data.
Heap is a general memory for _____, ______, and managed by _______.
data, dynamic, garbage collection
String Pool is a ________ for strings, optimized for ______ and ______.
special memory, reuse, efficiency
Strings in Java are _________, meaning their values __________ after creation.
immutable, cannot be changed
This behavior can lead to __________, especially when performing frequent __________.
higher memory usage, string
manipulations
Java’s __________ efficiently manages memory by cleaning up _________.
garbage collector, unused string data
In Java, __________ ensures ________ and ________, especially when multiple parts of a program use the same text.
immutability, predictable, safe behavior
_________ ensures that multiple threads can access shared _____ without causing _____ or _______.
Thread safety, data, errors, corruption
What is race condition?
two threads updating the same data simultaneously.
What is data inconsistency?
threads seeing incorrect or partial data.
What are the use of Single-Thread?
Perform one task at a time and Slower for multiple tasks.
What are the use of Multi-Threaded Programs?
Perform multiple tasks simultaneously. Faster and more efficient.
When to use Single-Thread?
Use single-thread for simple programs
When to use multi-thread?
Use multi-thread for complex programs
In Java, a ________ is a special method used for __________.
constructor, initialization
A constructor sets up the
_________ by _________ or
___________.
initial state, assigning values, allocating memory,
In terms of StringBuilder, a __________is used to ________ a new StringBuilder with a ________ or ___________.
StringBuilder, constructor initialize, default, specified value
The StringBuilder class provides several________ that allow different ways to create a _________.
constructors, StringBuilder instance
When using StringBuilder() (Default Constructor), what does it initializes?
an empty StringBuilder with a default capacity of 16 characters.
When using StringBuilder(int capacity) (Custom Capacity), What does it create?
a StringBuilder with a custom capacity (useful when handling large strings to reduce memory reallocation).
When using StringBuilder(String str) (Initialize with String), What does it create?
a StringBuilder initialized with the given string value with the initial capacity of 16+length of the string.
When using StringBuilder(CharSequence seq) (Initialize with CharSequence), What does it create?
CharSequence is a general representation of character sequences (like String and StringBuffer).
This constructor allows initializing a StringBuilder using a StringBuffer or another CharSequence.
StringBuilder(CharSequence seq) (Initialize with CharSequence)
The _________ class in Java provides various _________ to manipulate ________ efficiently.
StringBuilder, methods, string sequences
This method adds the specified text at the end of the existing StringBuilder content.
append(String s)
The _______ method returns the same _________, allowing ____________.
append(), StringBuilder instance, method chaining
This method Inserts the specified text at the given index (offset).
StringBuilder.insert(int offset, data);
The index position where the data should be inserted.
offset
The value to be inserted (can be a String, char, int, double, etc.).
data
The _______ method modifies the existing ________ data without creating a new one.
insert(), StringBuilder
When to use StringBuilder.insert(int offset, data); ?
Useful when inserting a string at a specific
position.
This method replaces the characters between start and end indexes with the specified string.
replace(int start, int end, String str)
The starting index (inclusive) where replacement
start
The ending index (exclusive) where replacement stops.
end
The new string that will replace the existing
characters in the given range.
str
The _______ method modifies the existing StringBuilder data without creating a new one.
replace()
When to use replace(int start, int end, String str)?
useful when modifying a portion of the string.
This method deletes characters between the specified start and end indexes.
delete(int start, int end)
When to use delete(int start, int end)?
when removing unwanted parts of the string.
Removes a single character at the
specified index.
deleteCharAt(int index)
The ________ method ______ the existing __________ instead of creating a new one.
deleteCharAt() , modifies, StringBuilder
When to use deleteCharAt(int index)?
Useful for deleting a single character without affecting the rest of the string.
This method reverses the sequence of characters in the StringBuilder.
reverse()
This method modifies the existing StringBuilder in place.
reverse()
When to use reverse()
useful when reversing strings for palindromes or algorithmic processing.
This method returns the current capacity (storage size) of the StringBuilder.
capacity()
__________ refers to the number of characters the StringBuilder can hold before it needs to resize.
Capacity
Default capacity is ________ if created with
new StringBuilder().
16 characters
When the capacity is _________, it _________ using the formula:
exceeded, expands automatically
What is the formula of Capacity method?
New Capacity = (Old Capacity * 2) + 2
When to use capacity?
When understanding how much space is allocated before resizing occurs.
This method does not shrink capacity, only increases it if necessary.
ensureCapacity(int minCapacity)
This method ensures that the StringBuilder has at least minCapacity of storage, growing if necessary.
ensureCapacity(int minCapacity)
If minCapacity is greater than the current capacity, the capacity increases using the formula. Which is ?
New Capacity = (Old Capacity * 2) + 2
If minCapacity is less than or equal to the current capacity, what happens?
no change is made
When to use ensureCapacity(int minCapacity)?
when preventing frequent resizing when adding large amounts of data.
This method returns the number of characters in the StringBuilder.
length()
What is the syntax of length?
int currentLength = StringBuilder.length();
What differs lengths() to capacity()?
length() → Returns the actual number of
characters in the StringBuilder.
capacity() → Returns the total allocated space for characters.
When to use length()?
When determining the current size of the content.
This method returns the character at the specified index
charAt(int index)
The position of the character to
retrieve
index
When to use charAt(int index)
When retrieving a specific character from the StringBuilder
This method changes the character at the specified index to the given char c.
setCharAt(int index, char c)
When to use setCharAt(int index, char c)?
Useful for modifying individual characters without modifying the whole string.
This method returns a new String starting from start index to the end.
substring(int start)
When to use substring(int start)
Extracts a portion of the StringBuilder as a String.
This method extracts a portion of the string between the given start (inclusive) and end (exclusive) indexes.
substring(int start, int end)
When to use substring(int start, int end)?
Extracts a portion of the StringBuilder as a String.
This method converts the StringBuilder data into a String.
toString()
When to use toString()?
Since StringBuilder is mutable, this method is used when we need an immutable String representation.
What are the key differences of StringBuilder and StringBuffer?
the key difference is that StringBuffer is thread- safe (synchronized) but slower, while StringBuilder is faster but not thread-safe.
What is the execution time for StringBuilder and StringBuffer when performing a large number of operations?
Appending 100,000 times
Give 3 string types in Java
String (Immutable)
StringBuffer (Mutable & Thread-Safe)
StringBuilder (Mutable & Fast but Not Thread-Safe)
“Since String is immutable, converting it to StringBuffer or StringBuilder allows modifications.”. What type of conversion is this?
Convert String to StringBuffer and
StringBuilder
“Since StringBuffer and StringBuilder are mutable, converting them to String gives an
immutable version.” What type of conversion is this?
Convert StringBuffer and StringBuilder
to String
“Since both are mutable, Java doesn’t provide a direct method to convert between them. We first convert to String, then create a new instance of the other type.” What type of conversion is this?
Convert StringBuffer to StringBuilder or
Vice Versa
In Java, working with different string types—_____, _____, and _______—is important for handling text efficiently.
String, StringBuffer, StringBuilder
_________ is immutable, meaning it cannot be changed once created, while ______ and ______ are mutable, allowing _______ without creating ________.
String, StringBuffer, StringBuilder, modifications, new instance.
The key difference between StringBuffer and StringBuilder is that StringBuffer is _________, making it suitable for _________, whereas StringBuilder is faster but not _________, making it ideal for ____________ environments.
thread-safe (synchronized), multi-threaded applications, synchronized, single-threaded