Tutorial 1: From Source Code to a Running Java Program

Unit 1 · Java setup and first programs

Objectives

The Java pipeline

A .java file contains source code. The compiler turns it into platform-independent bytecode in a .class file. The Java Virtual Machine (JVM) executes that bytecode. The JDK includes the compiler and development tools; the JRE conceptually describes the runtime needed to execute programs.

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

Save this as Hello.java. The public class name and file name must match. Run javac Hello.java, then java Hello. Do not include .class in the run command.

Practice

  1. Print your name, program, and one learning goal on separate lines.
  2. Change the class name and observe the compiler error before renaming the file.
  3. Compile with warnings enabled using javac -Xlint:all Hello.java.

Self-check

  1. Why does Java compile to bytecode?
  2. What does static mean in main?
  3. Predict the output of two consecutive println calls.

Mastery task: Create a CourseCard program that prints the course code, title, prerequisite, and three habits for successful study.

Self-Check Quiz

1. Which tool converts Java source into bytecode?

Answer(B) javac compiles source code into bytecode.

2. What must match the public class name?

Answer(C) A public class named Hello belongs in Hello.java.

3. What does the JVM execute?

Answer(B) The JVM executes platform-independent bytecode.

Exercises

Exercise 1: Explain the Pipeline

Draw or describe the path from Hello.java to program output, naming the JDK compiler and JVM.

Sample answerThe programmer writes source in Hello.java. javac Hello.java creates Hello.class bytecode. The JVM starts with java Hello, loads the bytecode, and executes main.

Exercise 2: First Modification

Modify the example so it prints a three-line study plan. Compile and run it.

Sample answer
public class StudyPlan {
    public static void main(String[] args) {
        System.out.println("Read the lesson");
        System.out.println("Write one program");
        System.out.println("Review the errors");
    }
}
The file must be named StudyPlan.java.

Homework

  1. Install or verify a JDK and record the output of java --version and javac --version.
  2. Create a CourseCard program that prints the course code, title, prerequisite, and three study habits.
  3. In 150 words, explain why Java bytecode supports portability.
Sample homework answer

A strong submission shows matching class and file names, compiles without errors, and runs with java CourseCard. The explanation should state that the compiler produces bytecode for the JVM rather than machine code for one operating system; a compatible JVM can execute that bytecode on different platforms.