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.
In TypeScript, you're familiar with introduction.
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:
// 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:
// 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));You may be used to different syntax or behavior.
TypeScript compiles to JS; Java compiles to JVM bytecode
You may be used to different syntax or behavior.
Java requires a class wrapper — every file is a class
You may be used to different syntax or behavior.
Entry point is public static void main(String[] args)
You may be used to different syntax or behavior.
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.
function main() { console.log("hi"); }public class Main {
public static void main(String[] args) {
System.out.println("hi");
}
}2. Static Typing Is Mandatory
TypeScript types are erased at runtime. Java types are real and enforced by the JVM at every step.
const x: number = 42;int x = 42;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.
tsc main.ts && node main.jsjavac Main.java && java Main
// or with Maven: mvn exec:java4. System.out.println
Java's standard output is verbose but precise. Most IDEs and build tools shorten this with aliases or logging frameworks.
console.log("Hello");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)
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