Beginning Java programming with Hello World Example
The process of Java programming can be simplified in three steps:
- Create the program by typing it into a text editor and saving it to a file – HelloWorld.java.
- Compile it by typing “javac HelloWorld.java” in the terminal window.
- Execute (or run) it by typing “java HelloWorld” in the terminal window.
Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.
Below given program is the simplest program of Java printing “Hello World” to the screen. Let us try to understand every bit of code step by step.
filter_none
edit
play_arrow
brightness_4
|
Output:
Hello, World
The “Hello World!” program consists of three primary components: the HelloWorld class definition, the main
method and source code comments. Following explanation will provide you with a basic understanding of the code:
- Class definition:This line uses the keyword class to declare that a new class is being defined.
class HelloWorld
HelloWorld is an identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace { and the closing curly brace } .
- main method: In Java programming language, every application must contain a
main
method whose signature is:public static void main(String[] args) public: So that JVM can execute the method from anywhere. static: Main method is to be called without object. The modifiers public and static can be written in either order. void: The main method doesn't return anything. main(): Name configured in the JVM. String[]: The main method accepts a single argument: an array of elements of type String.
Like in C/C++, main method is the entry point for your application and will subsequently invoke all the other methods required by your program.
- The next line of code is shown here. Notice that it occurs inside main( ).
System.out.println("Hello, World");
This line outputs the string “Hello, World” followed by a new line on the screen. Output is actually accomplished by the built-in println( ) method. System is a predefined class that provides access to the system, and out is the variable of type output stream that is connected to the console.
- Comments: They can either be multi-line or single line comments.
/* This is a simple Java program. Call this file "HelloWorld.java". */
This is a multiline comment. This type of comment must begin with /* and end with */. For single line you may directly use // as in C/C++.
Important Points :
- The name of the class defined by the program is HelloWorld, which is same as name of file(HelloWorld.java). This is not a coincidence. In Java, all codes must reside inside a class and there is at most one public class which contain main() method.
- By convention, the name of the main class(class which contain main method) should match the name of the file that holds the program.
Compiling the program :
- After successfully setting up the environment, we can open terminal in both Windows/Unix and can go to directory where the file – HelloWorld.java is present.
- Now, to compile the HelloWorld program, execute the compiler – javac , specifying the name of the source file on the command line, as shown:
javac HelloWorld.java
- The compiler creates a file called HelloWorld.class (in present working directory) that contains the bytecode version of the program. Now, to execute our program, JVM(Java Virtual Machine) needs to be called using java, specifying the name of the class file on the command line, as shown:
java HelloWorld
This will print “Hello World” to the terminal screen.
-
In Windows
In Linux
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
-
post a comment