MY mENU


Thursday, 31 January 2013

Strings in Java


In java.lang package we have below three classes to handle strings
  • String
  • StringBuffer
  • StringBuilder--- introduced in java 5, as non-thread safe class.
  1. These three are subclasses of CharSequence interface.
  2. All these three classes are final classes.
  3. These are subclasses of java.io.Serializable interface,
  4. String class is a sub class of comparable interface, but StringBuffer and StringBuilder are not.

String is immutable where as StringBuffer is mutable. Strings are constant their values cannot be changed in the same memory after they are created, so string is defined as an immutable sequence of characters.

String class is given when char array is available to represent sequence of characters because of its given to store characters dynamically without size limitation.

The possible ways to create String Object:
  • By assigning string literal directly   --- String s=”abc”;
  • By using any one of the string class constructors.

String class Constructors:
  • String(), no null string object
  • String(String value)
  • String(StringBuffer sb)
  • String(StringBuilder sb)
  • String(char[] ch)
  • String(char[] ch, int offset, int count)
  • String(byte[] ch)
  • String(byte[] ch, int offset, int count)

We should not pass null to create string object using any one of the above constructors it leads to RE: java.lang.NullPointerException

We can create null string referenced variable like string str=null;

But we cannot create String object with null, it leads NPE, like String str=new String(null);

What is the difference between creating String objects using constructor and by assigning literal?
String Pooling means pooling strings, it means grouping string objects. If we create String objects by assigning same string literal, only one object is created and all referenced variables are initialized with that same String object reference. Then all referenced variables are pointing to same String object. This is the another reason why String object become immutable.

Why because, if we change that String value using one referenced variable, if that modification is stored in same memory, other referenced variables are also affected. To solve this problem SUN decided to create String as immutable.

This string pooling concept is not applicable for the String objects those are created using “new and constructor”, all string objects created by using constructor will have separate object.

  • String is an immutable sequence of characters.
  • StringBuffer is a mutable sequence of characters.
  • StringBuilder is also mutable sequence of characters.
  • StringBuffer object is thread-safe; it means its object is not modified by multiple threads concurrently, because all its methods are declared as “Synchronized”.
  • StringBuilder as a non-thread-safe class means all its methods are non-synchronized methods.

So in single thread model application we must use StringBuilder, so that object locking and unlocking will not be there, hence performance is increased. In single thread model application operations are executed in sequence hence there is no chance StringBuffer or StringBuilder.

  • If we do not want to store string modifications in the same memory, we must choose string.
  • To modifications in the same memory, we must choose StringBuffer or StringBuilder.

Advantage and Disadvantage in String:
Since modifications are preservating in another memory location, we will have both original and modified values.

Disadvantage is it consumes lot memory for every operation, as it stored it modifications in new memory. So it leads to performance issue.

To solve this performance issue,  in projects developers store data using StringBuilder. After all modifications they convert SB into String and then pass it back to user.

Advantages in StringBuffer and StringBuilders are given high performance because consumes less memory as all modifications are stored in same memory. Disadvantage is original value will not be preserved.

StringBuffer Operations :
  • Append
  • Insert
  • Delete
  • Reverse

We cannot perform these operations on String.

Using String object we can concat new string to the current string in two ways.
  • Using “+” operator
  • Using concat() operator.
Using StringBuffer object we can concat new string to the current string only in one way.
  • Using append() method.

Due to concatenation operation by using concat() method, if current string object is modified, JVM creates new object and returns that object reference. If it is not modified it returns the same current object reference.
But when we use + operator whether current string object is modified or not modified JVM always creates new String Object.

The append() operation performed on StringBuffer , it always stores modification in same memory and returns the same SB object reference.

  • Concatenation means appending given string to current string in difference memory.
  • Appending means appending given string to current string in same memory.

Using equals() method String objects are compared with state, because it is overridden in this class. Also hashcode method is override to satisfy equals method contract.

But in StringBuffer and StringBuilder classes equals() method is not overridden, so using equals() method their objects are compared with reference.

In all three class toString() method is overridden to print the object state.

StringBuffer has default capacity 16 buffers. It increases its capacity automatically when size reached capacity.

  • Using String(StringBuffer) constructor and StringBuffer.toString() we can convert StringBUffer to string.
  • Using StringBuffer(String) constructor we can convert String to StringBuffer.

What is the best design in defining a method to take and return string data as argument?
The parameter and return type should be java.lang.String, not StringBuffer.
The problem if we take StringBuffer as parameter or return type is: user should convert that string data as StringBuffer object, because StringBuffer will not accept string data directly.





1 comment:

Anonymous said...

hi srinivas you copied this content from our harikrishna sir s notes please delete it from your blog

Post a Comment