Key points

  • How the JDK, JRE and JVM relate: the JDK contains the JRE, and the JRE contains the JVM. The JRE can only run programs and has no javac compiler; javac is in the JDK. With only a JVM, you can only run bytecode.
  • What each part of the main method signature means: public is the access level; static is the static keyword, which means main can be used without creating an object; void means it returns nothing; main is the method name; String[] args is the parameter, a temporary variable named args of type String array.
  • The file extension produced by compiling .java: running javac Hello.java produces a file with the .class extension, which is a bytecode file.
  • What javac and java each take: the java command takes a class name, not a file name, so running java Hello.class fails with an error saying the main class cannot be found.

Code and examples

(None this round)

Going further

  • Before running javac and java from the command line, you usually need to add the JDK’s bin directory to PATH; otherwise the system may not find these two commands.
  • If a source file declares a public class, the file name must match that class name exactly: public class Hello must be saved as Hello.java.
  • A .class file is platform-independent bytecode. The JVM on the target machine is what actually runs it, so the same .class file can run on different operating systems.

要点

  • 能说出JDK、JRE、JVM三者包含关系:JDK包含JRE,JRE包含JVM。JRE只能运行程序,没有javac编译器;javac编译器在JDK里,只装JVM只能运行字节码。
  • 能解释main方法签名的每个部分含义:main方法签名中,public表示访问权限;static是静态关键词,意味着无需创建对象即可使用main方法;void表示没有返回值;main是方法名;String[] args表示传参,即一个名叫args的String数组类型的临时变量。
  • 能说出.java编译后生成的文件后缀名:执行 javac Hello.java 后生成的文件后缀名为 .class,该文件是字节码文件。
  • 能区分javac和java命令的执行对象:java 命令后面跟的是类名,不是文件名,因此执行 java Hello.class 会报错,找不到主类。

代码与例句

(本轮无)

延伸

  • 在命令行直接运行 javac 和 java 前,通常要把 JDK 的 bin 目录加入 PATH,否则系统可能找不到这两个命令。
  • 如果源文件中声明了 public class,文件名必须与该 public 类名完全一致,例如 public class Hello 必须保存为 Hello.java。
  • .class 文件是平台无关的字节码,真正运行它的是目标机器上的 JVM,因此同一份 .class 可在不同操作系统上运行。

相关