Win a copy of Spring in Action (5th edition) this week in the Spring 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

() 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)
    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).
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!