Java Reference
In-Depth Information
This method declaration contains four things:
The method does not return anything as indicated by the keyword
void .
The name of the method is
main .
The method requires no arguments.
The method does not do anything as its body is empty.
The return value of a method is something that the method returns to its caller. The caller of the method may also
want to pass some values to the method. If a method requires its caller to pass some values to it, this fact must be indicated
in method's declaration. The fact that you want to pass some value to a method is specified within the parentheses that
follow the method name. You need to specify two things about the values you want to pass to the method:
The type of the value you want to pass. Suppose you want to pass an integer (say 10) to the
method. You need to indicate this by using a keyword int , which is used to indicate an integer
value like 10.
The identifier, which will hold the value you pass to the method. Identifier is a user-defined
name. It is called a parameter name.
If you want the main method to accept one integer value from its caller, its declaration will change to the
following one:
void main(int num) {
}
Note that num is the identifier, which will hold the value passed to this method. Instead of num , you may choose to
use another identifier, for example, num1 , num2 , num3 , etc. The above declaration of the main method is read as
The method main accepts one parameter of the type int and it does not return any value to
its caller.
If you want to pass two integers to the main method, its declaration will change to the following:
void main(int num1, int num2) {
}
It is clear from the above declaration that you need to separate the parameters passed to a method by a comma
(,). What will you do if you want to pass 50 integers to this method? You will end up with a method declaration like
void main(int num1, int num2, ..., int num50) {
}
I have shown only three parameter declarations. However, when you write a Java program, you will have to type
all 50 parameter declarations. Let's look for some alternate ways to passing 50 parameters to this method. There is
one similarity among all 50 parameters that they are all of the same type—integer. No values will contain fraction
like 20.11 or 45.09. This similarity among all parameters allows you to use a magical creature in the Java language
called an array. What is required to use array to pass 50 integer parameters to this method? When you write
int num
Search WWH ::




Custom Search