Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Friday 5 April 2013

Important Methods in the StringBuffer and StringBuilder Classes



public synchronized StringBuffer append(String s)
StringBuffer sb = new StringBuffer("set ");
sb.append("point");
System.out.println(sb); // output is "set point"

public StringBuilder delete(int start, int end)
StringBuilder sb = new StringBuilder("0123456789");
System.out.println(sb.delete(4,6)); // output is "01236789
public StringBuilder insert(int offset, String s)
StringBuilder sb = new StringBuilder("01234567");
sb.insert(4, "---");
System.out.println( sb ); // output is "0123---4567"

public synchronized StringBuffer reverse()
StringBuffer s = new StringBuffer("A man a plan a canal Panama");
sb.reverse();
System.out.println(sb); // output: "amanaP lanac a nalp a nam A"

public String toString()
StringBuffer sb = new StringBuffer("test string");
System.out.println( sb.toString() ); // output is "test string"

Difference between String and StringBuffer/StringBuilder in Java




Most of us are familiar with the String immutability in java. If someone doesn’t know about the String immutability please refer the link below:
Due to this immutability strings are not recommended for the projects where we need to update the string data frequently because it will consume a lot of memory as it creates the objects every time a new string value is created in string pool memory so java has given StringBuilder and StringBuffer to overcome this immutability constraint. They are pretty much similar to the String except that they are mutable. They have their own methods for data manipulation. To understand this more clearly please refer the below example:
Example Code:
public class demoString {

    public static void main(String s[])
    {
        String st = new String ("JAVA");
        st.concat(" is great");
        System.out.println("The result of String : "+st);//this will only print JAVA

        StringBuffer sbf = new StringBuffer ("JAVA");
        sbf.append(" is great");
        System.out.println("The result of StringBuffer : "+sbf);//this will print JAVA is great

        StringBuilder sb = new StringBuilder ("JAVA");
        sb.append(" is great");
        System.out.println("The result of StringBuilder : "+sb);//this will print JAVA is great
    }}

Output:
The result of String : JAVA
The result of StringBuffer : JAVA is great
The result of StringBuilder : JAVA is great
As in above example we can see that string does not concated the value “is great” to “JAVA “because of its immutability property but StringBuilder and StringBuffer do that because they both are mutable.

The StringBuilder and StringBuffer in java are almost same but the main difference is that StringBuffer is synchronized (which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized (which implies it isn’t thread safe).
Note: StringBuilder was introduced in Java 1.5 (so if you happen to use versions 1.4 or below you’ll have to use StringBuffer)

Difference between length() and length in java



This is a tricky question asked in java interviews.
 The main purpose of both are same as they are used for calculating the length but the difference can be understand by the definition of both mentioned below:  
The length() method is used for calculating the length of the String variable in java.
The length property is used to calculate the length of the Array  in java.
Example:-
public class ArrayTest
{
  public static void main(String[] args)
                     {
    int[] test = {12,1,4};
    String test2 = "Hoo";
    System.out.println(test.length);
    System.out.println(test2.length());
                     }
}

Some Common Methods of String Class in Java



The following methods are some of the more commonly used methods in the String
Class.

  1. charAt() Returns the character located at the specified index
  2.  concat() Appends one String to the end of another ( "+" also works)
  3.  equalsIgnoreCase() Determines the equality of two Strings, ignoring case
  4.  length() Returns the number of characters in a String
  5.  replace() Replaces occurrences of a character with a new character
  6.  substring() Returns a part of a String
  7.  toLowerCase() Returns a String with uppercase characters converted
  8.  toString() Returns the value of a String
  9.  toUpperCase() Returns a String with lowercase characters converted
  10.  trim() Removes whitespace from the ends of a String