Java Introduction: What It Is, How It Runs, Your First Program
ProgrammingLesson 1 of 65 min read
What you'll learn
- Explain what Java is and why it compiles to bytecode instead of machine code
- Tell the JDK, JRE and JVM apart and know which one you need to install
- Compile and run a Java program from the command line
- Read a basic Java program line by line, including comments and printing
On this page
Java is one of the most widely used programming languages for backend services, Android apps, enterprise software and coding interviews. Before you can write anything useful, though, you need a clear mental model of what happens between typing code and seeing it run. This lesson gives you that model and ends with a working program on your machine.
What is Java?
Java is a general-purpose, object-oriented, statically typed language. Statically typed means every variable has a type that is checked before the program runs, so a whole category of mistakes is caught at compile time instead of in production. Object-oriented means programs are organised around classes and objects, which you will meet in Java Classes and Objects.
Java's famous promise is "write once, run anywhere". When you compile a Java file, the compiler does not produce machine code for your specific processor. It produces bytecode, a compact instruction set stored in .class files. A program called the Java Virtual Machine (JVM) reads that bytecode and executes it. Because there is a JVM for Windows, macOS and Linux, the same .class file runs on all of them without recompiling.
JDK, JRE and JVM: what is the difference?
These three acronyms confuse almost every beginner. They are layers, each sitting inside the next one.
| Term | Stands for | What it does | Do you need it? |
|---|---|---|---|
| JVM | Java Virtual Machine | Loads and runs compiled bytecode on your operating system | Always, but it comes bundled |
| JRE | Java Runtime Environment | The JVM plus the standard class libraries needed to run Java programs | Only to run programs |
| JDK | Java Development Kit | The JRE plus developer tools such as the javac compiler | Yes, to write and compile code |
Installing Java and checking it works
Download a JDK (for example Eclipse Temurin or Oracle JDK) for your operating system, install it, then open a new terminal and check both tools. You need javac (the compiler) and java (the launcher) to be found.
javac -version
java -versionOutput
javac 17.0.9 java version "17.0.9" 2023-10-17 LTS
Your exact version numbers will differ, and that is fine as long as both commands print something. If the terminal says 'javac' is not recognized (Windows) or command not found (macOS/Linux), the JDK is either not installed or its bin folder is not on your PATH.
Your first Java program
Create a file called HelloWorld.java and type exactly the following. The file name must match the public class name, including capital letters.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Output
Hello, World!
Every part of that program has a job:
public class HelloWorlddeclares a class. In Java, all code lives inside a class.public static void main(String[] args)is the entry point. The JVM looks for a method with exactly this signature and starts there.staticmeans it can run without creating an object first, andvoidmeans it returns nothing.String[] argsholds any command-line arguments you pass in.System.out.println(...)prints text followed by a newline. Every statement ends with a semicolon.
Compile first, then run
Java is a two-step process. javac turns your source file into bytecode, and java hands that bytecode to the JVM. Notice that you compile with the file name (HelloWorld.java) but run with the class name (HelloWorld, no extension).
javac HelloWorld.java
java HelloWorldOutput
Hello, World!
After the first command you will find a new HelloWorld.class file next to your source. That is the bytecode. Since Java 11 you can also skip the visible compile step for single-file programs with java HelloWorld.java, which compiles in memory and runs immediately. It is handy for experiments, but understanding the two real steps helps you read compiler errors.
Printing, comments and escape characters
print keeps the cursor on the same line, println moves to the next one. Comments are ignored by the compiler and exist for humans. Escape sequences start with a backslash and let you print characters that would otherwise break the string. The last two lines show a classic gotcha: + works left to right, so "Total: " + 40 + 2 builds a string first and glues 2 on the end.
public class Welcome {
public static void main(String[] args) {
// A single-line comment: the compiler ignores it
System.out.print("Loading");
System.out.print("...");
System.out.println(" done");
/* A block comment
can span several lines */
System.out.println("Name:\tAsha");
System.out.println("Quote: \"Practice daily\"");
System.out.println("Line one\nLine two");
System.out.println("Total: " + 40 + 2);
System.out.println("Total: " + (40 + 2));
}
}Output
Loading... done Name: Asha Quote: "Practice daily" Line one Line two Total: 402 Total: 42
Passing input with command-line arguments
The args array in main receives whatever you type after the class name. This is the simplest way to make a program react to input before you learn about Scanner or files.
public class Greeter {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Hello, stranger! Pass your name as an argument.");
} else {
System.out.println("Hello, " + args[0] + "!");
}
}
}Output
$ java Greeter Hello, stranger! Pass your name as an argument. $ java Greeter Asha Hello, Asha!
Common mistakes
- File name does not match the public class. Saving
HelloWorldcode ashello.javagiveserror: class HelloWorld is public, should be declared in a file named HelloWorld.java. - Wrong capitalisation. Writing
system.out.printlnorstringinstead ofSystemandStringproduceserror: package system does not existorcannot find symbol. Java is case-sensitive. - Missing semicolon. The compiler reports
error: ';' expectedand points at the end of the previous line. - Running with the extension.
java HelloWorld.classfails withError: Could not find or load main class HelloWorld.class. Usejava HelloWorld. - Misspelling `main`. If the signature is not exactly
public static void main(String[] args), you getError: Main method not found in class HelloWorld.
How this shows up in interviews and real projects
Interviewers often ask the JDK/JRE/JVM question as a warm-up, and "why is Java platform independent?" is really the bytecode-and-JVM idea from this lesson. In real projects you rarely run javac by hand. Build tools such as Maven or Gradle and IDEs do it for you, but the same compile-then-run pipeline sits underneath, so understanding it makes build errors far less mysterious. When you are ready to apply what you learn, browse open roles to see what employers list under Java.
Try it yourself
Write a program called Profile that prints three lines: your name, your city and your favourite programming topic. Then modify it so the name comes from the first command-line argument and falls back to "Guest" when no argument is given. Compile and run it both ways.
Show a hint
Use args.length to check whether an argument exists, exactly like the Greeter example. Remember to compile again after every edit.
Key takeaways
- Java source compiles to bytecode, and the JVM runs that bytecode on any operating system.
- The JDK contains the compiler and tools, the JRE is the runtime, and the JVM executes the bytecode. Install the JDK.
- Compile with
javac File.java, run withjava ClassName(no extension). - Execution starts at
public static void main(String[] args), and the file name must match the public class name.
Test yourself
A trick from this lesson's ride: Sheet music, not sound: javac writes the score, every JVM is an orchestra that plays it. Four more tricks like it wait along the route, each followed by a question.
Frequently asked questions
What is the difference between JDK, JRE and JVM?
The JVM runs compiled Java bytecode. The JRE is the JVM plus the standard libraries needed to run programs, and the JDK is the JRE plus development tools such as the javac compiler. If you want to write Java, install the JDK.
Why is Java called platform independent?
Java source code is compiled into bytecode rather than machine code. Any device with a compatible JVM can run that same bytecode, so you compile once and run on Windows, macOS or Linux without changes. The JVM itself is platform specific, but your program is not.
Why must the file name match the class name in Java?
When a class is declared public, the compiler requires it to live in a file with exactly the same name plus .java. This rule lets the compiler and JVM find classes by name. A mismatch gives the error class X is public, should be declared in a file named X.java.