Compile and run your first Java program, experiment line by line in JShell, and understand the main method that every program starts from.
Why: unlike some languages, Java runs in two steps. First the compiler (javac) turns your .java text file into bytecode (a .class file). Then the java command runs that bytecode. The file name must match the public class name exactly — Hello.java holds class Hello.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}When: run these from the same folder as the file. javac compiles, producing Hello.class; java then runs it.
Compile, then run (note: no .class extension when running):
javac Hello.javajava HelloWhy: every Java program starts at a method with this exact signature. "public" means anything can call it, "static" means it belongs to the class itself (no object needed), "void" means it returns nothing, and String[] args holds any words you typed after the program name. Memorise this line — you will write it constantly.
public class Greet {
public static void main(String[] args) {
// args holds command-line words: java Greet Ada -> args[0] is "Ada"
String name = args.length > 0 ? args[0] : "world";
System.out.println("Hello, " + name + "!");
}
}Why: JShell is a live prompt where you type Java and instantly see the result — no class or main method needed. It is perfect for trying things out. Start it by typing "jshell". Type /exit to leave.
jshelljshell> 2 + 2jshell> String name = "Ada"jshell> name.toUpperCase()jshell> /exit
Comments & printing
Why: a comment is a note for humans that Java ignores. Use // for a single line and /* ... */ for a block. System.out.println() prints a line (with a line break); System.out.print() prints without one.