It is a jdk 1.5 specification that means this class is added to java in the release of jdk 1.5, like StringBuffer class, StringBuilder class objects are also mutable.
The main differences between these two things are
1.StringBuffer class is synchronized and StringBuilder is not.
2.In case of mutithreading StringBuffer is more reliable than StringBuilder.
3. In case of single thread execution StringBuilder is preferable, as it improves execution of time.
Here are the StringBuilder constructors
- StringBuilder():- Constructs a string builder with no characters in it and an initial capacity of 16 characters.
- StringBuilder(CharSequence seq):- Constructs a string builder that contains the same characters as the specified CharSequence.
- StringBuilder(int capacity):- Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument.
- StringBuilder(String str):- Constructs a string builder initialized to the contents of the specified string.
There are so may useful methods are available in java StringBuilder, here I am presenting some of the most frequently used methods. If you want complete method and their description you can visit the official website.
- append(char c) - Appends the string representation of the char argument to this sequence.
- capacity() - Returns the current capacity.
- charAt(int index) - Returns the char value in this sequence at the specified index.
- deleteCharAt(int index) - Removes the char at the specified position in this sequence.
- indexOf(String str, int fromIndex) - Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
- length() - Returns the length (character count).
- reverse() - Causes this character sequence to be replaced by the reverse of the sequence.
- setCharAt(int index, char ch) - The character at the specified index is set to ch.
- substring(int start) - Returns a new String that contains a subsequence of characters currently contained in this character sequence.
- toString() - Returns a string representing the data in this sequence.
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.