• Post Reply Bookmark Topic Watch Topic
  • New Topic

Very basic question. What exactly do parentheses do in Java code?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Java friends!

Fresh to the Java world and this seemed like the best place to post such a basic type of question. I think Stackexchange would have their pitchforks ready if I posted this. But...what is exactly is the purpose of paratheses? For example, I keep seeing things like .next() or .toUpperCase I know these are methods. Which I am still wrapping my head around. But what exactly are the () there for? I've seen it set up like class.method(x,y,z); but not exactly sure what's going on inside of the parentheses. Or why they are there and why they are connected to the method.

Please help clear up this confusion

Thank you
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37508
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin,
Yes. The parens are used to enclose the list of parameters or be empty if there are none.

And when you write a method, you do the same thing. Use parents to list the parameter types you expect
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you go more in depth. I still don't fully understand. An example would fully solidify it.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a parenthesis is like a place holder to contain some data. Its similar to braces.


similarly, for methods/constructors you have parenthesis or rounded brackets to symbolize where the parameters start .. and till where...


 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is even possible to span multiple lines with parenthesis as such:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:But...what is exactly is the purpose of paratheses? For example, I keep seeing things like .next() or .toUpperCase I know these are methods. Which I am still wrapping my head around. But what exactly are the () there for?

To tell the compiler that it IS a method; otherwise it would have no way of distinguishing the name from a variable.

However, parentheses can also be used to force evaluation of an expression in a specific order, kind of the way you might write them yourself. For example:
  1 + 2 * 3;
is 7, because '*' has higher precedence than '+', but:
  (1 + 2) * 3;
is 9.

HIH

Winston
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few more places that I think I missed out on parenthesis :
Conditional statements:


 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, a type name enclosed in parentheses is known as a 'cast operator'. Example:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!