Java Notes
Java code is written in a .java file, checked by javac, changed into a .class file, and then run by the JVM.
Your Java program does not run directly from the file you type. First, Java checks the code and creates a compiled file. Then the JVM runs that compiled file.
The simple path is: write code → compile code → run program.
Follow one program from start to finish
Imagine you create a file named Calculator.java. You write normal Java instructions inside it.
| Step | What happens in simple words | File or command involved |
|---|---|---|
| 1. Write | You type Java code that a person can read. | Calculator.java |
| 2. Compile | Java checks spelling, brackets, types, and rules. | javac Calculator.java |
| 3. Create class file | If there is no error, Java creates a compiled file. | Calculator.class |
| 4. Run | The JVM reads the compiled file and follows main. | java Calculator |
The .class file contains bytecode. Bytecode is the form Java uses between your source code and the JVM. You do not need to read bytecode yourself; Java creates and uses it for you.
Try the complete cycle
Save this as Calculator.java, or run it in the live editor first.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
In the live editor, press Run. On your computer, open a terminal in the folder where the file is saved and do these two steps:
- Run
javac Calculator.java. Java checks the code and createsCalculator.classwhen it finds no error. - Run
java Calculator. The JVM starts themainmethod, which callsadd(12, 30)and prints the answer.
Use the file name with .java while compiling. Use the class name without .java while running.
What happens when the code has a mistake?
There are two common moments when Java can report a problem.
A compile-time error: Java cannot prepare the program
This program has a wrong type. A word cannot be stored in an int variable.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
When you compile it, Java stops before the program starts. Read the first error message, find the line it mentions, correct the code—for example, write int score = 10;—and compile again.
A runtime error: the program starts, then meets a bad situation
This program follows Java's type rules, so it can compile. But dividing by zero is not allowed when the program actually runs.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The difference is important: compile-time errors prevent the program from starting; runtime errors appear only after Java starts running a line that causes a problem.
What the JVM does after you run a class
The JVM handles the complicated work in the background. For now, remember these three jobs:
- It finds the
.classfile you asked Java to run. - It checks that the compiled code is safe enough to run.
- It starts
mainand runs the instructions inside it.
Terms such as *class loader*, *verifier*, and *JIT compiler* describe parts of that background work. You will meet them later; you do not need them to compile and run your first programs.
When Java cannot find your class
If Java says it cannot find or load a main class, first check these simple things:
- Are you in the folder that contains the compiled
.classfile? - Did you run
javac FileName.javasuccessfully before runningjava ClassName? - Does the class name have the correct spelling and capital letters?
- Did you save the file with the same name as the public class?
The word classpath is Java's name for the places where it searches for class files. For the small one-file examples in this course, working in the same folder as your .class file is enough.
Remember this
- Write code in a
.javafile. - Compile it with
javacto create a.classfile. - Run it with
javato start the JVM. - Fix compile errors before trying to run the program.
Continue the lesson path