Our social:

Wednesday, October 5, 2016

StringBuffer and StringBuilder-Tutorial

More Advanced Methods in String Class

What are equals(), equalsIgnoreCase() method? 


All these 2 methods are used for String comparison. Here we need to remember that we cannot compare Strings using == operators. Because == operators are used to compare objects references.
equals() method compares the String case-sensitively
equalsIgnoreCase() method compares the Strings irrespective of their cases.
Example:
Try It
public class Compare 
{
 public static void main(String[] args) 
 {
  String a = "Welcome";
  String b = "Welcome";
  String c = "welcome";
  String d = "compareStrings";
  
  System.out.println(a.equals(b));
  System.out.println(a.equals(c));
  System.out.println(a.equalsIgnoreCase(c));
  System.out.println(a.equalsIgnoreCase(d));
 }
}
Click "Try It" button to execute above code. Output will be:
true
false
true
false
From "String in immutable" to "Concepts of String Pool"
String is immutable. Okay… we know it, but what does “immutable” means?
Well, immutable means, that once a string is created, it cannot be destroyed, neither can it be modified until JVM is restarted.
For example, if we do –
String a = "Java";
a = a + " Test";
By now, we can tell that this will assign value “Java Test” in variable a. But, internally that doesn’t quite do so. What it does is, it creates a new string “Java Test”, and point the variable ‘a’ to that memory location. So, now, variable ‘a’ have a value “Java Test”. So, internally there are 2 memory locations allocated for string “Java” and “Java Test”.
Programmatically we can prove that –
Try It
public class Immutable 
{
 public static void main(String[] args) 
 {
  String a = "Java";
  String b = a;
  System.out.println(a == b);
  a = a + "Test";
  System.out.println(a == b);
 }
}
So, it looks like that there’s a lot of memory is wasted if we use String all over our code, right? But, if we look close, certainly there is some facilities of using String. For that we need to know what String pool is.

String Pool

Whenever we are creating a new string using string literal (String a = "Welcome To Java";), it first searches in the String Pool. If that String already exists in there, it uses the reference of the same string in the program. If, it does not exist, it allocates memory for the new string and put it in the String Pool, so that the new variables can use its reference.
Again, when we create String objects by using new(), it doesn’t check in String Pool and it creates an object right there. So, it is a better habit to create strings using string literals.

Facilities of Using String

When, we are using same String again and again all over the program, it is better idea to use a String. Because, every time, it will not allocate memory location, rather, it will use the same reference again and again.

StringBuffer and StringBuilder

StringBuffer and StringBuilder are classes which provides almost same functionalities as String Class. 
Try It
public class StringBufferExample 
{
 public static void main(String[] args) 
 {
  StringBuffer sb = new StringBuffer("String Buffer Example");
  StringBuilder sb1 = new StringBuilder("String Builder Example");
  
  System.out.println(sb);
  System.out.println(sb1);
 }
}
Click "Try It" button to execute above code. Output will be:
String Buffer Example
String Builder Example

Difference between String, StringBuffer and StringBuilder

Properties
String
StringBuffer
StringBuilder
immutable
YES
NO
NO
Synchronized
N/A as String is Immutable
YES
NO
So, by performance StringBuilder is fastest as it is both not immutable and non-synchronized.

When to use String , StringBuffer and StringBuider?

When, we are using same String again and again all over the program, it is better idea to use a String. Because, every time, it will not allocate memory location, rather, it will use the same reference again and again. It can produce better performance.

If the object is not used by multiple threads it is better to use StringBuilder. If not used by multiple threads, it needs not to be synchronized.
If the String is going to be used by multiple threads in the application, then, we need synchronization and StringBuffer is synchronized by default.

0 comments:

Post a Comment

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you