• public void main(String[] args)
• public static void main(String args)
• public static void main()
• void main(String args)
Just digging in to this one line will take us deep into the world of Java, so we are going to start
digging but we are not going to dig too deeply right away. Much of what could be revealed by this
one line is better understood through other examples, so be patient.
The first word, public indicates to the Java compiler that this is a method that anyone can call.
We will see that Java enforces several levels of security on the methods we write, including public,
protected, and private methods.
The next word, static tells Java that this is a method that is part of the class, but is not a method
for any one instance of the class. The kind of methods we typically wrote in Python required an
instance in order for the method to be called. With a static method, the object to the left of the . is a
class, not an instance of the class. For example the way that we would call the main method directly
is: Hello.main(parameter1). For now you can think of static methods the same way you think of
methods in Python modules that don’t require an instance, for example the math module contains
many methods: sin, cos, etc. You probably evaluated these methods using the names math.cos(90)
or math.sin(60).
The next word, void tells the Java compiler that the method main will not return a value. This is
roughly analogous to omitting the return statement in a Python method. In other words the method
will run to completion and exit but will not return a value that you can use in an assignment
statement. As we look at other examples we will see that every Python function must tell the
compiler what kind of an object it will return. This is in keeping with the rule that says everything
in Java must have a type. In this case we use the special type called void which means no type.
Next we have the proper name for the method: main. The rules for names in Java are similar to the
rules in Python. Names can include letters, numbers, and the _. Names in Java must start with a
letter.
Finally we have the parameter list for the method. In this example we have one parameter. The
name of the parameter is args however, because everything in Java must have a type we also have
to tell the compiler that the value of args is an array of strings. For the moment You can just think
of an array as being the same thing as a list in Python. The practical benefit of declaring that the
method main must accept one parameter and the parameter must be a an array of strings is that
if you call main somewhere else in your code and and pass it an array of integers or even a single
string, the compiler will flag it as an error.
That is a lot of new material to digest in only a single line of Java. Lets press on and look at the
next line: System.out.println("Hello World!");. This line should look a bit more familiar to
you. Python and Java both use the dot notation for finding names. In this example we start with
System. System is a class. Within the system class we find the object named out. The out object
is the standard output stream for this program. Having located the out object Java will now call
the method named println(String s) on that object. The println method prints a string and
adds a newline character at the end. Anywhere in Python that you used the print function you
will use the System.out.println method in Java.
Now there is one more character on this line that is significant and that is the ; at the end. In Java
the ; signifies the end of a statement. Unlike Python where statements are almost always only one
line long java statements can spread across many lines. The compiler knows it has reached the end
of a statement when it encounters a ;. This is a very important difference to remember. In Java
the following statements are all legal and equivalent. I would not encourage you to write your code
like this, but you should know that it is legal.
System.out.println("Hello World");
System.out.println("Hello World")
8