1

i wrote this piece of code to compare String and StringBuffer.

public static void main(String[] args) {
    String inputText = "let us learn pig latin";
    usingString(inputText);
    usingStringBuffer(inputText);
}

public static void usingString(String input) {
    String pigLatinText = new String();
    input = input.trim();
    String tokens[] = input.split(" ");
    for (int i = 0; i < tokens.length; i++) {
        String temp = tokens[i];
        temp = temp.substring(1) + temp.charAt(0);
        temp += "ay";
        pigLatinText += temp+" ";
    }
    System.out.println("Using String             :"+pigLatinText);
}

public static void usingStringBuffer(String input) {
    String pigLatin = new String();
    input = input.trim();
    String tokens[] = input.split(" ");
    for (int i = 0; i < tokens.length; i++) {
        StringBuffer temp = new StringBuffer(tokens[i]);
        temp.append(temp.charAt(0)).deleteCharAt(0);
        temp.append("ay");
        pigLatin += temp+" ";
    }
    System.out.println("Using String Buffer  :"+pigLatin);
}

I know String + operator internally uses StringBuilder's append(). And it creates temporary objects as it is immutable. I am trying to prove that by counting the number of objects that are created while using String and by using StringBuffer. Using Static variable to count the number of objects is not going to help me as i cannot modify the String class. i found this link. It seems promising. but i don't know how to modify the Object.java file. using System.currentTimeInMillis() before and after doesn't help as this operation is very small. So my questions are

  1. is there any way to analyze or compare the performance of these classes ?
  2. How to count the number of String objects created in a program?
1
  • 1
    Netbeans has a Profile feature that will allow you to see how many Stringare created. Commented Feb 5, 2015 at 14:11

1 Answer 1

1

First of all concatenation of Strings is done using StringBuilder not StringBuffer.

example :

public static void main(String[] args) {
    String s = "a";
    String s1 = s + "b"; 
}

Byte code for s1=s+"b"

       16: invokevirtual #31   // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;

Here, You create 3 String Objects "a" and "b" are String Literals (which are also String Objects) and they go into the String constants Pool. "ab" will be created during runtime using StringBuilder.

StringBuffer is thread-safe / synchronized so it will be slower than StringBuilder.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick response.I have corrected that mistake. My question is not about string buffer and builder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.