Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

() vs []  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that [] is often used to form an array, but what else is it used for? I am in general confused about () vs [] vs {}. Why do a lot of commands end with () with nothing in the parenthesis?
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some special situations (like regular expressions), but in general...
  • Brackets [] are used for array indexes.
  • Parentheses () are used to enclose method arguments, and to group terms in expressions for order of evaluation or clarity.
  • Braces {} are used to enclose "blocks" of statements -- for example, within the body of a method definition.
  • All methods must include an argument list -- even if that list is empty. Therefore, all methods must include parentheses -- even if there's nothing in them. So when calling a method that takes no arguments, the form is myMethod();
     
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We put () on constructors, too. Constructors look an awful lot like methods without a return type, but technically they aren't "methods"
    and we don't really "call" them.

    Something like

    String[] myArray = new String[4];

    seems to break the rules because it uses "new" but doesn't use a constructor with (). There are only a few of these little exceptions to the normal syntax; we just have to learn them and go on.
     
    Ranch Hand
    Posts: 202
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi,
    you go to use: () - for methods, constructors [ ] - only for criacao or declaracao of Array {} - for block of c�digos.(class, static methods, blocks, blocks of the instance).
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!