• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

() vs []

 
Jake Miller
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?
 
marc weber
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();
     
    Stan James
    (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.
     
    camilo lopes
    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