What is Java?
Java is a general-purpose programming language created at Sun Microsystems in 1995 and now maintained by Oracle and the OpenJDK community. It is used for Android apps, banking and railway-booking back ends, government portals, big-data tools and desktop software.
Three ideas make Java popular:
- Write once, run anywhere. A compiled Java program runs on Windows, Linux and macOS without changes.
- Object-oriented. Programs are built from classes and objects, which keeps large projects organised.
- Safe and strict. Every variable has a fixed type, and the compiler catches many mistakes before the program runs.
JDK, JRE and JVM
| Part | Full name | What it does |
|---|---|---|
| JDK | Java Development Kit | Tools to write Java: the compiler javac, plus the JRE |
| JRE | Java Runtime Environment | Everything needed to run Java programs |
| JVM | Java Virtual Machine | The engine inside the JRE that runs bytecode |
As a student you install the JDK (version 21 is the current long-term release). It includes the other two.
How a Java program runs
- You write source code in a file such as
Hello.java. - The compiler turns it into bytecode:
javac Hello.javacreatesHello.class. - The JVM reads the bytecode and runs it:
java Hello.
Bytecode is not tied to one kind of computer. Each operating system has its own JVM, and every JVM understands the same bytecode. That is how "write once, run anywhere" works.
Your first program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Save it as Hello.java, then in a terminal:
javac Hello.java
java Hello
Hello, Java!
Every word explained
public class Hellodeclares a class namedHello. A public class must be saved in a file with the same name:Hello.java.public static void main(String[] args)is the entry point. The JVM starts running here.public: the JVM can call it from outside the class.static: it runs without creating an object first.void: it returns nothing.String[] args: any words typed after the program name on the command line.System.out.println(...)prints a line.System.out.print(...)prints without moving to a new line.- Every statement ends with a semicolon
;. Curly braces{ }group lines into a block.
Comments
// a single-line comment
/* a comment
over several lines */
/** a documentation comment, read by the javadoc tool */
Comments are ignored by the compiler. Use them to explain why the code does something.
print, println and escape sequences
public class Card {
public static void main(String[] args) {
System.out.print("BrainBit ");
System.out.println("Infotech");
System.out.println("Name:\tAsha\nCity:\tRanchi");
System.out.println("She said \"hi\"");
}
}
BrainBit Infotech
Name: Asha
City: Ranchi
She said "hi"
| Escape | Meaning |
|---|---|
\n | new line |
\t | tab |
\" | a double quote inside a string |
\\ | a backslash |
Java 21: running a single file directly
Since Java 11 you can skip the separate compile step for a one-file program:
java Hello.java
The JVM compiles it in memory and runs it. Real projects still use javac or a build tool such as Maven or Gradle.
Setting up
- Install a JDK (Oracle JDK, or a free OpenJDK build such as Eclipse Temurin).
- Check it in a terminal:
java -versionandjavac -version. - Use an editor: VS Code with the Java extension pack, IntelliJ IDEA Community or Eclipse.
Common mistakes
- Saving
public class Helloin a file namedhello.javaorMain.java. The names must match exactly, including capital letters. - Writing
system.out.println. Java is case-sensitive: it isSystemwith a capital S. - Forgetting the semicolon at the end of a statement.
- Running
java Hello.class. Give the class name only:java Hello. - Spelling
mainwrongly or changing its signature, so the JVM reports "main method not found".
Key points
- The JDK is for writing Java, the JRE for running it, and the JVM is the engine that runs bytecode.
javaccompiles.javato.classbytecode;javaruns it.- Execution starts at
public static void main(String[] args). - A public class lives in a file with the same name.
- Java is case-sensitive, and statements end with
;.
Practice questions
1. What is the difference between the JDK and the JRE?
The JRE contains what you need to run Java programs (the JVM and the standard library). The JDK contains the JRE plus development tools such as the compiler javac.
2. What file does javac Report.java create, and what is inside it?
Report.class, which holds bytecode: instructions for the JVM, not machine code for one processor.
3. Why is main declared static?
So the JVM can call it without first creating an object of the class.
4. Write a program that prints your name on one line and your city on the next, using one println and \n.
public class About {
public static void main(String[] args) {
System.out.println("Ravi\nRanchi");
}
}5. A student saves public class Marks in marks.java. What happens when they compile?
The compiler reports an error: class Marks is public and must be declared in a file named Marks.java. Rename the file with a capital M.