| Previous | Notes index | Next |
|---|---|---|
| Previous: Notes index | Notes index | Next: Variables, primitive types, and references |
The Java Development Kit (JDK) provides the compiler, runtime, standard libraries, and developer tools used to build Java programs. The Java Virtual Machine (JVM) loads and executes Java bytecode.
Check that the JDK commands are available:
java --version
javac --versionjava runs a program. javac compiles Java source code. The exact version depends on the JDK installed on the computer.
Save this source file as Main.java. A public top-level class named Main must be in a file named Main.java.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}main is the program entry point for this traditional Java application form. System.out.println writes a line of text to the terminal.
From the folder containing Main.java, compile the source and run the resulting class:
javac Main.java
java Mainjavac creates a Main.class file containing bytecode. java asks the JVM to load the class and call its main method. The command uses the class name Main, not the source filename.
jshell is an interactive tool included with the JDK. It can evaluate expressions without creating a source file.
jshell> int count = 3;
jshell> count * 2
$2 ==> 6
Use it for small experiments. Keep complete programs in source files so they can be compiled, tested, and maintained.
Change the printed message, save the file, compile again, and run it. If a compiler error appears, read the first reported line and compare its location with the source.
Common first checks include matching braces, spelling class and method names consistently, and saving the public class in the matching filename.
- What does the JDK provide?
- What does the JVM do?
- Which command compiles a Java source file?
- Which command runs a compiled Java class?
- What file is produced by compiling Main.java?
- Why must the public class name match the source filename?
- What is the purpose of the main method in this example?
- When is jshell useful?