The moose likes Beginning Java and the fly likes () vs [] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "() vs []" Watch "() vs []" New topic

() vs []

Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
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?
marc weber

Joined: Aug 31, 2004
Posts: 11343

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();

    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    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.

    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    camilo lopes
    Ranch Hand

    Joined: Aug 08, 2007
    Posts: 202
    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).

    Brazil - Sun Certified Java Programmer - SCJP 5 About Java - Update every Week.
    Guide SCJP - tips that you need know
    I agree. Here's the link:
    subject: () vs []
    It's not a secret anymore!