Java Reference

Java Reference Home | Smart Soft Home

Parenthesis ()

Description

Parenthesis are used for argument passing. They are used in three distinct areas:
  1. Passing arguments to a method.
  2. Passing arguments to a Java keyword (Ex: if,while,catch, etc.)
  3. Defining a method that accepts arguments.
Arguments serve as input to the method or keyword. Arguments are also called parameters. In HTML, they are called attributes.

Example: Passing arguments to a method

System.out.println("Hello World"); //println is a mthod that takes one argument of type String
myFrame.setSize(300,300); //setSize is a method that takes to arguments of type int

Example: Passing arguments to Java Keywords

//The if keyword takes one argument of type boolean
if(x==10) System.out.println("You are a 10"); 

//the catch keyword takes one argument of type Exception
try{
  Integer.parseInt(x);
}
catch(NumberFormatException ex){
  System.out.println("x must be an integer");
}

Example: Defining a method that takes arguments

public void jump(int howHigh){
  this.y+=howHigh;
  this.redraw();
}

Zero Argument Methods

You still need to provide parenthesis for methods that accept no arguments. In fact, () is how you inicate that the method takes no arguments.

Example: Zero Argument Method Invokation

r1.print();
r1.calc();