Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

String[] fruits = {"apple", "banana", "cherry"}; How do you tell how many items are in the fruits array?

The answer is fruits.length;

Why is fruits.size(); not the answer? Also, why are no parentheses present in the correct answer?

1 Answer

I've always hated this about Java. For some odd reason, getting the length of an array is stringName.length() while getting the length of an array is arrayName.length. It's an inconsistency that always kills me in Java but, just is. It must be remembered, I guess.

If you check the Array documentation for Java 8, you'll see that size() isn't a method. This is why it can't be used.

Java is an inconsistent language, in my opinion. Others may argue otherwise. It's just simply something that you, as a developer, must learn to live with. Hope this helped!

As for your no parentheses question, parentheses are used to access a method. When we use fruits.length, for example, we're actually accessing a PROPERTY of the array variable fruits and not using a METHOD of the array. I think arrays SHOULD have a method length(), but, I've no say. LOL