杰瑞科技汇

Java hello world 如何正确编写运行?

Of course! Here is a complete guide to creating and running a "Hello, World!" program in Java, broken down into simple steps.

We'll cover two main ways:

  1. The Manual Way: Using a plain text editor and the command line. This is great for understanding the fundamentals.
  2. The IDE Way: Using an Integrated Development Environment like IntelliJ IDEA. This is what most professional developers use.

Method 1: The Manual Way (Using a Text Editor and Command Line)

This method involves three steps:

  1. Write the code in a file named HelloWorld.java.
  2. Compile the code into bytecode using the Java compiler (javac).
  3. Run the code using the Java Virtual Machine (java).

Step 1: Write the Code

  1. Open any plain text editor (like Notepad on Windows, TextEdit on Mac, or VS Code).
  2. Copy and paste the following code into the editor:
public class HelloWorld {
    public static void main(String[] args) {
        // This line prints "Hello, World!" to the console.
        System.out.println("Hello, World!");
    }
}
  1. Save the file with the exact name HelloWorld.java. The filename must match the public class name exactly.

Step 2: Compile the Code

  1. Open a terminal or command prompt.

    • On Windows: Search for "cmd" or "PowerShell".
    • On macOS: Open the "Terminal" app (you can find it in Applications > Utilities).
    • On Linux: Open your distribution's terminal application (e.g., "Console" or "Terminal").
  2. Navigate to the directory where you saved HelloWorld.java. Use the cd (change directory) command. For example, if you saved it on your Desktop:

    cd Desktop
  3. Compile the file using the Java compiler (javac):

    javac HelloWorld.java

    If the compilation is successful, you will not see any messages. Instead, a new file named HelloWorld.class will appear in the same directory. This is the Java bytecode that the JVM can execute.

Step 3: Run the Code

Now, run the compiled bytecode using the java command:

java HelloWorld

Important: Notice that you do not include the .class extension when running the program. You are telling the java command which class to run, not which file to execute.

You should see the following output printed to your console:

Hello, World!

Congratulations! You've just written, compiled, and run your first Java program.


Method 2: The IDE Way (Using IntelliJ IDEA)

An IDE (Integrated Development Environment) provides a graphical interface, code completion, and tools to make development much easier.

Step 1: Install IntelliJ IDEA

  1. Go to the JetBrains IntelliJ IDEA website.
  2. Download the Community Edition. It's free and perfect for learning Java.
  3. Follow the installation instructions for your operating system.

Step 2: Create a New Project

  1. Open IntelliJ IDEA.
  2. Click on "New Project".
  3. In the new window, make sure the following are selected:
    • Name: HelloWorld (or any name you like).
    • Language: Java.
    • Build system: IntelliJ (it's the simplest for this task).
    • JDK: Select a JDK (Java Development Kit) version. If you don't have one, IntelliJ will help you download one. JDK 11 or 17 are great choices.
  4. Click "Create".

Step 3: Write the Code

  1. IntelliJ will create a project structure. In the src (source) folder, right-click and select New > Java Class.
  2. Name your class HelloWorld and press Enter.
  3. IntelliJ will automatically create a file with a basic structure. Replace the contents with the following code:
public class HelloWorld {
    public static void main(String[] args) {
        // This line prints "Hello, World!" to the console.
        System.out.println("Hello, World!");
    }
}

Step 4: Run the Code

  1. Right-click anywhere inside the HelloWorld.java file.
  2. Select 'Run 'HelloWorld.main()'' from the context menu.

Alternatively, you can click the green "play" button (▶) in the toolbar at the top of the window.

The output will appear in a "Run" window at the bottom of the screen:

Hello, World!

Code Breakdown

Let's break down the code so you understand what each part does.

// This is a comment. The compiler ignores it.
public class HelloWorld {
    // This is the main method. It's the entry point of any Java application.
    public static void main(String[] args) {
        // System is a built-in Java class.
        // out is a static field in the System class (a PrintStream object).
        // println() is a method of the PrintStream class that prints a line of text.
        System.out.println("Hello, World!");
    }
}
  • public class HelloWorld: This declares a class named HelloWorld. In Java, all code must reside inside a class. public means it can be accessed from anywhere.
  • public static void main(String[] args): This is the main method. It's the special method where the Java Virtual Machine (JVM) starts executing your program.
    • public: It can be called from anywhere.
    • static: It belongs to the class itself, not to a specific object instance. This allows the JVM to run it without creating an object first.
    • void: This method does not return any value.
    • main: The name of the method, which is the standard entry point.
    • (String[] args): This is a parameter that can accept command-line arguments.
  • System.out.println("Hello, World!");: This is the command that does the work.
    • System: A core Java class that contains system-level functionality.
    • out: A static PrintStream object in the System class, representing the standard output (usually your console).
    • println(): A method that prints the text inside the parentheses to the console and then moves the cursor to the next line.
分享:
扫描分享到社交APP
上一篇
下一篇