Java First Program

Beginning Java programming with Hello World Example

Tech Jitendra provides ,

In this article we are going to discuss on java first program.

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:

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./* This is a simple Java program. FileName : "HelloWorld.java". */classHelloWorld { // Your program begins with a call to main(). // Prints "Hello, World" to the terminal window. publicstaticvoidmain(String args[]) { System.out.println("Hello, World"); } }

Output:Hello, World

The “Hello World!” program consists of three primary components: the HelloWorld class definition, the mainmethod and source code comments. Following explanation will provide you with a basic understanding of the code:

  1. 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  } .
  2. 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.
  3. 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 outis the variable of type output stream that is connected to the console.
  4. 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 :

Java : hello word .

Thanking you

Tech Jitendra

Published by Tech Jitendra Direct Investing

Tech Jitendra Direct Investing (TJDI) Empoworing Your Wealth

Leave a comment

Design a site like this with WordPress.com
Get started