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
Modules to Packages
MirrorLesson 9 of 11
Lesson 9

Modules to Packages

Modules to Packages

Introduction

In this lesson, you'll learn about modules to packages 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 modules to packages.

JV
In Java:

Java has its own approach to modules to packages, 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
// Package declaration (must match directory structure)
package com.example.myapp;

// Standard library imports
import java.lang.Math;       // auto-imported — not needed
import java.nio.file.Path;
import java.nio.file.Files;

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.sqrt(16));
        System.out.println(Files.exists(Path.of("file.txt")));
        // No __name__ check — main() IS the entry point
    }
}

// Third-party: declare in pom.xml (Maven) or build.gradle
// <dependency>
//   <groupId>com.squareup.okhttp3</groupId>
//   <artifactId>okhttp</artifactId>
// </dependency>

Comparing to Python

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

PY
Python (What you know)
# Using standard library modules
import math
from os import path
from pathlib import Path

print(math.sqrt(16))
print(path.exists("file.txt"))

# Entry point guard
if __name__ == "__main__":
    print("Running directly")

# Third-party (pip install requests)
# import requests
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python modules are single files; Java packages are directories matching a com.company.app hierarchy

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Java package com.example is declared at the top of every file in that package

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Java has no __name__ guard — main() is always the entry point

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

java.lang (Math, String, System) is auto-imported; everything else needs an explicit import

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

pip install → Maven (pom.xml) or Gradle (build.gradle) dependency declarations

Step-by-Step Breakdown

1. Package Declaration

Every Java file in a package starts with a package statement. The package name must match the directory path.

PY
Python
# Python: the file location IS its module path
# myapp/utils.py → from myapp.utils import ...
JV
Java
// src/main/java/com/example/utils/Helper.java
package com.example.utils;

public class Helper { }

2. Importing Classes

Java imports are per class, not per file. import java.util.* imports all classes in a package.

PY
Python
from os import path
import json
JV
Java
import java.nio.file.Path;
import java.util.*;  // all of java.util

3. No __name__ in Java

Java has no concept of __name__. The JVM always starts at the class specified on the command line and calls its main() method.

PY
Python
if __name__ == "__main__":
    main()
JV
Java
// Just define main() — it is always the entry point
public static void main(String[] args) {
    // ...
}

4. Dependency Management

pip install is replaced by declaring dependencies in pom.xml (Maven) or build.gradle (Gradle). They download from Maven Central.

PY
Python
# requirements.txt
requests==2.31.0

# pip install -r requirements.txt
JV
Java
<!-- pom.xml -->
<dependency>
  <groupId>com.squareup.okhttp3</groupId>
  <artifactId>okhttp</artifactId>
  <version>4.12.0</version>
</dependency>
Rule of Thumb
Maven Central (search.maven.org) is Java's equivalent of PyPI.

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Python modules are single files; Java packages are directories matching a com.company.app hierarchy
  • Java package com.example is declared at the top of every file in that package
  • Java has no __name__ guard — main() is always the entry point
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

  • Python files = modules; Java directories = packages
  • package com.example declared at top of every file
  • pip → Maven/Gradle; PyPI → Maven Central
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in Java to practice these concepts.
PreviousNext