PY
JV

Python to Java

11 lessons

Progress0%
1Introduction2Variables & Types3Functions to Methods4Lists to Arrays5Dicts to Maps6Classes & OOP7Inheritance8Exception Handling9Modules to Packages10Ecosystem11Modern Java Features
All Mirror Courses
PY
JV
Introduction
MirrorLesson 1 of 11
Lesson 1

Introduction

Introduction

Introduction

In this lesson, you'll learn about introduction in Java. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
PY
From Python:

In Python, you're familiar with introduction.

JV
In Java:

Java has its own approach to introduction, which we'll explore step by step.

The Java Way

Let's see how Java handles this concept. Here's a typical example:

JV
Java Example
// Java: every program lives inside a class
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Comparing to Python

Here's how you might have written similar code in Python:

PY
Python (What you know)
# Python: run directly, no boilerplate
print("Hello, World!")

# Entry point convention
if __name__ == "__main__":
    print("Running as main script")
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python uses indentation for blocks; Java uses braces and semicolons

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Java requires a main() method as the program entry point

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

The Java file name must match the public class name exactly

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Java is compiled to bytecode; Python is interpreted line by line

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Java has no interactive REPL used in production

Step-by-Step Breakdown

1. Class Wrapper Required

Every Java program must be inside a class. Python code can live at the top level of a file.

PY
Python
print("Hello")
JV
Java
public class Main {
    // all code lives here
}

2. The main Method

Java's entry point is always public static void main(String[] args). Python uses the if __name__ == '__main__' convention.

PY
Python
if __name__ == "__main__":
    print("Hello")
JV
Java
public static void main(String[] args) {
    System.out.println("Hello");
}

3. Printing Output

Python's print() becomes System.out.println() in Java.

PY
Python
print("Hello, World!")
JV
Java
System.out.println("Hello, World!");
Common Pitfall
System.out.print() does NOT add a newline; System.out.println() does — like Python's print().

4. Compilation vs Interpretation

Python runs directly with 'python main.py'. Java must first be compiled with 'javac Main.java', then run with 'java Main'.

PY
Python
# python main.py
JV
Java
// javac Main.java
// java Main
Rule of Thumb
Compilation catches type errors before runtime — this is one of Java's biggest safety benefits.

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Python uses indentation for blocks; Java uses braces and semicolons
  • Java requires a main() method as the program entry point
  • The Java file name must match the public class name exactly
Common Pitfall
Don't assume Java works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Every Java file needs a public class matching the filename
  • main() is mandatory; Python uses if __name__ == '__main__'
  • Compile with javac, run with java
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in Java to practice these concepts.
OverviewNext