Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 845
cobolstinks Avatar asked Dec 01 '22 01:12

cobolstinks


1 Answers

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.

like image 87
Jon Skeet Avatar answered Dec 03 '22 14:12

Jon Skeet