Type Placeholder
/**
* Generics in Java
*
* A generic type is a class or method that can operate on
* different data types while still providing TYPE SAFETY.
*
* Without generics, everything is threated as Object.
* This causes problems:
* You can store anything (String, Integer, etc).
* You must CAST whne retriving.
*
* With generics (like MyClass<T>), Java enforces the type at
* COMPILE TIME.
*
* Generic were introduces in Java 5 to eliminate those
* type-safe problems, especially when using collections.
*
* What is T?
*
* T is a type placeholder.
* When you write Box<String> T becomes String.
*
* Non generic classes (A) is legal, but unsafe.
* Java allowes it because generic where added later.
*/
package com.minte9.collections.generics;
public class Type {
public static void main(String[] args) {
// --- Non-generic class (unsafe) ----
NonGenericClass A = new NonGenericClass();
A.set(10);
System.out.println(A.get()); // 10
A.set("John"); // allowed (but unsage)
System.out.println(A.get()); // John
// This compile but is dangerous:
//Integer x = (Integer) A.get(); // runtime crash
// --- Generic class (safe) ---
MyClass<Integer> B = new MyClass<>();
B.set(10);
System.out.println(B.get()); // 10
// B.set("John"); // ❌ compile-time error
// This is the whole point of generics.
}
}
class NonGenericClass {
private Object obj;
public void set(Object o) {
obj = o;
}
public Object get() {
return obj;
}
}
// Generic class
class MyClass<T> {
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return t;
}
}
Multiple Types
/**
* A generic class can have more than one type parameter.
* Example: Box<T, U>, Map<K, V>, Entry<K, V>, etc.
*
* Each type parameter must have a UNIQUE NAME.
*/
package com.minte9.collections.generics;
public class MultipleTypes {
public static void main(String[] args) {
// Box<T, U> can accept two different types
Box<Integer, Integer> box = new Box<>();
box.set(10);
System.out.println(box.get()); // 10
}
// Generic class with one type parameters: T
static class Box<T, U> {
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return t;
}
}
// static class Word<T, T> {} // ❌ Error: Duplicate type parameter T
}
Generics Limitations
/**
* Generic type cannot be used in STATIC context.
*
* Example:
* static T value; // ❌ compiler error
*
* Static fields belong to the CLASS, not to individual objects.
* Generic type (T) only exists per OBJECT INSTANCE.
*
* Generics cannot use PRIMITIVES.
*
* Example:
* Box<int> box; // ❌ not allowed
*
* Java uses TYPE ERASURE.
* After compilation, generics become Objects.
* Primitives (int, double, etc.) are not objects.
*
* Use their wrapper classes insteed:
* Box<Integer> box = new Box<>();
*/
package com.minte9.collections.generics;
public class GenericsLimitations {
public static void main(String[] args) {
Box<Integer> b1 = new Box<>();
Box<String> b2 = new Box<>();
System.out.println(b1.hashCode()); // 1878246837
System.out.println(b2.hashCode()); // 929338653
// Box<int> box = new Box<int>(); // ❌ primitives not allowed
}
static class Box<T> {
//static T t; // ❌ ERROR: static reference to non-static type T
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return t;
}
}
}
Questions and answers:
Clink on Option to Answer
1. What is the main purpose of generics in Java?
- a) To provide type safety at compile time
- b) To make the program run faster
2. What is T in MyClass
- a) A type placeholder
- b) A fixed data type
3. What is the main danger of non-generic classes?
- a) They require a constructor
- b) They allow unsafe casting at runtime
4. Why is this invalid? static T value;
- a) Static members belong to the class, not the generic type
- b) Generics do not support variables
5. After compilation, Java generics become:
- a) Objects (due to type erasure)
- b) Primitive types