Simple Hello World Java Program

This is a very simple Java program for beginners.To create a Hello World Program we need to write codes which are shown below in notepad and save it with the name HelloWorld.java. It must be sure that the file name must match the name of the class.You have to pay attention that this naming convention is CASE SENSITIVE it means that naming your file into helloworld.java WON'T work.


class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}


How to Compile and Run Your Program :

javac HelloWorld.java command is used to compile the source code. A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer(byte-codes. It takes a text file with the .java extension as input (your program) and produces a file with a .class extension (the computer-language version).

Once you compile your program, you can run it.

java HelloWorld command can execute the byte code in the Java interpreter.Notice that there is no .class extension on it.

Output :




Understanding the program :

- class HelloWorld

Class is the basic building block of the java program, java codes are written in the java class.Here HelloWorld is the name of Class and you can Use Any name.

- public static void main (String args[])

This is necessary to for any class to be runnable. This function is the entry point of the execution.

public - This access modifier makes this method accessible everywhere.
static - It will make this main method invokable without creating an instance of this class.
void - void declares that the method does not return any value.
main - it is a thread which the JVM looks first and is the entry point for the program
String args[] - declares a parameter named args, which is an array of instances of the class String.

- System.out.println("Hello World!");

This command is used to print some text on the screen.The text has to be surrounded by the double quotes. In this case, "Hello World!" is being printed. Remember that almost every command in Java ends with a semicolon(;).

No comments:

Post a Comment