Sunday, February 15, 2009

String class

String class it is there in Java.lang package.
Immutability of the String object:
String s=”java”;
s.concat(“soft”);
System.out.println(s);
output:- jaVa
Muatability of StringBuffer object:
StringBuffer s=new StringBuffer(“java”);
s.append(“soft”);
System.out.println(s);
output:- javasoft

  • Once we created a string object we are not allowed to perform any changes in the existing String object.
  • If you want to perform any changes (by using concat() or any other), with the changes a new String object will create.
  • Hence String objects are considered as Immutable
  • In the case of StringBuffer, once we created a StringBuffer object, we are allowed to perform any changes in the existing StringBuffer object only.
  • Hence StringBuffer object is mutable
1. String s1=new String(“java”);
2. String s2=new String(“java”);
System.out.println(s1==s2); --> false
System.out.println(s1.equals(s2)); --> true
But
3. StringBuffer s3=new StringBuffer(“viswan”);
4. StringBuffer s4=new StringBuffer(“viswan”);
System.out.println(s1==s2); --> false
System.out.println(s1.equals(s2)); --> false

  • In the String class .equals() is overridden for content comparision. Here the content of s1 and s2 are equal, that’s why s1.equals(s2) returns true.
  • In the String class .equals() is not overridden for content comparision. whenever we calling .equals()
Method on the StringBuffer object, Object class .eqauals() method will execute, which is meant for address comparision only.
Hence s3.equals(s4) returns false eventhough the contents of s3 and s4 are same.
Difference between the ways of creating String objects:-
1. String s1=”durga”;
2. String s2=new String(“durga”);
• In the first case, the JVM will check, is the any String object with content durga in the “String Constant Pool”. If there is no such object then only a new String object will create and s1 will pointed to that object.
• If the object already present then s1 will simply refers to that object instead of creating a new object.
• In the second case, two String objects will create, one is on the heap, for the other one in the “String Constant Pool” (If the object is not already present) and s2 will pointing to heap object.
Ex: String s=”viswan”;
s.concat(“sw”);

* In this content, three objects are created. One is on heap and another two on “String Constant Pool”

