Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Organizing Data Review Arrays

Jan Gooßens
Jan Gooßens
22,344 Points

Array Quiz

String[] jacksons = new String[5];

jacksons[0] = "Michael";

jacksons[1] = "Tito";

What is the result of the expression jacksons.length ?

The answers are 2 or 5.

But i think it has to be 6 because of the start from zero ? Right?

1 Answer

andren
andren
28,520 Points

While I can certainly see why you would think so the answer is in fact 5.

When you create a new array the number you put in the brackets is the number of objects you want to create. So 5 objects are created in the array at the start. Since the length property counts the number of objects (not their index) it will return 5 when there are 5 objects.