java-foundations Lesson 1 15 min read

Welcome to Java

From Python to Java — your first compiled program

Reading: Reges & Stepp: Ch. 1

After this lesson, you will be able to:

  • Explain the difference between compiled and interpreted languages
  • Write, compile, and run a Java program using javac and java
  • Identify the required structure of a Java class with a main method
  • Translate simple Python programs into equivalent Java code

Why Java?

In CSCD 110, you wrote Python programs — type a .py file, run it, see output. No extra steps. Python is an interpreted language: the interpreter reads your code line by line and executes it on the spot.

Java works differently. You write a .java file, then a compiler (javac) translates it into bytecode (a .class file). Then the Java Virtual Machine (JVM) runs that bytecode. Two steps instead of one — but in exchange, the compiler catches entire categories of bugs before your program ever runs.

Python:   source.py  →  interpreter  →  output
Java:     Source.java  →  javac  →  Source.class  →  java  →  output

From CSCD 110: You never had to worry about “compiling” in Python. In Java, compilation is a separate step that checks your code for type errors, missing semicolons, and undeclared variables. Think of it as a spell-checker that runs before your essay is published.


Your First Java Program

Every Java program lives inside a class, and execution starts in a special method called main:

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

That’s a lot of ceremony compared to Python’s print("Hello, World!"). Here’s what each piece does:

Java Purpose
public class HelloWorld Declares a class. The filename must match: HelloWorld.java
public static void main(String[] args) The entry point. JVM calls this method first.
System.out.println(...) Prints text followed by a newline (like Python’s print())
Every statement ends with ; Java requires semicolons. Python uses line breaks.
Check Your Understanding
Why does Java require a compilation step that Python doesn't?
A Java is slower than Python
B The compiler catches type errors and syntax problems before the program runs
C Java programs are too complex to interpret
D Python doesn't check for errors at all
Answer: B. Java's compiler performs static type checking — it verifies that every variable is used correctly based on its declared type. This catches bugs at compile time that Python would only catch at runtime.

Compiling and Running

Save your code as HelloWorld.java (filename must match class name), then:

javac HelloWorld.java    # Compile: creates HelloWorld.class
java HelloWorld          # Run: JVM executes the bytecode

If there’s an error — missing semicolon, wrong type, misspelled keyword — javac tells you before the program runs. This is one of Java’s biggest advantages.

Common Pitfall: The file name must exactly match the class name, including capitalization. helloworld.java won’t compile if the class is HelloWorld.

Check Your Understanding

What does the javac command produce when you run javac HelloWorld.java?


Python vs. Java Side-by-Side

Concept Python Java
Print print("hi") System.out.println("hi");
Comments # comment // comment
Entry point Top of file main method
Semicolons Not needed Required
File naming Any .py name Must match class name
Running python file.py javac file.java && java ClassName
Check Your Understanding
Which of the following is a valid Java program?
A print("Hello")
B class Hello { println("Hello"); }
C public class Hello { public static void main(String[] args) { System.out.println("Hello"); } }
D def main(): print("Hello")
Answer: C. Java requires a class declaration, a main method with the exact signature public static void main(String[] args), and System.out.println for output. The other options use Python syntax or incomplete Java.

The IPO-CDS Framework

Throughout this course, we use a structured approach to solve every problem:

  • IInputs: What data do we need?
  • PProcessing: What calculations or transformations?
  • OOutputs: What do we display?
  • CConstructs: What programming structures? (if, loop, method?)
  • DData Types: What types? (int, double, String, boolean?)
  • SSteps: What order do we write the code?

You’ll apply this framework starting in the next lesson when you begin working with variables and types.


Practice

Predict the Output

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

Fill in the Blanks

Complete the Java program so it prints "EWU":

 class School {
    public static void (String[] args) {
        System.out.("EWU");
    }
}

Arrange the Code

Put these lines in the correct order to make a valid Java program that prints "Go Eagles!":


Summary

Java is a compiled, statically-typed language. You write code in a .java file, compile with javac, and run with java. Every program needs a class and a main method. The compilation step is your safety net — it catches mistakes before your program ever executes.