Welcome to Java
From Python to Java — your first compiled program
After this lesson, you will be able to:
- Explain the difference between compiled and interpreted languages
- Write, compile, and run a Java program using
javacandjava - Identify the required structure of a Java class with a
mainmethod - 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. |
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.javawon’t compile if the class isHelloWorld.
What does the javac command produce when you run javac HelloWorld.java?
Python vs. Java Side-by-Side
| Concept | Python | Java |
|---|---|---|
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 |
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:
- I — Inputs: What data do we need?
- P — Processing: What calculations or transformations?
- O — Outputs: What do we display?
- C — Constructs: What programming structures? (if, loop, method?)
- D — Data Types: What types? (int, double, String, boolean?)
- S — Steps: 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.