Java program to print 'Hello world' (First program in Java)

Learn: How to write, compile and execute our first program in Java? in this program we will print some text ("Hello World") on the screen.

Welcome! Now, you are in the world of Java programming. Here we will learn to write, compile and execute a java program.

1) Writing a java program

You can write a java program in any simple text editor like Notepad, Notepad++ or any IDE (Integrated Development Environment) which supports Java compilation and execution, but here we are talking about writing java program - So use any text editor, write your program (a basic program in given below) and save the file with extension .java

Here, we are saving it with "HelloWorld.java".

2) Compiling java program

In java compilation, it’s a process to convert text file (.java file/java program) to class file. Java compiler converts Java source code to the class file and produced output file will be "HelloWorld.class".

Note: public class name will be the class file name.

To compile java program, follow following syntax:

    javac HelloWorld.java

Here, javac is the name of Java compiler.

3) Executing/Running java program

Once you compile the Java program and if it compiled successfully, you can execute Java program to produce the output.

To execute class file, follow following syntax:

    java HelloWorld

Here, "HelloWorld" is the name of class file (it may be different)


Program to print "Hello World" in Java

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

Output

Hello World!

Understanding the program

1) public class HelloWorld

Name of the public class, if there is no public class defined in the program, the class which has main() method will be class file after compiling the program.

2) public static void main(String []args)

main() method that calls when program is being executed. String []args - object of string array for compile line arguments.

3) System.out.println()

Method that prints the text and/or values on the output screen.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.