7.Java Program Example--hello world!

Source: Internet
Author: User

Following the world trend, the first Java program outputs "Hell world!".

Running programs through eclipse

Start eclipse, and in the menu, choose File-New-to-Java project to bring up the dialog box:


Figure 1 New Project


Fill in the project name, click "Done", create a successful project, you can see in the E:\java\workspace directory, a more demo folder.

In the menu, choose File-New-Class, which pops up the dialog box:


Figure 2 Creating a class


Enter the class name and click "Finish" to successfully create a HelloWorld class:


Figure 3 The newly created class file


Enter the following code into the source file, noting that Java is case-sensitive:

    1. Public class HelloWorld {
    2. public static void main(String[] args){
    3. System. Out. println("Hello world!" );
    4. }
    5. }


Click the Run button, or select "Run-to-run" in the menu to see the console output "Hello world!" 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 bytecode generated by the compilation. Unlike C, C + +, the result of Java compilation is not an executable file, but a byte-code file. Bytecode files cannot be run directly and must be translated by the JVM into machine code to run, which is why the JVM must be installed to run the Java program.

To run a program from the command line

Of course, you can also enter commands directly to compile and run Java.

Open CMD,CD to the directory where the source file is located, complete the compilation through the javac filename command, and run the program through the Java ClassName command as follows:


Figure 5 Compiling and running a Java program with commands


Running the program in eclipse actually executes the commands and displays the output to the console. With eclipse, programming is more convenient.

HelloWorld Program Analysis

Analyze the code above, as described at the beginning of the program.
public class HelloWorld
A class is defined, the class is public, 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", then the file should be saved as "Helloworld.java".
public static void Main (string[] args)
The main running method in Java, which is the same as main () in C + +, is that all programs start from "Main ()". To execute a Java program, you must have a class that includes the main run method. As for the meaning of "public static void", the reader can try to remove it and see what error the compiler is suggesting.
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.

Let's not rush to understand all the concepts and follow up on them.

7.Java Program Example--hello world!

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.