Java · Lesson 1 of 6
Hello, Java
JDK setup, your first class, and the main method.
- Beginner
- 10 min read
- 3 objectives
What you will learn
- Understand public class and main
- Compile with javac
- Run with java
Java is a compiled, statically typed language. You write source code in .java files, the compiler (javac) turns it into bytecode (.class files), and the Java Virtual Machine (JVM) runs that bytecode. Because the JVM exists on every major platform, the same compiled program runs on Windows, macOS and Linux. That portability, plus speed and a huge ecosystem, is why Java powers so many banks, Android apps and backend services.
Why Java?
Java has been one of the most widely used languages for almost 30 years. It powers Android apps, banking and payment systems, large web backends and huge data platforms. Its design goal was write once, run anywhere: your program is compiled to an intermediate form that any computer with a Java Virtual Machine can run. Java is also strictly typed, meaning the compiler checks your code before it ever runs, which catches a whole category of mistakes early. That makes it a demanding first language but an excellent way to learn how programming really works.
JDK, JRE and JVM
- JVM: the engine that runs bytecode.
- JRE: the JVM plus the standard libraries needed to run programs.
- JDK: the JRE plus developer tools such as the compiler. Install a JDK (17 or newer) to follow this course.
java -version
javac -versionYour first class
Every Java program lives inside a class. The program starts at the main method, which must have exactly this signature. The file name must match the public class name, so this class goes in Hello.java.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, stackcone!");
}
}Reading the signature word by word:
public: callable from anywhere, so the JVM can start it.static: belongs to the class itself, so no object is needed to call it.void: returns nothing.String[] args: command-line arguments, as an array of strings.
Compile and run
javac Hello.java
java HelloHello, stackcone!
javac produces Hello.class; java Hello runs it (note: no .class extension). Since Java 11 you can also run a single file directly with java Hello.java.
What every word in Hello World means
Java programs look wordy at first. Here is the smallest program, with each piece explained so none of it stays a mystery:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}Hello, Java!
public class Main: all Java code lives inside a class. The file must be namedMain.javato match.public static void main(String[] args): the entry point. Java starts running here.voidmeans it returns nothing;argsholds any command-line arguments.System.out.println(...): prints text followed by a new line.System.out.printomits the new line.- Every statement ends with a semicolon, and blocks are wrapped in curly braces.
Compile, then run
Unlike Python, Java is normally compiled first: javac turns your .java file into .class bytecode, and java runs that bytecode on the JVM. Modern Java (11 and later) can also run a single file directly.
javac Main.java # compile -> creates Main.class
java Main # run the bytecode
java Main.java # or compile and run a single file in one stepReading your first compiler error
The compiler is strict, and its messages are precise. Forget a semicolon and it tells you the file, the line and the problem.
Main.java:3: error: ';' expected
System.out.println("Hello, Java!")
^
1 errorThe ^ marks where Java noticed the problem. Fix that spot and compile again. Because the compiler refuses to build broken code, many bugs never reach a running program.
Printing values
public class Main {
public static void main(String[] args) {
int apples = 3;
System.out.println("Apples: " + apples);
System.out.println(2 + 3);
System.out.println("2 + 3 = " + (2 + 3));
System.out.printf("Pi is about %.2f%n", 3.14159);
}
}Apples: 3 5 2 + 3 = 5 Pi is about 3.14
Common mistakes
- Missing semicolon: every statement ends with
;. - Class and file name mismatch:
class Helloinhello.javafails to compile. - Case sensitivity:
system.out.printlnis an error; it isSystemwith a capital S. - Braces: every
{needs a matching}.
Key takeaways
- Java is compiled to bytecode and runs on the JVM, so the same program runs everywhere.
- Code lives in classes; execution starts at
public static void main. - Statements end with
;, blocks use{ }, and the compiler checks types before running. javaccompiles,javaruns; read compiler errors from the file and line number.
// Write your solution here
