← Java Developer
Day 1 · Java Basics: JVM, Types, Operators, Strings, Control Flow and Arrays
Java Developer · Crash Course
Day 1

Java Basics: JVM, Types, Operators, Strings, Control Flow and Arrays

⏱ About 90 minutes · 22 pages
By the end of today you will be able to
  • ✔ 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
  1. Why Java, and what happens when you run it
  2. From source code to running program
  3. Anatomy of your first program

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
JDK = JRE + tools (javac, jar, javadoc, jdb) JRE = JVM + class libraries (java.lang, java.util…) JVM Class loader · Bytecode verifier Interpreter · JIT compiler · GC
✍️ Example 1 — Which tool do you need?
  1. Riya only wants to run a ready-made .jar billing tool on her laptop.

  2. Running needs the JVM plus libraries, which is the JRE (many installs today simply ship a full JDK, which also works).

  3. Answer: the JRE is enough; the JDK is needed only if she wants to compile code.

Java Developer · Day 12 / 5

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:

  1. Compile. javac Hello.java checks syntax and types and produces Hello.class, which holds bytecode.
  2. Load. java Hello starts the JVM. The class loader finds Hello.class and loads it into memory.
  3. Verify. The bytecode verifier checks that the code is safe, for example that it does not jump to random memory.
  4. 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.

✍️ Example 2 — Classify the error
  1. int x = "5"; — a String cannot go into an int. The compiler sees the types.

  2. Compile-time error.

  3. int[] a = new int[3]; a[3] = 1; — the types are fine; index 3 is out of range only when it runs.

  4. Runtime error: ArrayIndexOutOfBoundsException.

✍️ Example 3 — What does JIT improve?
  1. A loop in a railway reservation program runs 10 lakh times.

  2. The interpreter would translate each bytecode instruction every time.

  3. The JIT notices the hot loop and compiles it once to native code.

  4. Answer: JIT improves speed of frequently run code; it does not change the program's output.

🙋 Try it now

Which component turns frequently executed bytecode into native machine code?

Java Developer · Day 13 / 5

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, main and MAIN are 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[] and String... args are all valid ways to write the parameter.
  • print does not add a new line; println does.
✍️ Example 4 — print versus println
  1. Code: System.out.print("A"); System.out.println("B"); System.out.print("C");

  2. print("A") writes A and stays on the same line.

  3. println("B") writes B and then a new line.

  4. print("C") writes C on the next line.

  5. Output: AB on line 1 and C on line 2.

Java Developer · Day 14 / 5
Free preview ends here

Keep going with the full course

Day 1 has 22 pages. You have read the first 3.

See what's inside →