Writing a Simple Java Program

For getting started with Java we will have to learn some basic understanding of the concepts of programming. A lot of coding examples are used throughout the tutorial to make you understand the language better. All the programs and code snippets in this website are compiled and run using the JDK 1.8. As expected, before writing a program and running it, we need to understand the Java Environment first.  This description is given below.

 

Java Environment

Java environment is composed of a number of system components. You use these components at compile time to create the Java program and at run time to execute the program. Java achieves its independence by creating programs designed to run on the Java Virtual Machine (JVM) rather than any specific computer system —

  • After you write a Java program, you use a compiler that reads the statements in the program and translates them into a machine independent format called bytecode.
  • Byte code files, which are very compact, are easily transported through a distributed system like the Internet.
  • The compiled Java code (resulting byte code) will be executed at run time.

Java programs can be written and executed in two ways —

  • Stand-alone application (A general-purpose utility program)
  • Applet which runs on a web browser (Example: Google Chrome or Mozilla Firefox)

 

Java Source Code

A Java program is a collection of one or more Java classes. A Java source file can contain more than one class definition and has a .java extension. Each class definition in a source file is compiled into a separate class file. The name of this compiled file is comprised of the name of the class with .class as an extension. Before we proceed further in this section, I would recommend you to go through the the next five topics as they cover the Basic Language Elementsof Java programming.

Below is a Java sample code (named HelloWorld.java) for the traditional Hello World program. Basically, the idea behind this Hello World program is to learn how to create a program, compile and run it. To create your Java source code you can use any editor (Notepad++ is my favorite) or you can use an IDEs like Eclipse or NetBeans.

HelloWorld.java

public class HelloWorl {        

   public static void main(String[] args)   {     
          System.out.println(”Hello World”);       
   } //End of main()
} //End of HelloWorld Class

Output:

Hello World

 

 

Java Program Structure and the main() method:

I have created a class named “HelloWorld” containing a simple main() method within it. The keyword class specifies that we are defining a class. The name of a public class is spelled exactly as the name of the file (Case Sensitive). All Java programs begin execution with the method named main(). The main() method that gets executed by the JVM has the following signature:

public static void main(String args[])

Declaring this method as public means that it is accessible from outside the class so that the JVM can find it when it looks for the program to start executing it. The keyword ‘static‘ denotes that the main() method is a direct member of the class to which it belongs (here the class name is HelloWorld). It is necessary that the method is declared with return type ‘void‘ (i.e. no value is returned from the method). The main() method contains a String argument array that can contain the command line arguments. The brackets ‘{ and ‘}’ mark the beginning and ending of the class.

The program contains a line “System.out.println(”Hello World”);” that tells the computer to print out on one line of text namely “Hello World“. The semi-colon ‘;’ ends the line of code. The double slashes ‘//’ are used for comments that can be used to describe what a source code is doing. Everything to the right of the slashes on the same line does not get compiled, as they are simply the comments in a program.

 
Java main() method declarations :—
class MainExample1 {public static void main(String[] args) {}}
class MainExample2 {public static void main(String []args) {}}
class MainExample3 {public static void main(String args[]) {}}

All the three valid main methods shown above accept a single String array argument.

 

 

Compiling and Running an Application

To compile and run the program you need the JDK distributed by Oracle Corporation. The JDK contains documentation, examples, installation instructions, class libraries and packages, and tools. Download an editor like Notepad++ us to type your code. You must save your source code with a .java extension. The name of the file must be the name of the public class contained in the file.

 
Steps for saving, compiling and running a Java program:-

Step 1: Save the program with .java Extension.

Step 2: Compile the file from command prompt by typing: javac  <filename>.

Step 3: Successful compilation, results in creation of .class containing byte code

Step 4: Execute the file by typing: java  <filename without extension>

Figure 1:  Steps to run a Java Program

 

The figure above depicts these steps mentioned before.