mimo31
mimo31

Reputation: 355

Is it wise to declare a String as final if I use it many times?

I have a method repeatedMethod like this:

public static void repeatedMethod() {
    // something else
    anotherMethod("myString");
    // something else
}

public static void anotherMethod(String str) {
    //something that doesn't change the value of str
}

and I call the repeatedMethod many times.

I would like to ask if it is wise to declare myString as static final outside that method like this:

public static final String s = "myString";

public void repeatedMethod() {
    anotherMethod(s);
}

I think that when I do anotherMethod("myString"), a new instance of String is created. And since I do that many times, many instances of String are created.

Therefore, it might be better to create only one instance of String outside the repeatedMethod and use only that one every time.

Upvotes: 4

Views: 499

Answers (4)

Peter Lawrey
Peter Lawrey

Reputation: 533750

Optimize for clarify before worrying about performance. String literals are only created once, ever (unless the literal is unloaded) Performance is not only less important usually but irreverent in this case.

You should define a constant instead repeating the same String to make it clear these strings don't happen to be the same, but must be the same. If someone trying to maintain the code later has to modify one String, does this mean they should all be changed or not.

BTW When you optimize for clarity you are also often optimizing for performance. The JIT looks for common patterns and if you try to out smart the optimizer you are more likely to confuse it resulting in less optimal code.

Upvotes: 0

柯鴻儀
柯鴻儀

Reputation: 623

no, you don't need to do that ,there is a "constant pool" in the JVM ,for every inline string (ex:"myString") ,it will be treated as an constant variable implicitly, and every identical inline string will be put in the constant pool just once.

for example ,for

    String i="test",j="test";

there will be just one instance of constant variable "test" in the constant pool.

also refer to http://www.thejavageek.com/2013/06/19/the-string-constant-pool/

Upvotes: 1

janos
janos

Reputation: 124744

String literals of the same text are identical, there won't be excessive object creation as you fear.

But it's good to put that string literal in a static final variable (a constant) with a descriptive name that documents the purpose of that string. It's generally a recommended practice to extract string literals to constants with good names.

This is especially true when the same string literal appears in more than one place in the code (a "magic string"), in which case it's strongly recommended to extract to a constant.

Upvotes: 4

Tunaki
Tunaki

Reputation: 137229

What you are doing is right but for the wrong reason.

When you do anotherMethod("myString"), no new instance of String is actually going to be created: it might reuse a String from the String constant pool (refer to this question).

However, factoring common String values as constants (i.e. as private static final) is a good practice: if that constant ever needs to change, you only need to change it in one place of the source code (for example, if tomorrow, "myString" needs to become "myString2", you only have one modification to make)

Upvotes: 6

Related Questions