cobolstinks cobolstinks - 6 months ago 48
Java Question

java java.lang.reflect.Array.getLength vs array.length

I've got to maintain some code written by someone else who is no longer with the company. I'm seeing several references to

java.lang.reflect.Array.getLength(anArray)
. Its working, but I've never seen reflection being used to get an array length. Does anyone know the difference between:

java.lang.reflect.Array.getLength(anArray)

and
anArray.length


Is it just syntactic sugar?

Thanks!

Answer

If anArray is statically typed to an array type, you should use anArray.length (not a method call, btw). You'd use the reflection form if you only had a reference to the array as Object.