• “String Constant Pool” never allows Garbage Collection.
• Heap durgasw
String Constant Pool software
• I the object does not have any references in the “String Constant Pool”, still this is not eligible for the Garbage Collection.
• All the objects present in the “String Constant Pool” destroy, whenever the JVM restarted.
What is the output and how many String objects are created.
String s1=”spring”;
String s2=s1+”summer”;
s1.concat(“fall”);
s2.covcat(“s1”);
s1+=”winter”;
System.out.println(s1+ “,”+s2);
output: spring , springsummer
string constant pool: spring, summer, fall, winter
Heap: springsummer, springfall, springwinter, springsummerspring
Advantage Of StringConstantPool:
String s1=”you can’t change me”;
String s2=”you can’t change me”;
System.out.println(s1==s2); // true
System.out.println(s1.equals(s2)); //true
String s3=new String(“you cannot change me”);
System.out.println(s1==s3);//false
System.out.println(s1.equals(s3)); //true
String s4=”you can’t” + “change me”;
System.out.println(s1==s4);
String s5=”you can’t”;
String s6=s5+”change me”;
System.out.println(s1==s6); //false
If final String s7=”you can’t”
System.out.println(s1==s7); //true
String s8= s7+”change me”;
s1==s8; //true.
• In our programme., if any String object is repeatedly going to use, we can keep only one copy in the String Constant Pool and shared by several required references. Instead of creating several same content object, we are creating only one object, this is very efficient w.r.t to memory point of view.
• As several refernces pointing to the same object in the StringConstantPool, by using any references , if you allowed to use the content of that object, the remaining references have to suffer.
• Hence once we created a string object we are not allowed to change content.
• If you want to perform any changes, with those changes a new String Object wil create . Hence the String Objects are declared as the Immutable.
• StringConstantPool ---> Performance is improved. (advantages)
----> Immutability ( disadvantages)
Interning Of Strings:
String s1=new String(“viswan”);
String s2=s1;
String s3=s1.intern();
S1==s3; //false
String s4=”viswan”;
s4==s3; //true
• Intern() method used to point the String constant pool object instead of heap object.
String Class Constructors:
1. String s=new String(String s);
2. String s=new String();
3. String s=new String(StringBuffer s1);
4. StringBuffer s1=new StringBuffer(“xxx”);
5. String s=new String(byte[]);
Ex: byte[] b={100,101,102,102}
String s=new String(b);
System.out.println(s); //”defg”
6. String s=new String(char[]);
Ex: Char[] ch={a,b,cd};
String s=new String(ch);
System.out.println(ch); // abcd
String Class Methods (only some imp):
1. public char charAt(int index):
This method returns the character located at the string’s specified index. Remember that String indexes are zero based.
2. public String concat(String s):
This method returns a string with the value of the String passed into the method appended to the end of the String used to invoke the method.
The overloaded + and += operators perform the same function of concat() method.
Ex: String s=”durga”;
s=s.concat(“sw”);
s=s+”sw”;
s+=”sw”;
System.out.println(s); //durgasw
3. public Boolean equals IgnoreCase(String s):
This method returns Boolean value ie., true or false depending on whether the value of the string in the argument is the same as the value of the String used to invoke the method.
Ex: String s=”DURGA”;
System.out.println(s.equalIgnoreCase(“Durga”); //true
s.equals(“Durga”); //false
4. public int length():
length() is the method in the case of String objects where as ‘length’ is the variable in the case of Arrays.
Ex: String s= “durga”;
System.out.println(s.length); //CTE
System.out.println(s.length());// 5
5. public String replace(char old, char new):
String x=”ababab”;
System.out.println(x.replace(‘a’,’b’); //bbbbbb
6. a). public String substring(int begin):
String x=”0123456789”;
System.out.println(s.subString(6)); //6789
b). public String substring(int begin, int end):
System.out.println(s.subStrin(5,8)); //567

* End is not included in o/p.
* This is already overloaded method.
* It considers the index only.

String s=”abcdefgh”;
System.out.println(s.subString(3)); //defgh
System.out.println(s.subString(4,7)); //efg
7. public String toUpperCase():
String s= “ A New Moon”;
System.out.println(s.toUpperCase()); // A New Moon
8. public String toLowerCase()
String s= “ A New Moon”;
System.out.println(s.toLowerCase()); // A New Moon
9. public String trim()
String s=”viswan”;
System.out.println(s.trim()); //viswan
---> Leading or trailing blackspaces removed but not in the middle.
10. public int indexOf(char ch)
---> Returns the index of the first occurance of the specified character.
---> Returns -1, if there is no such character.
String s=”durgascjp”;
s.indexOf(d); //0
11. public int lastIndexOf(char ch)
---> Returns the last occurrence index of the specified character.
String s=”durga”;
String y=s.toUpperCase();
String s=s.toLowerCase();
System.out.println(s==x);//true
System.out.println(s==y); // false

s,x ---> durga y---> durga

• After applying any method of the contest of the String has to change then only a new String object will create with the corresponding changes.

Ex1:
String s1=”abc”; s1 ---> abc
String s2=”def”; s2 ---> def <--- s3
String s3=”s2”; s2 ---> ghi
String s2=”ghi”;
S.o.p(s1+s2+s3); //abcghidef
Ex 2:
String x=new String(“xyz”);
y=”abc”;
x=x+y;
total objects are 4 ---> 2 on heap, 2 on string constant pool

* String objects as well as wrapper class objects are immutable.
* String, StringBuffer, wrapper, Math, StringBuilder classes are the final classes. So, we are not allowed to create the child classes.

0 comments:

Post a Comment