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 Arrays Gotchas and Wins Sorting

Ani Azot
Ani Azot
1,754 Points

How do you know when to add parentheses to the end of certain methods?

So on this line:

Arrays.sort(friends, Comparator.comparing(String::length).reversed());

...the class method length after String takes has no opening and closing parentheses however, .reversed() has them.

I understand these parentheses are present to take possible arguments but is there a rule for adding parentheses to methods after instances / classes?

Is this a java documentation / learn as you use each one kind of thing or have I missed something?

Thanks

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Ani Azot! Contrary to the other answer here, they are not both methods. One is a method and the other is a property. We always use the parentheses on a method. The String type contains a method called length() while the Array object contains a property called length

Here is some documentation from Oracle:

String documentation

Array documentation

When in doubt, always consult the documentation to find out if what you're trying to use is a method or a property. This will indicate whether or not you should use the parentheses.

Hope this helps! :sparkles:

edited for additional information

It was pointed out to me that the confusion here is in the double colon syntax. When referencing a method with the double colon, the parentheses will not be used.

Ani Azot
Ani Azot
1,754 Points

Ah thank you that's what I was looking for.

Teacher Russell
Teacher Russell
16,873 Points

Hi Jennifer. I understand that a method uses parenthesis, but instances like this, where you see a method of the String class referred to without them, made it hard to figure that out. In this instance, is this just the way it is with Comparator, or are there other exceptions?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Teacher Russell I think some of the confusion here might be with the double colon operator :: which is a form of "syntactic sugar" and wasn't even available until Java 8 as I understand it. This double colon is a reference to a method. When using the double colon, you will not need parentheses and this is part of that syntax. I might suggest taking a look at this article. But length on a String is a method, while length on an Array is a property. Thus, you would only find the double colon syntax with the String.length().

.length and .length() are 2 different methods, basically one if for a string's length, one for an array's length. https://www.geeksforgeeks.org/length-vs-length-java/