What is the purpose of using a local variable to store global?

I looked at the source code of the String.hashcode () method. It was an implementation in 6-b14 that has already been modified.

 public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; } 

My question is about this line:

int len = count;

Where count is a global variable representing the number of characters in a string.

Why is the local variable len used here for the loop condition, and not the global variable itself? Because the manipulation of the variable is absent, read only. Is it a good practice to use a local variable if a global field is used to read or write? If so, why for reading too?

+7
java string global-variables local-variables
source share
3 answers

Shaking in the String class, I found a comment regarding this strange assignment of a local variable in the String.trim () reading method "avoid getfield opcode" .

 public String trim() { int len = value.length; int st = 0; char[] val = value; /* avoid getfield opcode */ while ((st < len) && (val[st] <= ' ')) { st++; } while ((st < len) && (val[len - 1] <= ' ')) { len--; } return ((st > 0) || (len < value.length)) ? substring(st, len) : this; } 

So, it's all about performance, as Frank Olszewski noted.

In Java bytecode, the instance variable actually refers to the object and name (using the GETFIELD ). Without optimization, the VM needs to do more work to access the variable.

A potential impact on code performance is that it uses the GETFIELD instruction for each passage through the loop relatively GETFIELD . Local assignment in a method eliminates the need for GETFIELD every time through the loop.

The JIT optimizer may optimize the loop, but it may also not be, so the developers probably took the safe path, applying it manually.

There is a separate question about Avoid getfield opcode , which has details.

+2
source share

Accessing a local variable ist faster than accessing an instance variable. The new Java 8 code is also taken into account (see Anubians answer). It is for this reason that they use the local variable h for hash calculations and do not directly access the instance variable this.hash and create a local pointer char val[] = value; . But with that in mind, I don't know why they are not using i < val.length; or even better z = val.length; i < z; z = val.length; i < z; in a for loop, but i < value.length; .

+2
source share

If count can be changed, then you need a local variable. If you have multithreading, you need a local variable. It is safer to create a local variable. However, it is not strictly secret.

In this case, it overflows, since the lines are in any case immutable. The count value cannot even change.

This is pretty useless, so in Java 8 it looks like this:

 public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; } 

They no longer have counters, they use value.length , where value is the final char array.

They do char val[] = value , but this is just a reference and is strictly disordered.

It may be some subtle microlevel using a local variable, or perhaps it was done for readability, but it is not optional (and, in my opinion, less readable).

+1
source share

All Articles