In this tutorial, you will understand the basic structure of the java program by writing your first java program and will walk through the different ways to compile and run your java program.
Let&rsquo's begin with the Hello World program which has been traditionally illustrated as the basic program. If you run this program it just displays a message Hello, World!!!
on your screen. The main objective of this program is to give learners an idea of the fundamental structure of the java language.
The 1st line of the above java program is called a comment. Comments are, basically, texts either begins after two forward-slash (//) or are enclosed inside '/*' and ' */' to make the code more readable and meaningful. Here in our case from the very beginning line itself we understand, this is ‘Your first program- Hello World Program’.
In Java we can write comments in two ways:
The comments section is not for the front-end user but the programmers. It helps them to keep track of every section of the program without any interference with the programming structure. Since comments are not a programming statement they can appear anywhere in the program but are optional.
A java program is basically a collection of java instructions that executes in some order. Being an object-oriented programming language, everything in java is connected with classes and objects. Hence every instruction in java should reside inside a class and everything inside the class must be written inside a starting brace '{' and closing brace '}'. Unlike in other programming languages, the left brace in java starts in the same line where we define the class. So we can think of class in java as a container with a set of variables and its associated methods.
A simple class definition is as follows:
public class HelloWorld {
}
Here, Helloworld is the name of the class. ‘class’ is the java keyword used for declaring a new java class, and ‘public’ is another java keyword used to denote the access of class, whether the class is publically accessible or privately accessible.
While saving the java code, you should bear in mind that,” the name of the java file should be the same as the name of the class”. That means here the filename of the java code should be HelloWorld followed by the extension .java. So HelloWorld.java is the file name.
Another inevitable part of a java program is the methods. Methods are a set of instructions used to manipulate variables. Methods are mainly located inside the class. A class can contain an ‘n’ number of methods but no less than one. In other words, a class must contain at least one method to signal the Java Virtual Machine where to start the execution of the program. This single method that triggers the execution of the java program is commonly referred to as the main() method in java. Everything inside the main() method must be written inside a starting brace '{' and closing brace '}'
The main() method declaration takes the form as given below:
public static void main(String[] args) {
Statement/s;
}
Here,
public: The keyword public denotes the access specifier of the method. Here the method is declared as public which means it is accessible from anywhere in the program.
static: The java keyword static is used for memory management. It tells that the method main() is static to the class rather than to the instance of the class.
void: The java keyword void specifies that the main method does not return any type.
main(): This is the name of the only method which initiates the execution of the java program.
String[] args: it is simply an array of strings that stores user data passed by the command line when starting a program. To be specific ‘args’ stores all the command line arguments in the form of an array of strings.
Statements in java are purely java instructions to the compiler when a program starts to execute. A print statement in java has the following signature:
Here,
java.lang
which contains 3 main streams namely System.in
, System.out
and System.err
. In java most often we use System.out to display the output from the application.PrintStream
.Finally, the output of the above program is just the text inside the println() method. That is nothing other than the text “ Hello, World!!!”.
Note: A statement must always end with a semicolon
After writing a java program, the next necessary step is to compile them. Compilation in Java is quite different from the other languages. Here the programmer's readable code is converted into byte codes which are easily interpretable by any computer that has JVM. To know more about Java Virtual Machine refer to our previous tutorials. Execution is the process of running the program to get our desired output.
Here we will see how Compilation and Execution can be performed using Notepad and Command Prompt in the Windows environment step by step.
Step 1: Open notepad and write your first java program as given above.
Step 2: Save the program with the “.java” extension like “HelloWorld.java” in any workspace as you wish. Here I have saved it on desktop inside the folder JPRO which is highlighted in yellow color in the below picture.
Step 3: Now open your command prompt and change the directory to where the program is saved.
Step 4. Now provide the command for compilation as “Javac HelloWorld.java”.
Step 5: Next run the program to get the desired output with the command
“ java HelloWorld ”.
If you are a Windows user and have not downloaded eclipse IDE yet take your time to visit our tutorial-Java IDE installation for Windows to learn the installation and set up of IDE for hassle-free programming. Now let’s familiarize ourselves with the eclipse IDE to write your first program and then to compile and run it.
Step 1: Open the Eclipse IDE. The following window will appear.
Step 2: To create a java project follow the below step which is well picturized for easy understanding.
Click file -> New -> Java Project ->hit Enter.
Step 3: A window as shown below appear on your screen.
Step 4: Now a screen like the one below will appear to you. The window contains 3 parts
Step 5: Now to write your first program in the Eclipse IDE, you need to create a class under your java project. To create a class
Right-click on the MyJavaProject -> then click on New -> then click on the class
Step 6: The following window will be the result
Step 7: On clicking the finish button, the below window will show up with some predefined codes in the editor area. The java class and the main method are created automatically, now you can start writing your codes in the statements.
On the project explorer, you can notice that a java file named HelloWorld.java is created.
Note: Shortcut to run a java code is Ctrl key +F11.
Step 8: In Eclipse IDE both compilation and execution are done by a single button named the “Run” button as shown below.
Step 9: On executing the java code the output will be displayed on the console as in the screenshot below.