TS
JV

TypeScript to Java

10 lessons

Progress0%
1Introduction: Compiler to JVM2Type Systems: Structural vs Nominal3Classes & OOP4Generics5Modules to Packages6Null Safety7Async to Threads8Ecosystem9Exception Handling10Collections and Stream API
All Mirror Courses
TS
JV
Modules to Packages
MirrorLesson 5 of 10
Lesson 5

Modules to Packages

Modules to Packages

Introduction

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

Mirror Card
TS
From TypeScript:

In TypeScript, 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
// File: com/example/utils/MathUtils.java
package com.example.utils;

public class MathUtils {
    public static final double PI = 3.14159;

    public static int add(int a, int b) {
        return a + b;
    }
}

// File: com/example/Main.java
package com.example;

import com.example.utils.MathUtils;
import com.example.utils.*;  // wildcard import

public class Main {
    public static void main(String[] args) {
        System.out.println(MathUtils.add(2, 3));
    }
}

Comparing to TypeScript

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

TS
TypeScript (What you know)
// utils/math.ts
export function add(a: number, b: number): number {
  return a + b;
}
export const PI = 3.14159;

// main.ts
import { add, PI } from "./utils/math";
import type { User } from "./types";
Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

TypeScript uses file-based ES modules; Java uses package declarations

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

Java package names mirror the directory structure (com.example.utils)

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

No export keyword — public class/method is importable from outside the package

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

Static utility methods live in classes (no standalone functions)

Step-by-Step Breakdown

1. package Declaration

Every Java file starts with a package declaration that mirrors its folder path. com.example.utils → src/com/example/utils/.

TS
TypeScript
// TypeScript: folder structure is implicit
export function add() {}
JV
Java
package com.example.utils;

public class MathUtils {
    public static int add(int a, int b) { return a + b; }
}
Rule of Thumb
Package names are conventionally reverse domain: com.yourcompany.project.module.

2. import Statements

Java import uses fully qualified class names. import com.example.utils.MathUtils imports a single class. import com.example.utils.* imports all public classes in that package.

TS
TypeScript
import { add } from "./utils/math";
JV
Java
import com.example.utils.MathUtils;
// then use: MathUtils.add(2, 3)

3. No Standalone Functions — Use Static Methods

Java has no module-level functions. Utility functions are static methods in utility classes.

TS
TypeScript
export function clamp(n: number, min: number, max: number): number {}
JV
Java
public class NumberUtils {
    public static int clamp(int n, int min, int max) {
        return Math.max(min, Math.min(max, n));
    }
}

4. Build Tools: Maven and Gradle

npm manages packages in TypeScript. Java uses Maven (pom.xml) or Gradle (build.gradle) as both build tools and package managers, pulling from Maven Central.

TS
TypeScript
// package.json
{
  "dependencies": { "axios": "^1.0" }
}
JV
Java
<!-- pom.xml -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.14</version>
</dependency>

Common Mistakes

When coming from TypeScript, developers often make these mistakes:

  • TypeScript uses file-based ES modules; Java uses package declarations
  • Java package names mirror the directory structure (com.example.utils)
  • No export keyword — public class/method is importable from outside the package
Common Pitfall
Don't assume Java works exactly like TypeScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Java uses package declarations that mirror the directory structure
  • import uses fully qualified class names (com.example.utils.MyClass)
  • No standalone functions — use public static methods in classes
  • Maven (pom.xml) or Gradle is the Java equivalent of npm
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your TypeScript code in Java to practice these concepts.
PreviousNext