"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."
Call +91 74 88 34 7779 | Email : anishsingh@live.com
There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the wrapper classes and String class is immutable. We can also create immutable class by creating final class that have final data members as the example given below:
In this example, we have created a final class named Employee. It have one final datamember, a parameterized constructor and getter method. |
The above class is immutable because:
There are many differences between StringBuffer and StringBuilder. A list of differences between StringBuffer and StringBuilder are given below:
No. | StringBuffer | StringBuilder |
---|---|---|
1) | StringBuffer is synchronized i.e. thread safe. It means two threads can't call the methods of StringBuffer simultaneously. | StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously. |
2) | StringBuffer is less efficient than StringBuilder. | StringBuilder is more efficient than StringBuffer. |
hellojava
hellojava
Let's see the code to check the performance of StringBuffer and StringBuilder classes.
Time taken by StringBuffer: 16ms Time taken by StringBuilder: 0ms
There are many differences between String and StringBuffer. A list of differences between String and StringBuffer are given below:
No. | String | StringBuffer |
---|---|---|
1) | String class is immutable. | StringBuffer class is mutable. |
2) | String is slow and consumes more memory when you concat too many strings because every time it creates new instance. | StringBuffer is fast and consumes less memory when you cancat strings. |
3) | String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method. | StringBuffer class doesn't override the equals() method of Object class. |
Time taken by Concating with String: 578ms Time taken by Concating with StringBuffer: 0ms
As you can see in the program given below, String returns new hashcode value when you concat string but StringBuffer returns same.
Hashcode test of String: 3254818 229541438 Hashcode test of StringBuffer: 118352462 118352462
Java StringBuilder class is used to create mutable (modifiable) string. The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized. It is available since JDK 1.5.
Constructor | Description |
---|---|
StringBuilder() | creates an empty string Builder with the initial capacity of 16. |
StringBuilder(String str) | creates a string Builder with the specified string. |
StringBuilder(int length) | creates an empty string Builder with the specified capacity as length. |
Method | Description |
---|---|
public StringBuilder 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 StringBuilder 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 StringBuilder replace(int startIndex, int endIndex, String str) | is used to replace the string from specified startIndex and endIndex. |
public StringBuilder delete(int startIndex, int endIndex) | is used to delete the string from specified startIndex and endIndex. |
public StringBuilder 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. |
Let's see the examples of different methods of StringBuilder class.
The StringBuilder append() method concatenates the given argument with this string.
The StringBuilder insert() method inserts the given string with this string at the given position.
The StringBuilder replace() method replaces the given string from the specified beginIndex and endIndex.
The delete() method of StringBuilder class deletes the string from the specified beginIndex to endIndex.
The reverse() method of StringBuilder class reverses the current string.
The capacity() method of StringBuilder class returns the current capacity of the Builder. The default capacity of the Builder 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 StringBuilder 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.
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.
Constructor | Description |
---|---|
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.
SACHIN sachin Sachin
The string trim() method eliminates white spaces before and after string.
Sachin Sachin
true true
The string charAt() method returns a character at specified index.
S h
The string length() method returns length of the string.
6
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.
Sachin
The string valueOf() method coverts given type such as int, long, float, double, boolean, char and char array into string.
Output:
1010
The string replace() method replaces all occurrence of first sequence of character with second sequence of character.
Output:
Kava is a programming language. Kava is a platform. Kava is an Island.
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).
Tendulkar Sachin
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:
Output:Sachin Tendulkar
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:
80Sachin4040
The String concat() method concatenates the specified string to the end of current string. Syntax:
Let's see the example of String concat() method.
Sachin Tendulkar
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:
Output:true true false
Output:false true
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:
Output:Sachin
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:
Output:Sachin Tendulkar
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. |