Writing your very first program! This classic "Hello, World!" example is perfect for getting started because it introduces basic Java syntax without being overly complicated. Don't worry if every line doesn't make perfect sense right now; we'll break down the details in future tutorials. For now, just follow these steps to write and run your program.
The Code
Here's the simple Java program you'll be working with:
Java
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Step-by-Step Instructions
Open a Text Editor:
Start by opening a plain text editor on your computer. Notepad (Windows), TextEdit (macOS), or any code editor like VS Code or Sublime Text will work perfectly.
Write the Code:
Carefully type or copy the Java code provided above into your open text editor. Pay close attention to capitalization, punctuation, and curly braces {}.
Save the File:
This is a crucial step! Save the file with the name Main.java.
It's important that the file name exactly matches the class name declared in the code (public class Main).
Choose a simple, easy-to-access location, like a new folder on your desktop (e.g., C:\JavaPrograms on Windows or ~/Desktop/JavaPrograms on macOS/Linux).
Open Your Command Prompt/Terminal:
You'll need to use your computer's command line to compile and run the program.
Windows: Search for "Command Prompt" in your Start menu.
macOS/Linux: Search for "Terminal."
Navigate to Your Program's Location:
In the command prompt/terminal, use the cd (change directory) command to go to the folder where you saved Main.java.
For example, if you saved it in C:\JavaPrograms, type:
Bash
cd C:\JavaPrograms
If you saved it in ~/Desktop/JavaPrograms on macOS/Linux, type:
Bash
cd ~/Desktop/JavaPrograms
Compile the Program:
Now, use the Java compiler (javac) to convert your human-readable Java code into bytecode that the computer can understand. Type:
Bash
javac Main.java
Press Enter. If there are no errors, you won't see much output, but a new file named Main.class will be created in the same folder. This is your compiled program!
Run the Program:
Finally, use the Java Virtual Machine (java) to execute your compiled program. Type:
Bash
java Main
Important: Notice you don't include the .java or .class extension here. Just the class name Main.
See the Output!
After pressing Enter, you should see:
Hello, World!
Congratulations! You've just written and run your first Java program!
This "Hello, World!" program is your first step into the world of Java. As the note says, it helps you understand the basic structure. Are you ready to move on and explore what each part of this program actually does?
视频信息
答案文本
视频字幕
Welcome to Java programming! The Hello World program is the traditional first step for any programmer. This simple program demonstrates the basic structure of a Java application. When you run this code, it will display the text Hello World on your screen. Let's examine what makes this program work.
Now let's break down the structure of our Hello World program. The code consists of three main parts. First, we have the class declaration which creates a container called Main. Second, the main method serves as the entry point where program execution begins. Finally, the print statement outputs our message to the console. Each part uses specific Java syntax with curly braces, semicolons, and keywords.
Welcome to Java programming! Today we'll write the classic Hello World program. This simple example introduces basic Java syntax without being overly complicated. Don't worry if every line doesn't make perfect sense right now - we'll break it down step by step!
Let's start with the first two steps. Step one: open a plain text editor on your computer. This could be Notepad on Windows, TextEdit on Mac, or any code editor like VS Code. Step two: carefully type or copy the Java code. Pay close attention to capitalization, punctuation, and curly braces. Every opening brace must have a matching closing brace!
Step three is crucial: saving your file correctly. You must save the file as Main dot java. The filename must exactly match the class name declared in your code. This is case-sensitive, so it must be Main with a capital M, not main with a lowercase m. Choose a simple, easy-to-access location like a JavaPrograms folder on your desktop.
Now for steps four through six. Step four: open your command prompt on Windows or terminal on Mac and Linux. Step five: navigate to the folder where you saved your Java file using the cd command. Step six: compile your program using javac Main dot java. If there are no errors, this creates a Main dot class file containing the compiled bytecode.
Congratulations! You've successfully written, compiled, and run your first Java program! You should see Hello World printed on your screen. This Hello World program is your first step into the world of Java programming. In our next tutorials, we'll explore what each part of this program actually does and why it's structured this way. Great job on completing your first program!
Now for steps four through six. Step four: open your command prompt on Windows or terminal on Mac and Linux. Step five: navigate to the folder where you saved your Java file using the cd command. Step six: compile your program using javac Main dot java. If there are no errors, this creates a Main dot class file containing the compiled bytecode.
Congratulations! You've successfully written, compiled, and run your first Java program! You should see Hello World printed on your screen. This Hello World program is your first step into the world of Java programming. In our next tutorials, we'll explore what each part of this program actually does and why it's structured this way. Great job on completing your first program!