Java 教程是为 JDK 8 编写的。本页中描述的示例和实践未利用在后续版本中引入的改进。
问题1:编译用 Java 编程语言编写的程序时,编译器会将人类可读的源文件转换为 Java 虚拟机可以理解的与平台无关的代码。这个独立于平台的代码是什么?
答案1:字节码。
问题2:以下哪项是 不是 有效评论:
a. /** comment */
b. /* comment */
c. /* comment
d. // comment
答案2:c 是无效的评论。
问题3:如果在运行时看到以下错误,应该检查的第一件事是什么:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp.java.
答案3:检查你的类路径。你的类无法找到。
问题4:main 方法的正确签名是什么?
答案4:正确的签名是 public static void main(String[] args) 或 public static void main(String... args)
问题5:声明 main 方法时,哪个修饰符必须先出现,public 或 static?
答案5:它们可以是任意顺序,但规范是 public static。
问题6:main 方法定义了哪些参数?
答案6:main 方法定义了一个参数,通常名为 args,其类型是 String 对象的数组。
练习1:更改 HelloWorldApp.java 程序,使其显示 Hola Mundo! 而不是 Hello World!。
答案1:这是必须更改的唯一代码行:
System.out.println("Hola Mundo!"); //Display the string.
练习2:你可以在此处找到 HelloWorldApp 的略微修改版本:HelloWorldApp2.java
该程序有错误。修复错误,以便程序成功编译并运行。错误是什么?
答案2:这是你尝试编译程序时遇到的错误:
HelloWorldApp2.java:7: unclosed string literal
System.out.println("Hello World!); //Display the string.
^
HelloWorldApp2.java:7: ')' expected
System.out.println("Hello World!); //Display the string.
^
2 errors
为了解决这个错误,你需要关闭字符串周围的引号。这是正确的代码行:
System.out.println("Hello World!"); //Display the string.