First Java program Example--hello World!__java

Source: Internet
Author: User
From http://www.weixueyuan.net/view/5947.html
running programs through EclipseLaunch Eclipse, select "File--> new--> Java Project" from the menu, pop-up dialog box:
Figure 1 New Project
Fill in the project name, click "Finish", create project success, you can see in the E:\java\workspace directory more than a demo folder.

In the menu, select File--> new--> Class, pop-up dialog box:
Figure 2 Creating a class
Enter the class name and click "Finish" to create a HelloWorld class successfully:
Figure 3 The newly created class file
Enter the following code into the source file, noting that Java is case sensitive:
public class HelloWorld {public
    static void Main (string[] args) {
        System.out.println ("Hello world!");
    }

Click on the Run button, or select "Run-->" in the menu, and you can see the "Hello world!" output from the console below. :
Figure 4 Console output
Congratulations, you have successfully written and run the first Java program.

Open the Bin directory under the Demo folder, and a Helloworld.class file appears, which is the byte code that compiles the build. Different from C, C + +, the result of Java compilation is not an executable file, but a bytecode file. Bytecode files cannot run directly, and must be translated by the JVM into machine code to run, which is why running a Java program must install the JVM. To run a program from the command lineOf course, you can also directly enter commands to compile and run Java.

Open CMD,CD to the directory where the source files are located, complete the compilation via the javac filename command, and run the program through the Java ClassName command, as follows:
Figure 5 Compiling and running the Java program by command
Running programs in Eclipse is actually executing these commands and displaying the output to the console. With eclipse, programming is more convenient. HelloWorld Program AnalysisAnalyze the above code and introduce it at the beginning of the program.
public class HelloWorld
Defines a class, which is a public type, and the class is named "HelloWorld". In addition, the main class name in Java should be the same as the Java file name to be saved, that is, the class name defined here is "HelloWorld" and the file should be saved as "Helloworld.java".
public static void Main (string[] args)
A master-run method in Java, which is the same as the main () function in C + +, where all programs start in "Main ()". To execute a Java program, you must have a class that includes the main running method. As for the meaning of "public static void", readers can try to remove it and see what errors the compiler prompts.
System.out.println ("Hello World");
"System.out.println ()" is a method of the Java.lang package that is used to output the string "Hello World" to a command line window.

Everyone should not rush to understand all the concepts, the follow-up will be explained.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.