Core Java - Hello Word Program / Application
Here is the first program / application in Java, which is known as Hello World! program / application of Java.
Steps:
- Open Notepad or Textpad or any similar Text Editor
- Create New file in that and write following code in that:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}
- Now save this file as HellowWorld.java (e.g you have saved at c:\test\HelloWorld.java) (In Java you should give file name as
.java, in this case HelloWorld is the class name so we save it as HelloWorld.java) Now go to Command prompt Change the directory of the command prompt to where you have saved HelloWorld.java
e.g. cd c:\test
Here type the command javac <java file name>
javac HelloWorld.java
This command will compile the Java file Now to run this program give command java <class name which has main method> java HelloWorld
This program will run and give output as follows: Hello world!
- In the above program public class HelloWorld defines the class name in Java. Declaring public as access modifier means it is accessible outside.
- The line public static void main(String[] args)
is the method which will be called first by the JVM when your try to run any application or program is java. We must declare this method as public since it will be called by JVM We must declare this method static since JVM will call this method by class name The main method always have arguments String[] this stores command line arguments if any
No comments yet