Friday, March 29, 2013

String class and StringBuffer class

A String is a group of characters.

String classes are used to represents these group of characters.

We can even create an object of a string class without using ‘new’ operator. Instead of the ‘new’ operator we use double quotes(“”).

As soon as JVM encounters “ “, it understands  that it is has to create a object of the String class with the contents present in “ “ and for each and every individual character inside ” “ it assigns a unique index value starting from Zero.

Ex: String s=”abcde”;

Address of the object is assigned to the variable s.

This object has a very near relation with arrays.

Once a String object is created, the data inside the object cannot be modified. This is the reason. Why, we say that , String are mutuable.

Different ways of creating String objects are shown below.

String s1 = new String ();
String s2 = new String (“abc”);
String s3 =  “ MSDASS”;

Consider the following two statements.
A a1 = new A();
String s1 = “abcde”;

Both the above statements create the object of their respective classes.

Now, when we say

System.out.println(a1);  --  Its prints the address of the object,

whereas for  System.out.println(s1); -- it prints abcde (i.e the contents of the object).Why?

The explanation is related to static polymorphism.The prinln() method,called in the above two cases is not the same.

One println() method is defined to accept object of object class and its functionality is to print the address of the onject.

The second println() method is defined to accept object of String class and its functionality is to print the contents of the object pointed by the address.

Question:

 What is the difference b/w creating the string object using new operator and creating the string object using double quotes?

Ans : Using new operator, we can create many objects of the same String class.

Ex:
 String s1 = new String (“xyz”);
String s2  = new String (“xyz”) ;

Here, through the contents are same, two different objects are created and the address are assigned to different variables.

But when we create objects of string class using double quotes, we can almost create only one object.

If we try to create more objects, the same address of the already created object will be assigned to the variables pointing to the newly created objects.

Ex:
String s1 = “MSD”;
String s2 = “MSD”;

The above example explained difference is illustrated in the following program.

Class Sdemo1
{
Public Static void main(String args[])
{
String s1 = “abc”;
String s2 = new String (“xyz”);
String s3 = new String (“xyz”);
If(s2 == s3){
System.out.println(s2 and s3 are equal);
else{
System.out.println(s2 and s3 are not equal); //this statement is executed
}
String s4 = “abc”;
If(s1==s4){
System.out.println(s1 and s4 are equal); //this statement is executed
}else{
System.out.println(s1 and s4 are not equal);
}
}
}

String Concatenation:

Appending a string to another string is known as string concatenation.

The ‘+’ symbol acts as the concatenation operator.

Note: + operator works as an arithmetic operator if and only if the two operator operands supplied to the + operator are arithmetic.

+ operator works as a concatenation operator if atleast one operand supplied to the + operator is a String  object.

Concatenation operator always outputs a new String class object with the contents of LHS and RHS of the + operator.

Ex:
String s1 = “abc” + “xyz”;

System.out.println(s1); //it prints abcxyz

String s2 = “MSD”;

S2= s2 + “SSA”;

System.out.println(s2); //it prints  MSDSSA


Question:

We said that Strings are immutable(i.e: contents of the object of string class cannot be changed), but  how are the above statements possible?

Ans: Actually, the content of the object pointed by s2 is not changed. Infact, a new object is created and the same new content is appended to the existing content and this is stored is in the object and the address of the object is assigned to variable.


StringBuffer Class:

We said that Strings are immutable. But we have another class by name StringBuffer class, the contents of where objects are mutable.

i.e: we can alter the contents of the object which is created upon the stringbuffer class.

Ex:
StringBuffer sb1 = new StringBuffer (“abc”);

Some of the methods available in the StringBuffer class are

append();
remove();
replace(); etc.

By using these methods, we can alter the contents of the object od StringBuffer class.

StringBuffer sb2= new String (“xyz”);

String s3 = new String (“abc”);

Both the above statement s are not the valid statement s .The reason  is , we are typing  to assign 
 object of one class to reference of another class which is against the rules that an object of a class  should be assigned to the reference variable of the type same class.











No comments:

Post a Comment