The first program in java
Program is, to print "Hello World" on the screen
After discussing the history of java, we are going to discuss the first simple program in java, which prints "Hello World!" on the screen of the computer. Java file is saved with the ".java" extension. Let's start with the commands, which are used to compile and run the java program on Ubuntu.
Compilation:
Java program file is compiled by using the following command on Ubuntu:javac file_name.java
What happens after compiling the java file? It creates the new file with the same name and .class extension which contains the bytecode version of the program. It also checks the syntax as per java language. It shows errors if any. After successful compilation, we have to run the .class file which contains the byte-code.
Run:
TO run the .class file, use the following command for Ubuntu:java file_name
After entering this command, we will get the output on the screen.
Code for Hello World Program:
Let's see the code line by line with the explanation.---------------------------------------------------------------------------------------------------------------- The first line contains:
/* Hello world program in java */
This is the comment, which is used to provide the information about program. Comments are used in programs for the documentation purpose.---------------------------------------------------------------------------------------------------------------- The Third line contains:
class example{
This line contains the keyword class, example is the identifier which is the name of the class. The name of the class should be as same as the name of the file. The class definition contains its members inside the curly braces {}.---------------------------------------------------------------------------------------------------------------- The fourth line contains:
public static void main(String args[]){
This line contains the main() method. Every program begins with a call to the main() method. The public keyword is an access specifier. We will discuss access specifiers in detail later. The keyword static allows the main( ) to be called without having to instantiate a particular instance of the class. This is necessary since the main() is called before creating any object. The void is the return type of the main() method. This calls to the compiler that, main() method does not return any value. String args[ ] declares a parameter named args, which is an array of instances of the class String. ---------------------------------------------------------------------------------------------------------------- The fifth line contains:
System.out.println("Hello World!");
This line shows the output Hello World!, followed by the new line. If we use the print instead of println, it does not follow by a new line. We will discuss the difference between both of them in the next post.---------------------------------------------------------------------------------------------------------------The next two lines complete the curly braces of class and the main method. This is the small explanation of the simple hello world program. If you have any doubt then comment on it, I will try to solve it.
Thanks.
TO run the .class file, use the following command for Ubuntu:
Let's see the code line by line with the explanation.
- The first line contains:
- The Third line contains:
class example{
This line contains the keyword class, example is the identifier which is the name of the class. The name of the class should be as same as the name of the file. The class definition contains its members inside the curly braces {}.
---------------------------------------------------------------------------------------------------------------
- The fourth line contains:
public static void main(String args[]){
This line contains the main() method. Every program begins with a call to the main() method. The public keyword is an access specifier. We will discuss access specifiers in detail later. The keyword static allows the main( ) to be called without having to instantiate a particular instance of the class. This is necessary since the main() is called before creating any object. The void is the return type of the main() method. This calls to the compiler that, main() method does not return any value. String args[ ] declares a parameter named args, which is an array of instances of the class String.
---------------------------------------------------------------------------------------------------------------
- The fifth line contains:
System.out.println("Hello World!");
This line shows the output Hello World!, followed by the new line. If we use the print instead of println, it does not follow by a new line. We will discuss the difference between both of them in the next post.
---------------------------------------------------------------------------------------------------------------
The next two lines complete the curly braces of class and the main method. This is the small explanation of the simple hello world program. If you have any doubt then comment on it, I will try to solve it.
Thanks.
Comments
Post a Comment