1. Overview
1.概述
Java is a general-purpose programming language that focuses on the WORA (Write Once, Run Anywhere) principle.
Java是一种通用的编程语言,注重WORA(Write Once, Run Anywhere)原则。
It runs on a JVM (Java Virtual Machine) that is in charge of abstracting the underlying OS, allowing Java programs to run almost everywhere, from application servers to mobile phones.
它在JVM(Java虚拟机)上运行,负责对底层操作系统进行抽象,使Java程序几乎可以在任何地方运行,从应用服务器到移动电话。
When learning a new language, “Hello World” is often the first program we write.
在学习一门新语言时,”Hello World “往往是我们写的第一个程序。
In this tutorial, we’ll learn some basic Java syntax and write a simple “Hello World” program.
在本教程中,我们将学习一些基本的Java语法并编写一个简单的 “Hello World “程序。
2. Writing the Hello World Program
2.编写 “Hello World “程序
Let’s open any IDE or text editor and create a simple file called HelloWorld.java:
让我们打开任何IDE或文本编辑器,创建一个简单的文件,名为HelloWorld.java。
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
In our example, we’ve created a Java class named HelloWorld containing a main method that writes some text to the console.
在我们的例子中,我们创建了一个名为HelloWorld的Java类,其中包含一个main方法,可以向控制台写入一些文本。
When we execute the program, Java will run the main method, printing out “Hello World!” on the console.
当我们执行程序时,Java将运行main方法,在console上打印出 “Hello World!”。
Now, let’s see how we can compile and execute our program.
现在,让我们看看如何编译和执行我们的程序。
3. Compiling and Executing the Program
3.编译和执行程序
In order to compile a Java program, we need to call the Java compiler from the command line:
为了编译一个Java程序,我们需要从命令行调用Java编译器。
$ javac HelloWorld.java
The compiler produces the HelloWorld.class file, which is the compiled bytecode version of our code.
编译器产生HelloWorld.class文件,这是我们代码的编译字节码版本。
Let’s run it by calling:
让我们通过调用来运行它。
$ java HelloWorld
and see the result:
并看到结果。
Hello World!
4. Conclusion
4.总结
With this simple example, we created a Java class with the default main method printing out a string on the system console.
通过这个简单的例子,我们创建了一个Java类,其默认的main方法在系统控制台上打印出一个字符串。
We saw how to create, compile, and execute a Java program and got familiar with a little bit of basic syntax. The Java code and commands we saw here remain the same on every OS that supports Java.
我们看到了如何创建、编译和执行一个Java程序,并熟悉了一点基本语法。我们在这里看到的Java代码和命令在每个支持Java的操作系统上都是一样的。