0% found this document useful (0 votes)
4 views5 pages

String Buffer Class1

The document provides an overview of various methods in the StringBuffer class in Java, including append(), insert(), delete(), deleteCharAt(), reverse(), capacity(), ensureCapacity(), setLength(), and replace(). Each method is explained with its purpose, syntax, and an example demonstrating its usage. The document emphasizes that these methods are synchronized, making them thread-safe for use in multi-threaded applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

String Buffer Class1

The document provides an overview of various methods in the StringBuffer class in Java, including append(), insert(), delete(), deleteCharAt(), reverse(), capacity(), ensureCapacity(), setLength(), and replace(). Each method is explained with its purpose, syntax, and an example demonstrating its usage. The document emphasizes that these methods are synchronized, making them thread-safe for use in multi-threaded applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

append()

• Appends the string representation of the argument to the sequence.


• The append() method is used with the StringBuffer class in Java to add data to the end
of the existing sequence (i.e., the StringBuffer content).

Syntax:
public synchronized StringBuffer append(dataType x)

Example:
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
System.out.println(sb); // Output: Hello World

2. insert()

• Inserts the string representation of the argument at the specified position.


• The insert() method is a member of the StringBuffer class and is used to insert data
into the existing character sequence at a specified index (position).
• The input can be of any data type (String, int, char, etc.), and it is automatically
converted to a string before insertion.

Syntax:
public synchronized StringBuffer insert(int offset, dataType x)

Example:
StringBuffer sb = new StringBuffer("HelloWorld"); sb.insert(5,
" ");
System.out.println(sb); // Output: Hello World
3. delete()

• Removes the characters in a substring of the sequence.


• The delete() method is used with the StringBuffer class to remove a range of
characters from the sequence.
• Purpose: Deletes characters from the StringBuffer starting at a specified start index
and ending just before the end index.
• Start index (inclusive): The position where deletion begins.
• End index (exclusive): The position just after the last character to be deleted.
• The characters from start to end - 1 are removed.
• This method is synchronized, making it safe for use in multithreaded programs.

Syntax:
public synchronized StringBuffer delete(int start, int end)

Example:
StringBuffer sb = new StringBuffer("HelloWorld");
sb.delete(5, 10);
System.out.println(sb); // Output: Hello

4. deleteCharAt()

• Removes the character at the specified position.


• Purpose: Deletes the character located at the given index.
• Index (0-based): Specifies the position of the character to be removed.
• Only one character is removed.
• Like other StringBuffer methods, it is synchronized, making it thread-safe.

Syntax:
public synchronized StringBuffer deleteCharAt(int index)

Example:
StringBuffer sb = new StringBuffer("Hello"); sb.deleteCharAt(1);
System.out.println(sb); // Output: Hllo
5. reverse()

• Reverses the sequence of characters.


• Purpose: Reverses the current content of the StringBuffer.
• All characters are rearranged in reverse order.
• The original string is modified directly.
• This method is synchronized, so it is safe in multi-threaded contexts.

Syntax:
public synchronized StringBuffer reverse()

Example:
StringBuffer sb = new StringBuffer("Hello");
sb.reverse();
System.out.println(sb); // Output: olleH

6. capacity()
Theory: Returns the current capacity of the buffer.
The capacity() method is a member of the StringBuffer class in Java that returns the current
capacity of the buffer. The capacity represents the amount of storage available for newly
inserted characters, beyond which an allocation will occur.
Key Points
1. Initial Capacity: When a StringBuffer is created without specifying a capacity, it
defaults to 16 characters.
2. Dynamic Resizing: The StringBuffer automatically increases its capacity when
needed:
o When the length of the content exceeds the current capacity
o The new capacity is typically calculated as: (current capacity + 1) * 2
3. Performance Consideration:
o Frequent resizing operations can impact performance
o For large content, it's better to initialize with an appropriate capacity
Syntax:
public int capacity()

Example:
java
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); // Output: 16 (default capacity)

7. ensureCapacity()

The ensureCapacity(int minimumCapacity) method in StringBuffer ensures that the buffer


has at least the specified minimum capacity. If the current capacity is less than the given
minimumCapacity, the method increases the capacity to either:
• The specified minimumCapacity, or
• Twice the current capacity + 2 (i.e., (currentCapacity + 1) * 2), whichever is larger.
This method is synchronized, meaning it is thread-safe.

Syntax:
public synchronized void ensureCapacity(int minimumCapacity)

Example:
StringBuffer sb = new StringBuffer();
sb.ensureCapacity(50);
System.out.println(sb.capacity()); // Output: 50

8. setLength()

• Sets the length of the character sequence.


• Purpose: Sets the length of the StringBuffer to the specified value (newLength).
• If newLength is:

• Less than current length → extra characters are truncated (deleted).


• Greater than current length → the buffer is extended, and the new characters are filled
with null characters (\u0000).

• This method modifies the content directly.


Syntax:
public synchronized void setLength(int newLength)

Example:
java
StringBuffer sb = new StringBuffer("Hello");
sb.setLength(3);
System.out.println(sb); // Output: Hel

9. replace()

• Theory: Replaces the characters in a substring with characters in the specified String.
• Purpose: Replaces the characters between the start index (inclusive) and end index
(exclusive) with the specified new string.
• This modifies the original content of the StringBuffer.
• Like other StringBuffer methods, it is synchronized, ensuring thread safety.

Syntax:
public synchronized StringBuffer replace(int start, int end, String str)

Example:
StringBuffer sb = new StringBuffer("Hello World");
sb.replace(6, 11, "Java");
System.out.println(sb); // Output: Hello Java

You might also like