Recap
A class, main method and a statement. Let us summarize these three elements:
A Class: Class declaration in our example has three components:
Optional modifiers public, the meaning of which you will learn later.
Give any name of the noun form with first letter capitalized and follow java literal naming conventions.
The class body is surrounded by left and right curly braces, {}.
At the minimum a class must have a name and a set of curly braces for the JRE to compile. However to run it from the command line it has to have a main method.
A main method: Main method should have
public and static keywords in any order
Method return type should be void
Method name should be 'main'
Should receive argument of type a String array represented by String[]. You can name the parameter what ever you want although typically 'args' is used.
Mandatory method body, containing optional statements enclosed between left and right curly braces which forms the main method code block.
A Statement: A Statement is a single line of executable. They end with a semicolon ‘;’ System.out.println statement will enable us to write something to the console. That is why we see ‘Hello World’ printed on the console.
Key Terms
What is a Java Program (a.k.a. application)
A typical Desktop Java program is a collection of one or more classes with typically one of them containing the main method, which is the program execution starting point. Java source file can contain more than one class definition however the file name should match the public class name. Each class definition is compiled into separate .class file containing the Java byte code. The name of the .class file matches the class name defined in the .java file. All .java files must be first compiled to .class files and then you run the .class file.
What is Java Runtime Environment (JRE)
What is Java Development Kit (JDK)
What is JVM?
Points to note
- Every operating system has its own version of JDK and JRE. Popular operating systems (OS) on which you can install JDK and JRE are Windows, Linux, MacOS
- Once a Java program is compiled on one OS, it can run on any other OS which has JRE installed. This is why Java had a debut with the slogan "Write once run anywhere!"