简体   繁体   English

Java静态字符串或静态StringBuilder

[英]Java Static String or Static StringBuilder

In my Android project, I have a singleton class called Question which will be used 90% of the time while the application is running. 在我的Android项目中,我有一个名为Question的单例类,该类在应用程序运行时将有90%的时间被使用。 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. 该类包含两个名为String静态String变量,因为使用不同的String反复调用新实例以用于Question和Answer,因此每次都会创建一个新的String对象。 I was thinking about changing the String variables to StringBuilder and replace the contents every time. 我正在考虑将String变量更改为StringBuilder并每次都替换其内容。

Here is the code with String variables: 这是带有String变量的代码:

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: 这是带有StringBuilder变量的代码:

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? 我更希望使用哪一个内存越少越好?

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. 这似乎违反直觉-因为如果您尝试创建Question多个实例,则静态变量的值将由最后创建的实例设置。

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. 只需创建Question实例并像常规Java对象一样使用它们,而不是使用静态变量创建单例,并且在此过程中会引入意外的错误。 Let Android do the garbage collection of the objects that are no more in use. 让Android对不再使用的对象进行垃圾回收。

Question q = new Question("What is capital of India?", "New Delhi");

The class StringBuilder is useful when you need to modify the content of sequence of chars. 当您需要修改字符序列的内容时, StringBuilder类非常有用。

From javadoc: 从javadoc:

The principal operations on a StringBuilder are the append and insert methods StringBuilder上的主要操作是append和insert方法

StringBuilder becomes very useful to replace a sequence of concatenation using + or concat() method between strings. StringBuilder对于替换字符串之间使用+concat()方法的一系列连接变得非常有用。

If you need to replace the whole content of a String with another String StringBuilder is not the right choice . 如果您需要用另一个String替换String的全部内容, StringBuilder是不正确的选择 Infact I imagine that you will pass a string with the new question to the StringBuilder to change its content. 实际上,我想您将带有新问题的字符串传递给StringBuilder来更改其内容。 If you do that you already created the new String (with the new question). 如果这样做,您已经创建了新的String(带有新问题)。

So using a StringBuilder in this context add some memory usage and some cpu calculations instead of remove it. 因此,在这种情况下使用StringBuilder会增加一些内存使用量和一些cpu计算,而不是删除它们。

First let's discuss two things: 首先让我们讨论两件事:
One: Why assigning a string to another consumes more memory. 一个:为什么将一个字符串分配给另一个会消耗更多的内存。
Second: How StringBuilder solves this problem. 第二:StringBuilder如何解决此问题。
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 . 当您有一个字符串(例如String a并为它分配一个值(例如a = "Hello" ,java要做的就是在内存中创建一个新字符串,a会指向它。 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. 所以每次例如值更改为a = "good"它在内存中创建另一个字符串和a指针将改变hellogoodhello的记忆仍然存在,但它是可访问的,因为没有一个指向它。
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. 我想看看这个也会对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM