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 trialAni Azot
1,754 PointsHow 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
Treehouse TeacherHi 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:
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!
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.
Mustafa Khan
6,224 Points.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/
Ani Azot
1,754 PointsAni Azot
1,754 PointsAh thank you that's what I was looking for.
Teacher Russell
16,873 PointsTeacher Russell
16,873 PointsHi 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
Treehouse TeacherJennifer Nordell
Treehouse TeacherTeacher 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. Butlength
on a String is a method, whilelength
on an Array is a property. Thus, you would only find the double colon syntax with theString.length()
.