5

below content is taken from Best practice: Writing efficient code but i didn't understand why

private static String x = "example";

faster than

private static final String x ="example";

Can anybody explain this.

Using static variables for Strings

When you define static fields (also called class fields) of type String, you can increase application speed by using static variables (not final) instead of constants (final). The opposite is true for primitive data types, such as int.

For example, you might create a String object as follows:

private static final String x = "example";

For this static constant (denoted by the final keyword), each time that you use the constant, a temporary String instance is created. The compiler eliminates "x" and replaces it with the string "example" in the bytecode, so that the BlackBerry® Java® Virtual Machine performs a hash table lookup each time that you reference "x".

In contrast, for a static variable (no final keyword), the String is created once. The BlackBerry JVM performs the hash table lookup only when it initializes "x", so access is faster.

private static String x = "example";

You can use public constants (that is, final fields), but you must mark variables as private.

4 Answers 4

9

I wasn't aware of this, but it makes sense to me:

The JVM has an internal String Literal Cache. Everytime you create a String by using a literal, the JVM has to look for it in the cache and if it isn't there, store it.

Now a compiler can inline a final variable with a String literal, because it is known at compile time and it seems to be a good idea for performance.

So your code:

static final String CONST = "myconst";
...
if (CONST.equals(aVar))
...
case CONST
...

is rewritten by the compiler to:

static final String CONST = "myconst";
...
if ("myconst".equals(aVar))
...
case "myconst"
...

If the JVM implementation isn't clever enough, it needs to look up "myconst" three times in this example.

When you don't mark CONST as "final", the compiler can't "optimize" it since the variable can change at runtime. Your code would be compiled 1:1 and the JVM only needs to look for the Object at the variable.

btw: Bad JVM implementations shouldn't define your coding style. "final" gives a lot of safety, so as long as it doesn't really hit your performance: Don't care about if it increase or decrease you speed - its different for the next JVM anyhow

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

2 Comments

perhaps you should clarify that the 'JVM' in question here is the Blackberry 'JVM' implementation. To the best of my knowledge, the Oracle JVM (if they follow the OpenJDK in this respect, that is) internalizes static final Strings and put them in PermGen as long as there are references to them. This makes lookup O(1) instead of the O(n) which is the result of the above (in comparisons)
@Steen The Oracle JVM has a String Literal Cache, too. What you are describing, is the internal implementation of this cache. Every JVM needs one, because String.intern() and other stuff wouldn't work without it.
2

the text explains it, just read it.

but to reword it: Its faster because it is. The way blackberry jvm is made its better to use the non final version. Its like that because its designed in that fashion

3 Comments

For this static constant (denoted by the final keyword), each time that you use the constant, a temporary String instance is created.why??
@Vivart: because the JVM used on the BlackBerry OS was designed that way. No other reason! It's an implementation detail and any JVM implementation may choose to use this way or another, as long as it conforms to the specification. Nothing in the specification says that final fields must be faster than non-final fields.
thanks for the reply after reading that document i was confused that what's going on.
1

This is the specifics of the Blackberry VM. Other VM might do it differently.

Side Note: do not pay too much attention to the optimization until you actually run into the performance issues (this is called "premature optimization") 'cause if you do it is highly possible that performance is leaked in the place where you would never expect it to.

2 Comments

Regarding "other VM might do it differently": I'd even say that most other VMs almost definitely do it differently. This particular advice applies specifically to the BlackBerry VM and should not be taken to be true in general.
@Joachim: that's good to know. I was flipping out when I first read the question. At least this travesty is limited to BlackBerry VM.
0

There is a runtime String Literal Pool. When you create the Strings as Final the JVM creates a copy of the String each time you use it. When the String is non-final it is inserted into the String Literal Pool the first time you create it and then after that it is just looked up from the pool, this avoids copies of the Strings.

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.