Java 简明教程
Java - Hello World Program
在输出屏幕(控制台)上打印“Hello World”是 Java 及其他编程语言中的第一个程序。本教程将教你如何在 Java 编程中编写你的第一个程序(打印“Hello World”程序)。
Printing "Hello World" on the output screen (console) is the first program in Java and other programming languages. This tutorial will teach you how you can write your first program (print "Hello World" program) in Java programming.
Java program to print "Hello World"
在下面给出用于打印“Hello World”的 Java 程序:
Java program to print "Hello World" is given below:
public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}
Steps to Write, Save, and Run Hello World Program
让我们看看怎样来保存文件、编译和运行程序。请遵循以下步骤:
Let’s look at how to save the file, compile, and run the program. Please follow the subsequent steps −
-
Open notepad and add the code as above.
-
Save the file as − "MyFirstJavaProgram.java".
-
Open a command prompt window and go to the directory where you saved the class. Assume it’s C:\.
-
Type 'javac MyFirstJavaProgram.java' and press enter to compile your code. If there is no error in your code, the command prompt will take you to the next line (Assumption − The path variable is set. Learn: Java Envionment Setup).
-
Now, type 'java MyFirstJavaProgram' to run your program.
-
You will be able to see "Hello World" printed on the screen.
Explanation of Hello World Program
既然我们已成功地将“Hello World”打印到输出屏幕上,那么让我们一行一行地来理解代码。
As we’ve successfully printed Hello World on the output screen. Let’s understand the code line by line.
1. Public Main Class
public class MyFirstJavaProgram {
这行代码正在创建一个新的类 MyFirstJavaProgram,而且 是公共的,这个类被定义在与 MyFirstJavaProgram.java 相同的名称文件中。这个约定有助于在读取文件内容之前,帮助 Java compiler 识别要创建的公共类的名称。
This line is creating a new class MyFirstJavaProgram and being public, this class is to be defined in the same name file as MyFirstJavaProgram.java. This convention helps Java compiler to identify the name of public class to be created before reading the file content.
2. Comment Section
/* This is my first java program.
* This will print 'Hello World' as the output
*/
以 /* */ 块中显示的这些行不被 Java 编译器视为 comments。注释有助于更好地理解程序,并使代码可读且易于理解。
These lines being in /* */ block are not considered by Java compiler and are comments. A comment helps to understand program in a better way and makes code readable and understandable.
3. Public Static Void Main
public static void main(String []args) {
这行代码表示当该程序被载入到内存中时,JVM 调用的主方法。该方法用于执行程序。当该方法完成时,程序就在单线程环境中结束了。
This line represents the main method that JVM calls when this program is loaded into memory. This method is used to execute the program. Once this method is finished, program is finished in single threaded environment.
4. Keywords Used
让我们查看该行中各个关键字的用途。
Let’s check the purpose of each keyword in this line.
-
public − defines the scope of the main method. Being public, this method can be called by external program like JVM.
-
static − defines the state of the main method. Being static, this method can be called by external program like JVM without first creating the object of the class.
-
void − defines the return type of the main method. Being void, this method is not returning any value.
-
main − name of the method
-
String []args − arguments passed on command line while executing the java command.