JS
JV

JavaScript to Java

10 lessons

Progress0%
1Variables & Types2Functions & Methods3Arrays & Collections4Classes & OOP5Exception Handling6Async vs Threads7Generics8String Methods9Interfaces and Abstract Classes10Build Tools and Ecosystem
All Mirror Courses
JS
JV
Variables & Types
MirrorLesson 1 of 10
Lesson 1

Variables & Types

Static typing vs dynamic typing

Introduction

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

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with static typing vs dynamic typing.

JV
In Java:

Java has its own approach to static typing vs dynamic typing, 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
String name = "Alice";
int age = 30;
double score = 9.5;
boolean active = true;

// Types cannot change after declaration
int value = 42;
// value = "string"; // compile error!

final int MAX = 100; // like JS const

// var (Java 10+) — infers type
var city = "Istanbul"; // inferred as String

Comparing to JavaScript

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

JS
JavaScript (What you know)
let name = "Alice";
let age = 30;
let score = 9.5;
let active = true;

// Types can change
let value = 42;
value = "now a string"; // valid in JS

const MAX = 100;
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java is statically typed — types are fixed at compile time

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

JS let/const → Java type declarations (String, int, etc.)

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

JS variables can change type; Java variables cannot

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java final = JS const; Java var = type inference (Java 10+)

Step-by-Step Breakdown

1. Explicit Types

Java requires you to declare the type of every variable. The compiler catches type mismatches before the program runs.

JS
JavaScript
let age = 30;
JV
Java
int age = 30;

2. Primitive vs Object Types

Java has primitive types (int, double, boolean) and object wrappers (Integer, Double, Boolean). Primitives are more efficient.

JV
Java
int x = 5;          // primitive
Integer y = 5;      // object wrapper (nullable)
double d = 3.14;
String s = "hello"; // always an object

3. final and var

Java's final prevents reassignment like JS const. Java 10+ var infers the type from the right side.

JS
JavaScript
const MAX = 100;
let city = "Istanbul";
JV
Java
final int MAX = 100;
var city = "Istanbul"; // String inferred

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • Java is statically typed — types are fixed at compile time
  • JS let/const → Java type declarations (String, int, etc.)
  • JS variables can change type; Java variables cannot
Common Pitfall
Don't assume Java works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Java types are fixed at declaration; JS types are dynamic
  • Java final = JS const
  • Java primitives: int, double, boolean, char
  • var in Java 10+ infers types like JS let
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Java to practice these concepts.
OverviewNext