5

What is the difference (memory-wise) between

private static final String FAILURE_MESSAGE=    "...";

protected String getFailedMsg() {
    return FAILURE_MESSAGE;
}

And

protected String getFailedMsg() {
    return "...";
}

Assuming that the FAILURE_MESSAGE is only referenced from the above function.

I mean where and how are the above objects/strings being held in memory in the above cases? Is it JVM specific?

Edit: I know that the string is interned in the first approach, but where is it value being stored/held/(interned?) in the second approach before the function is called?

Second edit as an afterthought - what if the strings are replaced with ints or some other class that is not a string?

3
  • You mean to return {START_MESSAGE}? You have returned {FAILURE_MESSAGE}. Commented Mar 15, 2012 at 9:05
  • @sans481 - thanks, stupid copy paste mistake... Commented Mar 15, 2012 at 9:06
  • It is worth noting that all of you String literals "..." will reference the same instance in memory. Commented Aug 30, 2017 at 1:49

5 Answers 5

4

The first example doesn't compile and the second example does.

Performance is usually less important than simplicity and clarity, and you have a good example here of that. If it did compile, the first example would be as fast as the second.

BTW: It doesn't matter how many times and in how many classes a string literal is used, they will all be String.intern() so they will all be the same object.

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

4 Comments

Sorry about the mix up, it was a copy paste mistake, but the question still holds.
So you say that the string literal in the second approach is interned and occupies memory in the pool even if the function is never called? When is it interned then? First call of the function or during the class loading or during something else?
Oh, and what happens if the strings are replaced with ints in my examples?
a) It's stored in the class's constant pool, so it's interned when the class is loaded. b) The exact same thing, although the constant might just get inlined.
1

The bytecode generated are the same in both cases:

protected java.lang.String getFailedMsg()
    0 ldc 2 (java.lang.String) "..."
    2 areturn

so it's purely sugar.

Comments

1

All String literals will be interned, so it doesn't make any difference (memory-wise, looking at the String instance).

The only difference is that in your first case, the class holds one reference pointer to the String instance. The second approach creates the reference on the stack only.

3 Comments

But where the string value stored before it's created on the stack in the second approach?
@Vic: The value is never created on the stack (only the reference is). All Java objects are created on the heap (well, some modern JVMs create short-lived objects on the heap, but that's not relevant here).
The second part of your comment "the only difference".. is not right. In both cases the bytecode created for the method is "LDC <string>" then "areturn".
1

String literals refer to the same String object, so there is no difference memory-wise in this case.

Section 3.10.5 String Literals of the Java Language Specification 3.0 states:

A string literal always refers to the same instance (§4.3.1) of class String... [they] are “interned” so as to share unique instances, using the method String.intern.

Comments

0

In both the cases, it creates literal in string pool. After creating String s = "...", if u try to return "..." in any method, both point to one string literal created in string pool.

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.