[SOLVED] When checking for empty array, is arr.length == 0 preferred over arr.length < 1?


When checking for an empty array, array.length < 1 And array.length == 0 will get you the same result but which is preferred or does it matter at all? I can see the reason array.length == 0 is preferred is that the length of an array will never be less than 0 and it’s more precise to put it that way.


They are identical from the JVM point of view.

From the programmer point of view is more readable array.length == 0.

Generally speaking, try to let your code readable, and only if you have any performance gap to solve try to investigate if it is possible to write the same code in a more efficient way also if it is less human-readable.

Answered By – Davide Lorenzo MARINO

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *