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
Generics
MirrorLesson 7 of 10
Lesson 7

Generics

Type-safe containers and reusable algorithms

Introduction

In this lesson, you'll learn about generics 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 type-safe containers and reusable algorithms.

JV
In Java:

Java has its own approach to type-safe containers and reusable algorithms, 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
import java.util.*;

// Generic method
public static <T> T identity(T value) { return value; }
public static <T> T first(List<T> list) { return list.get(0); }

// Generic class
public class Box<T> {
    private T value;
    public Box(T value) { this.value = value; }
    public T get() { return value; }
}

// Bounded wildcards
public static double sum(List<? extends Number> list) {
    return list.stream().mapToDouble(Number::doubleValue).sum();
}

// Usage
List<Integer> nums = List.of(1, 2, 3);
List<String>  strs = List.of("a", "b");
System.out.println(first(nums)); // 1
System.out.println(first(strs)); // "a"
Box<String> box = new Box<>("hello");

Comparing to JavaScript

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

JS
JavaScript (What you know)
// JS has no generics — types are dynamic
function identity(value) { return value; }
function first(arr) { return arr[0]; }

const nums = [1, 2, 3];
const strs = ["a", "b"];
console.log(first(nums)); // 1
console.log(first(strs)); // "a"

// No compile-time safety
const mixed = [1, "two", true];
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java generics are compile-time constructs; JS has no equivalent

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

<T> declared on method/class; T can be bounded with 'extends'

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

? extends T (covariant wildcard) — read-only; ? super T — write

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Type erasure: generics are erased at runtime (no List<Integer>.class)

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Collections API (List, Map, Set) are generic by design

Step-by-Step Breakdown

1. Generic Methods

Declare <T> before return type. Java infers T from the argument — just like dynamic dispatch in JS, but checked at compile time.

JS
JavaScript
function wrap(val) { return { value: val }; }
JV
Java
public static <T> Map<String,T> wrap(T val) {
    return Map.of("value", val);
}

2. Generic Classes

A generic class like Box<T> works for any type T. The type is fixed when you instantiate it.

JS
JavaScript
class Box { constructor(v) { this.value = v; } }
JV
Java
class Box<T> {
    private T value;
    Box(T v) { this.value = v; }
    T get() { return value; }
}

3. Bounded Type Parameters

Use 'extends' to restrict which types are valid. <T extends Comparable<T>> lets you call compareTo() on T.

JS
JavaScript
function max(a, b) { return a > b ? a : b; }
JV
Java
public static <T extends Comparable<T>> T max(T a, T b) {
    return a.compareTo(b) > 0 ? a : b;
}
Rule of Thumb
Use ? extends T when reading from a collection; ? super T when writing.

4. Wildcards

Wildcards allow flexibility when the exact type doesn't matter — only the bounds do.

JS
JavaScript
function printAll(list) { list.forEach(x => console.log(x)); }
JV
Java
void printAll(List<?> list) {
    for (Object item : list) System.out.println(item);
}
Common Pitfall
You cannot add elements to a List<?> (wildcard list) — only null is safe to add.

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • Java generics are compile-time constructs; JS has no equivalent
  • <T> declared on method/class; T can be bounded with 'extends'
  • ? extends T (covariant wildcard) — read-only; ? super T — write
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

  • Declare <T> before return type for generic methods; after class name for generic classes
  • <T extends Foo> bounds the type; ? extends/super for wildcard flexibility
  • Type erasure means no List<Integer>.class at runtime — use raw List.class
  • Collections (List, Map, Set) are generic; always specify type parameters
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Java to practice these concepts.
PreviousNext