views:

202

answers:

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.

+2  A: 

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

Peter
For this static constant (denoted by the final keyword), each time that you use the constant, a temporary String instance is created.why??
Vivart
@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.
Joachim Sauer
thanks for the reply after reading that document i was confused that what's going on.
Vivart
A: 

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.

Romain Hippeau
+1  A: 

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.

Juriy
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 Sauer
@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.
polygenelubricants
+5  A: 

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

Hardcoded
@Hardcoded thanks for the example. now i understand that article.
Vivart
A: 

wow!

because BlackBerry® Java® Virtual Machine is retarded?

If a Java VM has huge penalty for using final variables, which is an important language concept, it is retarded.

irreputable