Swipe left or tap Next ▶ to turn the page.
Select any text to highlight it or add a note. Tap a diagram to enlarge it.
Java Basics: JVM, Types, Operators, Strings, Control Flow and Arrays
- ✔ Explain JDK, JRE, JVM and bytecode clearly in an interview
- ✔ Predict the output of expressions that mix types, casting and operators
- ✔ Use String, StringBuilder and the string pool correctly, including == versus equals()
- ✔ Write and trace loops, switch statements and array programs by hand
Why Java, and what happens when you run it
Almost every Java technical round opens with "What is the difference between JDK, JRE and JVM?", so this is the first answer you must say smoothly.
Java is a general-purpose, object-oriented language. Its big idea is "write once, run anywhere". You write source code once, the compiler turns it into bytecode, and that bytecode runs on any machine that has a Java Virtual Machine. Windows, Linux and macOS all have their own JVM, but the bytecode file is the same.
Think of it like a railway ticket printed in a standard format. Every station in India reads the same ticket, even though each station has its own staff and machines. Bytecode is the standard ticket; each JVM is a station that knows how to read it.
The three layers
| Name | Full form | What it contains |
|---|---|---|
| JVM | Java Virtual Machine | Runs bytecode: class loading, verification, interpreter, JIT compiler, garbage collector |
| JRE | Java Runtime Environment | JVM + the standard class libraries needed to run programs |
| JDK | Java Development Kit | JRE + development tools such as javac, java, jar, javadoc, jdb |
Riya only wants to run a ready-made
.jarbilling tool on her laptop.Running needs the JVM plus libraries, which is the JRE (many installs today simply ship a full JDK, which also works).
Answer: the JRE is enough; the JDK is needed only if she wants to compile code.
From source code to running program
Output questions and "what is bytecode?" questions both depend on knowing these steps in order.
When you write Hello.java and run it, four things happen:
- Compile.
javac Hello.javachecks syntax and types and producesHello.class, which holds bytecode. - Load.
java Hellostarts the JVM. The class loader findsHello.classand loads it into memory. - Verify. The bytecode verifier checks that the code is safe, for example that it does not jump to random memory.
- Execute. The interpreter runs bytecode instructions. Code that runs again and again ("hot" code) is compiled to native machine code by the JIT (Just-In-Time) compiler, which makes it fast.
flowchart LR A[Hello.java<br/>source] -->|javac| B[Hello.class<br/>bytecode] B -->|java Hello| C[Class loader] C --> D[Bytecode verifier] D --> E[Interpreter] E -->|hot code| F[JIT compiler<br/>native code] E --> G[Output] F --> G
Compile-time errors versus runtime errors
A compile-time error is caught by javac, so no .class file is made. Examples: a missing semicolon, assigning a String to an int, using an undeclared variable.
A runtime error happens while the program runs. The code compiled fine, but something went wrong: dividing an integer by zero, reading index 5 of a 3-element array, or calling a method on null. Java reports these as exceptions.
int x = "5";— a String cannot go into an int. The compiler sees the types.Compile-time error.
int[] a = new int[3]; a[3] = 1;— the types are fine; index 3 is out of range only when it runs.Runtime error: ArrayIndexOutOfBoundsException.
A loop in a railway reservation program runs 10 lakh times.
The interpreter would translate each bytecode instruction every time.
The JIT notices the hot loop and compiles it once to native code.
Answer: JIT improves speed of frequently run code; it does not change the program's output.
Which component turns frequently executed bytecode into native machine code?
javac makes bytecode from source; the JIT compiler converts hot bytecode into native code at runtime.
Anatomy of your first program
Interviewers often ask "Why is main public static void?" — each word has a reason.
public class Hello {
public static void main(String[] args) {
System.out.println("Namaste, Java!");
}
}
Namaste, Java!
Let us read it word by word.
| Part | Meaning |
|---|---|
public class Hello |
A class named Hello. A public class must be in a file named Hello.java. |
public |
The JVM must be able to call main from outside the class. |
static |
The JVM calls main without creating an object of Hello. |
void |
main returns nothing to the JVM. |
String[] args |
Command-line arguments, as an array of strings. |
System.out.println |
Prints the value and then moves to a new line. |
Rules worth knowing
- Java is case-sensitive.
Main,mainandMAINare three different names. - One file can have many classes, but at most one public class, and the file name must match it.
String[] args,String args[]andString... argsare all valid ways to write the parameter.printdoes not add a new line;printlndoes.
Code:
System.out.print("A"); System.out.println("B"); System.out.print("C");print("A")writes A and stays on the same line.println("B")writes B and then a new line.print("C")writes C on the next line.Output:
ABon line 1 andCon line 2.
Keep going with the full course
Day 1 has 22 pages. You have read the first 3.
See what's inside →