String and StringBuffer in java


Strings in Java(java.lang.String Class)
1. String is a class which is used to store group of chars in JAVA.
2. In c/c++ if we want to store strings means we need to take the help of Char arrays.
3. In JAVA, that is provided with a powerful derived datatype known as  “ String”.
       String   s = “abc”;
  It internally stores by compiler like this: ---
           Char s [] = {‘a’,’b’,’c’);
           String s= new String(s);
4. To create any String type of object we don’t need to use any ‘new’ keyword.
5. But can also create, no error,but no need
String    s = new    String (“admin”);
Note:- String class objects are immutable
Ex:--
Class strDemo {
public static void main(String [] args){
String str1="string one";
String str2="two";
String str3=str1;
String str4="string one";
//to find the length of a string
System.out.println("\n length of a str1 is:"+str1.length());
//to find the character at perticular position
System.out.println("\n 5th char in  str1 is:"+str1.charAt(5));
//equals() and '=='
if(str1.equals(str4))
System.out.println("\n both are equal");
//'==' compare two object references
System.out.println(str1+"=="+str3+"->"+(str1==str2));
//equals() and equalsIgnoreCase()
if(str1.equalsIgnoreCase(str4))
System.out.println("\nstr1==str4,case ignored");
else
System.out.println("not equal");
//using concat()
String str5=str1.concat(" ,concat() function performed");
System.out.println("\n str5 is:"+str5);
}
}


StringBuffer , StringBuilder(jdk1.5) Class
The StringBuffer class is used to represent characters that can be modified. This is simply used for concatenation or manipulation of the strings. StringBuffer is mainly used for the dynamic string concatenation which enhances the performance.
The StringBuilder class introduced in jdk1.5 ,which is also generates a mutable sequence of characters like StringBuffer class . StringBuilder is not synchronized where as StringBuffer objects are synchronized.
Note:- A string buffer implements a mutable sequence of characters.
Methods :
append ()
This is the append() function used for the concatenate the string in string buffer
insert()
This is the insert() function used to insert any string or character at the specified position in the given string.
reverse()
This is the reverse() function used to reverse the string present in string buffer.
setCharAt()
This is the setCharAt() function which is used to set the specified character in buffered string at the specified position of the string in which you have to set the given character.
charAt()
This is the charAt() function which is used to get the character at the specified position of the given string.
substring()
This is the substring() function which is used to get the sub string from the buffered string from the initial position to end position (these are fixed by you in the program).
deleteCharAt()
This is the deleteCharAt() function which is used to delete the specific character from the buffered string by mentioning that's position in the string.
length()
This is the length() function is used to finding the length of the buffered string.
delete()
This is the delete() function is used to delete multiple character at once from position to position (n and m are will be fixed by you.) in the buffered string.
capacity()
This is the capacity() function is used to know about the current characters kept which is displayed like : number of characters + 6.
public class StringBufferFunctionsDemo {
      public static void main(String[] args) {
                  //          Examples of Creation of Strings
                  StringBuffer strBuf1 = new StringBuffer("Bobby");
                  StringBuffer strBuf2 = new StringBuffer(100); //With capacity 100
                  StringBuffer strBuf3 = new StringBuffer(); //Default Capacity 16
                  System.out.println("strBuf1 : " + strBuf1);
                  System.out.println("strBuf1 capacity : " + strBuf1.capacity());
                  System.out.println("strBuf2 capacity : " + strBuf2.capacity());
                  System.out.println("strBuf3 capacity : " + strBuf3.capacity());
                  System.out.println("strBuf1 length : " + strBuf1.length());
                  System.out.println("strBuf1 charAt 2 : " + strBuf1.charAt(2));
                  //          A StringIndexOutOfBoundsException is thrown if the index is not valid.
                  strBuf1.setCharAt(1, 't');
                  System.out.println("strBuf1 after setCharAt 1 to t is : "+ strBuf1);
                  System.out.println("strBuf1 toString() is : " + strBuf1.toString());
                  strBuf3.append("beginner-java-tutorial");
                  System.out.println("strBuf3 when appended with a String : "+ strBuf3.toString());
                  strBuf3.insert(1, 'c');
                  System.out.println("strBuf3 when c is inserted at 1 : "+ strBuf3.toString());
                  strBuf3.delete(1, 'c');
                  System.out.println("strBuf3 when c is deleted at 1 : "+ strBuf3.toString());
                  strBuf3.reverse();
                  System.out.println("Reversed strBuf3 : " + strBuf3);
                  strBuf2.setLength(5);
                  strBuf2.append("jdbc-tutorial");
                  System.out.println("strBuf2 : " + strBuf2);
                  //          We can clear a StringBuffer using the following line
                  strBuf2.setLength(0);
                  System.out.println("strBuf2 when cleared using setLength(0): "+ strBuf2);
      }
}
STRING v/s STRING BUFFER CLASS                                                                  
                   String
                    StringBuffer
  1.String is a immutable object
1. It is a mutable object.
  2. For each concatenation or updation, the String creates new object.
2. StringBuffer is a mutable sequence of characters. It is like a String but the contents of the StringBuffer can be modified after creation.
 3. If the GC is busy then all those new references are in queue .So wastage of performance.
3. So to rectify this problem we have StringBuffer class is there.
StringBuffer sb = new StringBuffer(“abc”)

4. It doesn’t consist of any append ().
4. In consists of apend ().
If we want to add a new string then
Sb.append(“d”);
Then it takes same memory location and
Takes abcd as a string.           

No comments:

Post a Comment