The document discusses the StringBuffer class in Java. It describes how StringBuffer supports mutable strings and provides methods for modifying, inserting, and deleting characters from strings. It explains important StringBuffer methods like append(), insert(), delete(), and setLength(). Constructors and rules for automatically increasing the capacity are also covered. Examples demonstrate how to use StringBuffer methods and how capacity is adjusted.
The document discusses the StringBuffer class in Java. It describes how StringBuffer supports mutable strings and provides methods for modifying, inserting, and deleting characters from strings. It explains important StringBuffer methods like append(), insert(), delete(), and setLength(). Constructors and rules for automatically increasing the capacity are also covered. Examples demonstrate how to use StringBuffer methods and how capacity is adjusted.
The document discusses the StringBuffer class in Java. It describes how StringBuffer supports mutable strings and provides methods for modifying, inserting, and deleting characters from strings. It explains important StringBuffer methods like append(), insert(), delete(), and setLength(). Constructors and rules for automatically increasing the capacity are also covered. Examples demonstrate how to use StringBuffer methods and how capacity is adjusted.
The document discusses the StringBuffer class in Java. It describes how StringBuffer supports mutable strings and provides methods for modifying, inserting, and deleting characters from strings. It explains important StringBuffer methods like append(), insert(), delete(), and setLength(). Constructors and rules for automatically increasing the capacity are also covered. Examples demonstrate how to use StringBuffer methods and how capacity is adjusted.
Department of Computer Science, BITS-Pilani, Pilani Campus CS F213 RL 8.4 : Topics
StringBuffer class in Java
2 Object-Oriented Programming (CS F213)
StringBuffer class Supports mutable, modifiable, writable and growable strings in Java You can insert characters or sub-strings in the middle or in the end length of a StringBuffer instance means how many characters it currently holds. The current length of a StringBuffer instance can be checked via length() method capacity of a StringBuffer instance means how many more characters it can accommodate. The current capacity of a StringBuffer instance can be checked via capacity() method capacity increases automatically whenever the need arises. The rule to increase capacity is if current-capacity < 16 then new-capacity = 16 else new-capacity = 2 * current-capacity + 2. Important Constructors 1. StringBuffer() length =0, capacity = length +16 = 16 2. StringBuffer(int size) length =0, capacity = size 3. StringBuffer(String str) length =str.length(), capacity = length + 16
3 Object-Oriented Programming (CS F213)
StringBuffer class : Example 1 StringBuffer strb1 = new StringBuffer(); System.out.println(strb1.length()); 0 System.out.println(strb1.capacity()); 16
StringBuffer class : Important Methods void setCharAt(int where, char ch) sets character ch at where index. (0 <=where<=L, where L is length) StringBuffer append(String str) appends str at the end of invoking string buffer instance StringBuffer append(int num) appends a character represented by num at the end of invoking string buffer instance StringBuffer append(Object obj) appends the string form of obj at the end of invoking string buffer instance String form of obj is determined by toString() method String insert(int index, String str) Inserts String str at index in the invoking string buffer instance [ 0 <= index <= L, L is length] String insert(int index, char ch) Inserts character ch at index in the invoking string buffer instance [ 0 <= index <= L, L is length] String insert(int index, Object obj) Inserts Object obj at index in the invoking string buffer instance [ 0 <= index <= L, L is length] obj is first converted to String form by calling toString() method and then the resultant string form of obj is inserted at index in the invoking string buffer instance
5 Object-Oriented Programming (CS F213)
StringBuffer class : Important Methods . StringBuffer reverse() reverses the characters of the invoking string buffer instance and returns the reversed string StringBuffer deleteChatAt(int loc) deletes a single character at loc and returns the updated string. [0 <=loc <=L, L is the length of string buffer] StringBuffer delete(int start, int end) deletes sequence of characters from index start (inclusive) to index end (exclusive) and returns the updated string. Characters will be deleted from indexes start to end-1. [0 <=start,end <=L and the value of end should not be less than start] StringBuffer replace(int startIndex, int endIndex, String str) Replaced the character sequence from startIndex to endIndex by str. Characters will be replaced from indexes startIndex to endIndex-1.
6 Object-Oriented Programming (CS F213)
String Buffer Example 2 StringBuffer strb1 = new StringBuffer("Object Oriented Programming"); System.out.println(strb1.length()); 27 System.out.println(strb1.capacity()); 43
setLength() void setlength(int len) Sets the current lentgth of the invoking string buffer object to len. Changing length may affect capacity also Suppose L is the current length of a string buffer instance and C is the current capacity. If len < L (current length) then L-len characters will be removed from the end of the string buffer instance and len becomes the new length of string buffer but no change in C If len > L (current length) && len <= C (current capacity) then extra white space characters will be appended in the end of the string buffer instance and len becomes the new length of string buffer but no change in C If len > C (current capacity) && len <= 2*C + 2 then extra white space characters will be appended in the end of the string buffer instance and len becomes the new length of string buffer and new capacity = 2 * C + 2. If len > 2*C + 2 then extra white space characters will be appended in the end of the string buffer instance and len becomes the new length of string buffer and new capacity = len.