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
Introduction: Compiler to JVM
MirrorLesson 1 of 10
Lesson 1

Introduction: Compiler to JVM

Introduction

Introduction

In this lesson, you'll learn about introduction: compiler to jvm 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 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 — compiles to bytecode, runs on the JVM
public class Main {
    public static void main(String[] args) {
        String message = "Hello, World!";
        System.out.println(message);
        System.out.println(add(2, 3));
    }

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

Comparing to TypeScript

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

TS
TypeScript (What you know)
// TypeScript — transpiles to JavaScript, runs on Node.js or browser
const message: string = "Hello, World!";
console.log(message);

function add(a: number, b: number): number {
  return a + b;
}

console.log(add(2, 3));
Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

TypeScript compiles to JS; Java compiles to JVM bytecode

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

Java requires a class wrapper — every file is a class

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

Entry point is public static void main(String[] args)

Mirror Card
TS
From TypeScript:

You may be used to different syntax or behavior.

JV
In Java:

System.out.println replaces console.log

Step-by-Step Breakdown

1. Everything Lives in a Class

Java has no top-level functions. Every method must belong to a class. The JVM looks for main() as the program entry point.

TS
TypeScript
function main() { console.log("hi"); }
JV
Java
public class Main {
    public static void main(String[] args) {
        System.out.println("hi");
    }
}
Common Pitfall
The class name must match the file name exactly. Main.java must contain public class Main.

2. Static Typing Is Mandatory

TypeScript types are erased at runtime. Java types are real and enforced by the JVM at every step.

TS
TypeScript
const x: number = 42;
JV
Java
int x = 42;
Rule of Thumb
Java primitives: int, long, double, boolean. Reference types: String, Integer, List<T>.

3. Compile then Run

TypeScript uses tsc then node. Java uses javac to produce .class files then java to run them. Build tools (Maven/Gradle) automate this.

TS
TypeScript
tsc main.ts && node main.js
JV
Java
javac Main.java && java Main
// or with Maven: mvn exec:java

4. System.out.println

Java's standard output is verbose but precise. Most IDEs and build tools shorten this with aliases or logging frameworks.

TS
TypeScript
console.log("Hello");
JV
Java
System.out.println("Hello");
// or with a logger:
logger.info("Hello");

Common Mistakes

When coming from TypeScript, developers often make these mistakes:

  • TypeScript compiles to JS; Java compiles to JVM bytecode
  • Java requires a class wrapper — every file is a class
  • Entry point is public static void main(String[] args)
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 compiles to JVM bytecode — runs anywhere the JVM is installed
  • Every function must be a method inside a class
  • Entry point is public static void main(String[] args)
  • Types are mandatory and enforced at runtime by the JVM
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your TypeScript code in Java to practice these concepts.
OverviewNext