A Simple Java Program
In this tutorial I explain
- How to write an elementary Java program
- How to save
- How to compile & run
- And also a few points
Here's a simple Java source code -
/* this is a simple Java program */
class Example {
// your program begins with a call to main()
public static void main(String args[])
{
System.out.println("This is a simple Java program. Hello Java!");
}
}
Entering the source -
Here's what we do to save the above piece of code as a valid Java file.
- Open up notepad & write the above piece of code in it .
- Make sure you don't miss out on any of the characters .
Saving the source -
Now that you've written the code in notepad , this is how you'll save it
- Select File/Save As from the notepad menu .
- In the File name field type "Example.java" including the double quotes
- In the Save as type field select All Files ( *.* ) .
- Click enter & the file is saved .
Compiling & running the source -
To compile the source code open up your DOS prompt & move over to the folder that contains the saved java file .
Now execute the compiler , javac , specifying the name of the source file on the command line as shown below .
C:\>javac Example.java
The javac compiler creates a file called Example.class that contains the bytecode version of the program . The output of javac is not a code that can be directly executed but is an intermediate representation of the program that contains instructions that the Java interpreter will execute .
To run the program , use the Java interpreter , called java & pass the class name Example as the command line argument as shown here .
C:\>java Example
The output of the program is
This is a simple Java program
When Java source code is compiled , each individual class is put into it's own output file named after the class & using the .class extension . This is why it is a good idea to give the Java source files the same name as the class they contain - the name of the source file will match the name of the .class file .
Why save as Example.java ?
For most compute languages , the name of the file that holds the source code to a program is arbitrary .This is not the case in Java . For the above example , the name of the source file should be Example.java .
In Java ,a source file is officially called a compilation unit . It is a text file that contains one or more class definitions . The Java compiler requires that a source file use the .java filename extension . As the file extension is four characters long DOS & Windows 3.1 are not capable of supporting Java .
In Java , all code must reside inside a class . By convention , the name of that class should match the name of the file that holds theprogram & that is why we named the example as Example.java .This convention makes it easier to maintain & organize programs .
Another important point to note here is that Java is case-sensitive .
A closer look at Example.java !
The program begins with the following lines :
/*
This is a simple Java program .
Call this file "Example.java" .
*/
This is a comment & is ignored by the compiler . java supports three types of comments & the one used here is a multiline comment .
The next line of code in the program is :
class Example {
This line uses the keyword class to declare that a new class is being defined . Example is an identifier that is the name of the class . The entire class definition , including all of it's members , will be between the opening curly brace ( { ) & the closing curly brace ( } ) & the use of the curly braces in Java is identical to the way they're used in C/C++ .
An another type of comment is used next :
//your program begins with a call to main
This type of comment is called a single line comment .
The next line of code is as follows :
public static void main ( String args [ ] ) {
This line begins the main method . This is the line @ which the program will begin executing . All java applications begin execution by calling main ( ) .
The public keyword is an access specifier , which allows the programmer to control the visibility of class members . When a class member is preceded by or declared as public ,then that member may be accessed by code outside the class in which it is declared . main ( ) is declared public because it must be called by code outside of it's class when the program is started .
The keyword static allows main ( ) to be called without having to instantiate a particular instance of the class . This is necessary since main ( ) is called by the Java interpreter before any objects are made
The keyword void simply tells the compiler that main ( ) does not return any value .
Note -
The Java compiler will compile classes that do not contain a main ( ) method , but the Java interpreter has no way to run these classes .
The next line of code is :
System.out.println ( " This is a simple Java program " );
This line outputs the string " This is a simple Java Program "
System is a predefined class that provides access to the system , & out is the output stream connected to the console .
Statements end in a semi-colon ( ; ) .The reason why lines other than println ( ) in the example don't end with a " ; " is 'cause they are technically not statements .