|
Java Reference Home | Smart Soft Home |
|
|
Parenthesis ()
DescriptionParenthesis are used for argument passing. They are used in three distinct areas:
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 MethodsYou 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 Invokationr1.print(); r1.calc(); |
|