Начало программирования на Java с примера Hello World

Опубликовано: 15 Февраля, 2022

Процесс программирования на Java можно упростить в три этапа:

  • Создайте программу, набрав ее в текстовом редакторе и сохранив в файл HelloWorld.java.
  • Скомпилируйте его, набрав «javac HelloWorld.java» в окне терминала.
  • Выполните (или запустите) его, набрав «java HelloWorld» в окне терминала.

Рекомендуется: сначала решите эту проблему на «ПРАКТИКЕ», прежде чем переходить к решению.

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". */
class HelloWorld
{
    // Your program begins with a call to main().
    // Prints "Hello, World" to the terminal window.
    public static void main(String args[])
    {
        System.out.println("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:

  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 out is 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 :

  • 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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.