"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."
Call +91 74 88 34 7779 | Email : email@example.com
Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.
|StringBuffer()||creates an empty string buffer with the initial capacity of 16.|
|StringBuffer(String str)||creates a string buffer with the specified string.|
|StringBuffer(int capacity)||creates an empty string buffer with the specified capacity as length.|
|Modifier and Type||Method||Description|
|public synchronized StringBuffer||append(String s)||is used to append the specified string with this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc.|
|public synchronized StringBuffer||insert(int offset, String s)||is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc.|
|public synchronized StringBuffer||replace(int startIndex, int endIndex, String str)||is used to replace the string from specified startIndex and endIndex.|
|public synchronized StringBuffer||delete(int startIndex, int endIndex)||is used to delete the string from specified startIndex and endIndex.|
|public synchronized StringBuffer||reverse()||is used to reverse the string.|
|public int||capacity()||is used to return the current capacity.|
|public void||ensureCapacity(int minimumCapacity)||is used to ensure the capacity at least equal to the given minimum.|
|public char||charAt(int index)||is used to return the character at the specified position.|
|public int||length()||is used to return the length of the string i.e. total number of characters.|
|public String||substring(int beginIndex)||is used to return the substring from the specified beginIndex.|
|public String||substring(int beginIndex, int endIndex)||is used to return the substring from the specified beginIndex and endIndex.|
A string that can be modified or changed is known as mutable string. StringBuffer and StringBuilder classes are used for creating mutable string.
The append() method concatenates the given argument with this string.
The insert() method inserts the given string with this string at the given position.
The replace() method replaces the given string from the specified beginIndex and endIndex.
The delete() method of StringBuffer class deletes the string from the specified beginIndex to endIndex.
The reverse() method of StringBuilder class reverses the current string.
The capacity() method of StringBuffer class returns the current capacity of the buffer. The default capacity of the buffer is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.
The ensureCapacity() method of StringBuffer class ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.
The java.lang.String class provides a lot of methods to work on string. By the help of these methods, we can perform operations on string such as trimming, concatenating, converting, comparing, replacing strings etc.
Java String is a powerful concept because everything is treated as a string if you submit any form in window based, web based or mobile application.
Let's see the important methods of String class.
The java string toUpperCase() method converts this string into uppercase letter and string toLowerCase() method into lowercase letter.
The string trim() method eliminates white spaces before and after string.
The string charAt() method returns a character at specified index.
The string length() method returns length of the string.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
The string valueOf() method coverts given type such as int, long, float, double, boolean, char and char array into string.
The string replace() method replaces all occurrence of first sequence of character with second sequence of character.
A part of string is called substring. In other words, substring is a subset of another string. In case of substring startIndex is inclusive and endIndex is exclusive.
You can get substring from the given string object by one of the two methods:
In case of string:
Let's understand the startIndex and endIndex by the code given below.
In the above substring, 0 points to h but 2 points to e (because end index is exclusive).
In java, string concatenation forms a new string that is the combination of multiple strings. There are two ways to concat string in java:
Java string concatenation operator (+) is used to add strings. For Example:
The Java compiler transforms above code to this:
In java, String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append method. String concatenation operator produces a new string by appending the second operand onto the end of the first operand. The string concatenation operator can concat not only string but primitive values also. For Example:
The String concat() method concatenates the specified string to the end of current string. Syntax:
Let's see the example of String concat() method.
We can compare string in java on the basis of content and reference.
It is used in authentication (by equals() method), sorting (by compareTo() method), reference matching (by == operator) etc.
There are three ways to compare string in java:
The String equals() method compares the original content of the string. It compares values of string for equality. String class provides two methods:
In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.
Once string object is created its data or state can't be changed but a new string object is created.
Let's try to understand the immutability concept by the example given below:
Now it can be understood by the diagram given below. Here Sachin is not changed but a new object is created with sachintendulkar. That is why string is known as immutable.
As you can see in the above figure that two objects are created but s reference variable still refers to "Sachin" not to "Sachin Tendulkar".
But if we explicitely assign it to the reference variable, it will refer to "Sachin Tendulkar" object.For example:
In such case, s points to the "Sachin Tendulkar". Please notice that still sachin object is not modified.
|Because java uses the concept of string literal.Suppose there are 5 reference variables,all referes to one object "sachin".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.|
The String equalsIgnoreCase() method compares the two given strings on the basis of content of the string irrespective of case of the string. It is like equals() method but doesn't check case. If any character is not matched, it returns false otherwise it returns true.
str : another string i.e. compared with this string.
It returns true if characters of both strings are equal ignoring case otherwise false.
The java string equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.
The String equals() method overrides the equals() method of Object class.
anotherObject : another object i.e. compared with this string.
true if characters of both strings are equal otherwise false.
equals() method of java Object class.
The java string endsWith() method checks if this string ends with given suffix. It returns true if this string ends with given suffix else returns false.
The syntax or signature of endsWith() method is given below.
suffix : Sequence of character
true or false
The java string contains() method searches the sequence of characters in this string. It returns true if sequence of char values are found in this string otherwise returns false.
The signature of string contains() method is given below:
sequence : specifies the sequence of characters to be searched.
true if sequence of char value exists, otherwise false.
NullPointerException : if sequence is null.