1

In my Android project, I have a singleton class called Question which will be used 90% of the time while the application is running. The class contains two static String variables named question and answer, as the new instance is called repeatedly with different strings for question and answer, so a new String object is created every time. I was thinking about changing the String variables to StringBuilder and replace the contents every time.

Here is the code with String variables:

public class Question {

    private static Question mQuestionClass = null;
    private static String mQuestion = "";
    private static String mAnswer = "";

    private Question() {
    }

    public static Question getQuestionInstance(String question, String answer) {
        if (mQuestionClass == null) {
            mQuestionClass = new Question();
        }
        mQuestion = question;
        mAnswer = answer;
        return mQuestionClass;
    }

}

Here is the code with StringBuilder variables:

public class Questiontwo {

    private static Questiontwo mQ2 = null;
    private static StringBuilder mQ = null;
    private static StringBuilder mA = null;

    private Questiontwo() {
    }

    public static Questiontwo newInstance(String q, String a) {
        if (mQ2 == null) {
            mQ2 = new Questiontwo();
        }

        if (mQ == null) {
            mQ = new StringBuilder(q);
        }
        mQ = mQ.replace(0, mQ.length(), q);

        if (mA == null) {
            mA = new StringBuilder(a);
        }
        mA = mA.replace(0, mA.length(), a);
        return mQ2;
    }

}

Which one should I prefer to use as less memory as possible?

1
  • What is the purpose of using StringBuilder when you dont use append here ? I dont see neither += on string in heavy loaded for loop ? Your code does not need StringBuilder Commented Jul 18, 2015 at 19:20

3 Answers 3

3

In both scenarios, your design seems bit flawed. You are using static variables and initializing it in Constructor. This seems counter-intuitive - because if you try to create multiple instances of Question, the static variables will have value set by last instance created.

I would suggest just use a simple class

public class Question {
  private String mQuestion = "";
  private String mAnswer = "";

  public Question(String question, String answer) {
    mQuestion = question;
    mAnswer = answer;
  }
}

Just create instances of Question and use them like regular Java Objects, instead of creating singleton using static variables and in the process introduce unintended bugs. Let Android do the garbage collection of the objects that are no more in use.

Question q = new Question("What is capital of India?", "New Delhi");
Sign up to request clarification or add additional context in comments.

Comments

1

The class StringBuilder is useful when you need to modify the content of sequence of chars.

From javadoc:

The principal operations on a StringBuilder are the append and insert methods

StringBuilder becomes very useful to replace a sequence of concatenation using + or concat() method between strings.

If you need to replace the whole content of a String with another String StringBuilder is not the right choice. Infact I imagine that you will pass a string with the new question to the StringBuilder to change its content. If you do that you already created the new String (with the new question).

So using a StringBuilder in this context add some memory usage and some cpu calculations instead of remove it.

Comments

1

First let's discuss two things:
One: Why assigning a string to another consumes more memory.
Second: How StringBuilder solves this problem.
One:
When you have a string for example String a and you assign a value to it for example a = "Hello" what java will do is to create a new string in the memory and a will point to it . So every time you change the value for example to a = "good" it creates another string in memory and a pointer will change from hello to good and hello still exists in memory however it is in accessible, because no one points to it.
Two:
String builder instead modifies the old string and changes it to the new one so it will consume less memory. It's good for when you want to modify a string.

I think looking at this will also help you.

Comments

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.