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

Java Java Objects Delivering the MVP Review Arrays

2 Answers

andren
andren
28,558 Points

No, fruits.length is correct.

On arrays the length is a property, in other words a public field, not a method.

If you don't believe that you can easy verify it yourself by actually trying to run this code in Java:

public class Main {
   public static void main(String[] args) {
      String[] fruits = {"apple", "banana", "cherry"};
      System.out.println(fruits.length);
   }
}

Here is a link to an online Java REPL where I have setup the code for you, just run the code on that site and you'll see that fruit.length is indeed correct. If you change it to fruit.length() that will cause the program to crash.

Fahad Mutair
Fahad Mutair
10,359 Points

I agree with andren, fruits.length is correct

Here's how can you use it

length used for Array length

length() used for String length

you can read more in this link