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 trialMinseok Kim
2,708 PointsWhy is there () in string.upper, etc.?
Why don't string.upper work, and why do you need () at the end?
1 Answer
Christian Blume
10,424 PointsHello there,
upper()
is a method of the class String. Methods are different from instance or class variables, since the former perform actions on your object (like calculating values, changing the state of your object etc.) and the latter hold information.
For example, in some languages String objects hold an instance variable called length
describing the number of characters this string contains. You would get that value by calling length
on your String object (note: there are no parenthesis (()
) since length is an instance variable).
Then there are methods. These are always invoked by using the parenthesis. Some methods require you to provide arguments with which this method is supposed to do something, others don't. In cases where you need to provide arguments, these arguments are put in between the parenthesis, like in println("Hello, world!")
. In this case, you pass the string "Hello, world!"
as an argument to the method println()
in order for the string to be printed to the console. The method upper()
does not receive any arguments so nothing has to be put in between the parenthesis, but since you're are still calling a method the parenthesis have to be there.
I hope this clears things up for you.