Java Reference
In-Depth Information
it means that
num
is an identifier of the type
int
and it can hold one integer value. If you place two magic brackets (
[])
after int, as in
int[] num
it means that
num
is an array of
int
and it can hold as many integer values as you want. There is a limit to the number
of integers that
num
can hold. However, that limit is very high and I will discuss that limit when I discuss arrays in
detail. The values stored in
num
can be accessed using subscripts:
num[0]
,
num[1]
,
num[2]
, etc. Note that in declaring
an array of the type
int
, you have not mentioned the fact that you want
num
to represent 50 integers. Your modified
declaration for
main
method, which can accept 50 integers, would be as follows:
void main(int[] num) {
}
How will you declare the
main
method, which will let you pass names of 50 persons? Since
int
can only be used
for passing integers, you must look for some other type that represents a text in Java language because the name of a
person will be text, not an integer. There is a type
String
(note the uppercase
S
in
String
) that represents a text in Java
language. Therefore, to pass 50 names to the method
main
, you can change its declaration as follows:
void main(String[] name) {
}
In this declaration, you need not necessarily change the parameter name from
num
to
name
. You changed it just to
make the meaning of the parameter clear and intuitive. Now let's add some Java code in the body of the
main
method,
which will print a message on the console.
System.out.println("The message you want to print");
This is not the appropriate place to discuss what
System
,
out
, and
println
are all about. For now, just type in
System
(note uppercase
S
in
System
), a dot,
out
, a dot,
println
followed by two parentheses that contain the message
you want to print within double quotes. You want to print a message “
Welcome to the Java world
” and your
main
method declaration will be as follows:
void main(String[] name) {
System.out.println("Welcome to the Java world");
}
This is a valid method declaration that will print a message on the console. Your next step is to compile the source
code, which contains the
Welcome
class declaration, and run the compiled code. However, when you run a class, the
JVM looks for a method
main
in that class and the declaration of the method
main
must be as follows, though name
could be any identifier.
public static void main(String[] name) {
}
Apart from two keywords,
public
and
static
, you should be able to understand the above method declaration,
which states: “
main
is a method, which accepts an array of
String
as a parameter and returns nothing.”