1

Is any benefit--small as it may be--of putting the length of an array into an int in this situation, or is simply calling array.length twice better? Or does compiler optimization make these approaches equivalent?

No int:

Vector<String> vs = new Vector<String>(a_o.length);
for(int i = 0; i < a_o.length; i++)  {
   vs.add(a_o[i]);
}

int:

int iLen = a_o.length;
Vector<String> vs = new Vector<String>(iLen);
for(int i = 0; i < iLen; i++)  {
   vs.add(a_o[i]);
}
aliteralmind
  • 19,847
  • 17
  • 77
  • 108

2 Answers2

5

AFAIK, a decent compiler the compiler will do this optimization for you.
And even if not, the hit is minimal. You are probably going to suffer much more from using Vector. see Why is Java Vector class considered obsolete or deprecated?

Community
  • 1
  • 1
Itay Karo
  • 17,924
  • 4
  • 40
  • 58
1

The compiler is smart, trust it. It'll optimize it.

Don't waste time on performance issues like this one, worry more about other things like data structures..

Maroun
  • 94,125
  • 30
  • 188
  • 241