hello world program in java
To write the simple program, an open notepad by start menu -> All Programs -> Accessories -> notepad and write a simple program as displayed below:
Simple Program of Java
In this page, we will learn how to write the simple program of java. We can write a simple hello java program easily after installing the JDK.
To create a simple java program, you need to create a class that contains the main method. Let's understand the requirement first.
The requirement for Hello Java Example
For executing any java program, you need to
Let us look at a simple java program.
class Hello { public static void main(String[] args) { System.out.println ("Hello World program"); } } |
Simple Program of Java
Steps to Compile and Run your first Java program
Step 1: Open a text editor and write the code as above.
Step 2: Save the file as Hello.java
Step 3: Open a command prompt and go to the directory where you saved your first java program assuming it is saved in C:\
Step 4: Type and
javac Hello.java
press toReturn
compile your code. This command will call the Java Compiler asking it to compile the specified file. If there are no errors in the code the command prompt will take you to the next line.
Step 5: Now type
java Hello
on command prompt to run your program.
Step 6: You will be able to see Hello world program printed on your command prompt.
Parameters used in a first java program
Let's see what is the meaning of class, public, static, void, main, String[], System.out.println().
- 1- the class keyword is used to declare a class in java.
- 2- public keyword is an access modifier which represents visibility, it means it is visible to all.
- 3- static is a keyword, if we declare any method as static, it is known as a static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create an object to invoke the main method. So it saves memory.
- 4- void is the return type of the method, it means it doesn't return any value.
- 5- main represents the starting point of the program.
- 6- String[] args is used for command line argument. We will learn it later.
- 7- System.out.println() is used print statement. We will learn about the internal working of System.out.a println statement later.
Now let us see What happens at Runtime
After writing your Java program, when you will try to compile it. The compiler will perform some compilation operation on your program.Once it is compiled successfully bytecode(.class file) is generated by the compiler.
After compiling when you will try to run the bytecode(.class file), the following steps are performed at runtime:-
1- Classloader loads the java class. It is a subsystem of JVM Java Virtual machine.
2- ByteCode verifier checks the code fragments for illegal codes that can violate access right to the object.
3- The interpreter reads the bytecode stream and then executes the instructions, step by step.
To write the simple program, an open notepad by start menu -> All Programs -> Accessories -> notepad and write a simple program as displayed below:
As displayed in the above diagram, write the simple program of java in notepad and saved it as Simple.java. To compile and run this program, you need to open a command prompt by start menu -> All Programs -> Accessories -> command prompt
To compile and run the above program, go to your current directory first; my current directory is c:\new . Write here: |
To compile: | javac Simple.java |
To execute: | java Simple |
Comments
Post a Comment