STRING AND STRING BUFFER
String class is a immutable class . Immutable classes are those classes in which if we create object of that class and after that alter the content of that object then every time a new object is created that store the altered content
String buffer class is mutable class . Mutable classes are those classes in which if we create object of that class and after that alter the content of that object that changes will occur in same object . No new object is created .
EXAMPLE :
String s = new String("Hello");
s=s+"World";
Understand that , a string s is created in line 1 in which initially we put "Hello" in string s . And in statement second we have concatenated string s . And add further "World " to this .
As this is a string class and string classes are immutable . Hence , world will not be stored in same object where hello was stored . A new object will be created every time we concatenate the string or add anything in the string.
As this is a string class and string classes are immutable . Hence , world will not be stored in same object where hello was stored . A new object will be created every time we concatenate the string or add anything in the string.
Now , consider the following set code :
StringBuffer b = new StringBuffer("Hello");
b.append("World");
Here , in the first line , string buffer is declared and in the object Hello is stored . When second line is read , to add more contents to string s we use keyword append . It does not create a new object. World is added to the existing object which contains Hello .
NOTE :
If content of string changes rapidly then instead of String , String buffer is convenient and is suggested .
Consider a String defined this way :
String s="Hello World";
We have a list of methods :
Understanding the above mentioned methods :
s.length():
It will display out the characters used in the string . To print the number of characters on screen :
System.out.println(s.length(););
And for above string s it would be 11 . Note that space is also considered to be a character.
s.charAt(n);
This methods gives the character present at nth position . Positions are considered by system in the following way :
Hello World
01234567891011
Hence ,
s.charAt(0) -- > H
s.charAt(1) -- > e
s.charAt(2) -- > l
s.charAt(3) -- > l
s.charAt(4) -- > 0
s.charAt(5) -- > ' '
s.charAt(6) -- > W
s.charAt(7) -- > o
s.charAt(8) -- > r
s.charAt(9) -- > l
s.charAt(10) -- > d
s.toLowerCase
This method changes the string s to Lower case i.e. all the characters in string s are changed to Lower case characters.
s.toUpperCase
This method changes all the characters of string s in Upper case characters i.e capital letters .
s.indexOf('l'):
This method gives the position or location of character l . Also in other words it is a kind of reverse of s.charAt method.
Index of missing character is defined as -1.
s.replace('l','*');
It will replace the character l in the string with the character * . Generally .
In s.replace('x','y') , character x will be replaced by character y.
s.subString(6);
It will display all the character from position6 till the end of string . In other words , substring of characters from character 6 till end would be accessed.
s.subString(3,6);
If a single argument is passed through substring it will display the characters from 6 till end.In case of two arguments it will check from 3 to 6 .
s.equals("xyz");
It checks weather string in brackets i.e string xyz is same as string s or not . IT IS CASE SENSITIVE .
s.equalsIgnoreCase("xyz");
It is not case sensitive. This is only difference in s.equals and this method.
String s[] = s.split("x");
This method is used to split the string from character x.
If we split the string s from l , then:
String s[]=s.split("l");
Four strings would be formed .
He
"" // an empty string as l appears two consecutive times
o Wor
d
NOTE :
If content of string changes rapidly then instead of String , String buffer is convenient and is suggested .
Methods in CLASS BUFFER:
Consider a String defined this way :
String s="Hello World";
We have a list of methods :
- s.length();
- s.charAt(n);
- s.toLowerCase();
- s.toUpperCase();
- s.indexOf('char');
- s.lastIndexOf('char');
- s.replace('x','y');
- s.subString(x);
- s.subString(x,y);
- s.equals("xyz");
- s.equalsIgnoreCase("xyz");
Understanding the above mentioned methods :
s.length():
It will display out the characters used in the string . To print the number of characters on screen :
System.out.println(s.length(););
And for above string s it would be 11 . Note that space is also considered to be a character.
s.charAt(n);
This methods gives the character present at nth position . Positions are considered by system in the following way :
Hello World
01234567891011
Hence ,
s.charAt(0) -- > H
s.charAt(1) -- > e
s.charAt(2) -- > l
s.charAt(3) -- > l
s.charAt(4) -- > 0
s.charAt(5) -- > ' '
s.charAt(6) -- > W
s.charAt(7) -- > o
s.charAt(8) -- > r
s.charAt(9) -- > l
s.charAt(10) -- > d
s.toLowerCase
This method changes the string s to Lower case i.e. all the characters in string s are changed to Lower case characters.
s.toUpperCase
This method changes all the characters of string s in Upper case characters i.e capital letters .
s.indexOf('l'):
This method gives the position or location of character l . Also in other words it is a kind of reverse of s.charAt method.
Index of missing character is defined as -1.
s.replace('l','*');
It will replace the character l in the string with the character * . Generally .
In s.replace('x','y') , character x will be replaced by character y.
s.subString(6);
It will display all the character from position6 till the end of string . In other words , substring of characters from character 6 till end would be accessed.
s.subString(3,6);
If a single argument is passed through substring it will display the characters from 6 till end.In case of two arguments it will check from 3 to 6 .
s.equals("xyz");
It checks weather string in brackets i.e string xyz is same as string s or not . IT IS CASE SENSITIVE .
s.equalsIgnoreCase("xyz");
It is not case sensitive. This is only difference in s.equals and this method.
String s[] = s.split("x");
This method is used to split the string from character x.
If we split the string s from l , then:
String s[]=s.split("l");
Four strings would be formed .
He
"" // an empty string as l appears two consecutive times
o Wor
d