• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

() vs []

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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).
     
    Yeah, but is it art? What do you think tiny ad?
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic