2

Let's say I create an array of ints with length 10, i.e.

int[] array = new int[10];

At some point in my code, I want to compare the value of an int variable, let's call it var, with the length of the array.

I would like to know if this piece of code:

if(var == array.length) { // stuff }

and this piece of code:

if(var == 10) { // stuff }

which do exactly the same thing, have also the same performance.

In other words, I would like to know the internal mechanics that the JVM (?) uses to find the length of the array (I don't say "to return" since length is a field, not a method). Does it make use of iteration? Because if it does, then the 2nd piece of code would be faster than the 1st one.

EDIT: Similar question regarding array.length cost (even though focusing more to its use in for loops):

What is the Cost of Calling array.length

6
  • 9
    Never use the second one. Always use the first one. Magic numbers are bad. Commented Feb 26, 2014 at 17:15
  • The (JIT) compiler will optimze this out. Commented Feb 26, 2014 at 17:16
  • @SotiriosDelimanolis In general you are right about that, but in my code this is not a problem, really. From a performance point of view? Commented Feb 26, 2014 at 17:16
  • I might be mistaken, but logic dictates that the field is updated when the array langth changes (;)). thus, a lookup for the array length doesn't need any iteration, for the field already contains the number. Commented Feb 26, 2014 at 17:16
  • @CoolBeans I haven't check this because it is about 2-D array (i.e. a table). I will take a look, maybe I will find something there. Commented Feb 26, 2014 at 17:18

4 Answers 4

3

.length is a property, so it would not do iteration for sure. Still, the value of the property is, naturally, fetched at runtime, meaning that the second solution will be a little bit faster (as this is comparison with constant).

Still the first implementation is far more preferable:

  • This makes your code quite more maintainable
  • You can alter the length of the array only at one place
  • You will never feel the performance difference unless you pass through this if litterally millions of times in a second.

EDIT By the way you can yourself tell this is a property - there are no braces after the call. I at least do not know of a way in java to make property access do additional computation, but just retrieving its value.

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

3 Comments

+1 for the good answer, it highlights all the aspects of the concept! Thank you!
@PeterHiggs Sure, thanks. Also please mind my edit I think it will help you get around similar cases in the future.
Yes I will! I am new in programming and it happens from time to time to have questions that are simple to answer and have to do with fundamental concepts, but at least once I learn from my mistakes, I usually don't repeat them! :)
1

.length is a property of the array, not a function. Thus, the result would be available immediately, with no iteration necessary.

4 Comments

That was my initial thought, but I wanted to be sure, because I have some code that runs much slower than expected and I want to find out why! Well, array.length is not the problem apparently! Thank you!
@PeterHiggs any chance the Java chat room would like to help?
@Jan Dvorak Yes, I think I will discuss about my performance issues and how to optimize the performance of my code on the Java chat.
@Jan Dvorak Already uploaded on the chat, you could take a look if you would like: filetea.me/t1sLLgTRgRbTGizbpbc5e1EYQ and filetea.me/t1seyU1k5P7Q5qxx1JMP9iKTg
1

From the Java Doc

The members of an array type are all of the following:

The public final field length, which contains the number of components of the array. length may be positive or zero.

length is an final field of array, so no iterations are required while writing following code.

if(var == array.length) { // stuff }

And it is good coding practice indeed.

Comments

0

The length property of an array is extracted in constant (O(1)) time - there is no iteration needed. It's also good practice to use this.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.