杰瑞科技汇

Java的Hello World程序如何编写与运行?

Of course! Here is the classic "Hello, World!" program in Java, presented with a detailed explanation.

Java的Hello World程序如何编写与运行?-图1
(图片来源网络,侵删)

The Code

This is the complete, minimal code required to print "Hello, World!" to the console.

// File: HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

How to Compile and Run

You need to have the Java Development Kit (JDK) installed on your system.

  1. Save the code: Save the code above into a file named HelloWorld.java. The filename must exactly match the public class name (including the capitalization).

  2. Open a terminal or command prompt: Navigate to the directory where you saved the file.

    Java的Hello World程序如何编写与运行?-图2
    (图片来源网络,侵删)
  3. Compile the code: Use the Java compiler (javac) to turn your human-readable .java file into Java bytecode (a .class file).

    javac HelloWorld.java

    If this command runs without any errors, a new file named HelloWorld.class will appear in your directory.

  4. Run the program: Use the Java Virtual Machine (java) to execute your compiled bytecode.

    java HelloWorld

    You should see the following output printed to your console:

    Java的Hello World程序如何编写与运行?-图3
    (图片来源网络,侵删)
    Hello, World!

Note: Notice that when you run the program, you type java HelloWorld (without the .class extension). This is a common point of confusion for beginners.


Code Breakdown

Let's break down each part of the program to understand what it does.

// 1. The Class Declaration
public class HelloWorld {
    // ... code inside the class ...
}
  • class: This keyword declares a new class. A class is a blueprint for creating objects. In Java, all code must reside inside a class.
  • HelloWorld: This is the name of our class. By convention, class names in Java start with a capital letter (this is known as PascalCase).
  • public: This is an "access modifier." It means the class can be accessed from anywhere.
  • The curly braces define the beginning and end of the class's code block.
// 2. The main Method
    public static void main(String[] args) {
        // ... code inside the method ...
    }
  • This is the most important line for making the program run. When you execute a Java program, the JVM looks for a specific method to start with, and it's always main.
  • public: It can be called from anywhere.
  • static: It belongs to the class HelloWorld itself, not to a specific instance (object) of the class. This means you can run it without creating an object of HelloWorld.
  • void: This means the method does not return any value.
  • main: This is the special, required name for the starting method.
  • String[] args: This is a parameter for the method. It's an array of strings that can hold command-line arguments, which we aren't using in this simple example.
  • The curly braces define the beginning and end of the method's code block.
// 3. The Statement
        System.out.println("Hello, World!");
  • System.out: This is an object that represents the standard output stream, which is typically your console or terminal window.
  • .println(): This is a method (a function) on the System.out object that stands for "print line." It prints the text you give it to the console and then moves the cursor to the next line.
  • "Hello, World!": This is a string literal. Any text you want to display must be enclosed in double quotes.
  • (semicolon): This statement terminator marks the end of a Java statement. It's like a period at the end of a sentence.

Variations

You might also see Hello, World! written using System.out.print() instead of System.out.println(). The difference is subtle but important:

  • System.out.println("Hello, World!"); prints "Hello, World!" and then moves the cursor to a new line.
  • System.out.print("Hello, "); prints "Hello, " but keeps the cursor on the same line.
  • System.out.print("World!"); prints "World!" right after the "Hello, ".

Combined, they produce the same output:

System.out.print("Hello, ");
System.out.println("World!");
分享:
扫描分享到社交APP
上一篇
下一篇