Every programmer must first understand the rules of the programming language in which they are working. The first rule in Java is that every program runs from a method called "main". When the Java Virtual Machine (JVM) is asked to execute a Java program it searches for a main method. If there is no main method, then the program will not run. Additionally, it is good practice to provide comments throughout your program. Notice the use of comments in the example below. The text that follows the double-slash will be ignored by the compiler and is known as a single line comment. These comments are for the program author and future programmers responsible for maintaining and updating the code.
// Write a method description here, include preconditions and purpose.
public static void main(String[ ] args)
{
// Write Code Here, this is the body of the method.
}
Your main method must be public:
This is called a visibility modifier and makes main accessible from outside your class.
Your main method must be static:
This is the accessibility modifier and means the method does not need its own class object to access it.
Your main method must be void:
This is the data return type, void means that main will not be returning data.
Your main method identifier must be main:
This is the method/identifier, it is demanded by the JVM that the method be titled "main".
Your main method has a parameter:
The parameter is of data type String array called "args", technically you could change "args" to any name and the main method will work just fine. However, it is standard industry practice to use args as the formal parameter name to the main method.
After the parameter there are opening and closing braces { and }. In between these braces is what we call the body of the method. This is where you will write your code. In the sample code depicted below you will see that there are two declared variables with a system out command containing literal text, a concatenation operator, and a math operation. We will discuss these topics in the next section titled "Operations".