java-generics

Explain and demonstrate Java generics with type parameters, wildcards, and type bounds.

Updated Mar 3, 2026
One-click install
npx skills add https://github.com/abudhahir/projects-pkm --skill java-generics
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: java-generics
Source: https://github.com/abudhahir/projects-pkm/tree/main/SKILLS/java-generics
Command: npx skills add https://github.com/abudhahir/projects-pkm --skill java-generics

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps developers write type-safe, reusable Java code by leveraging generics, eliminating common runtime errors and improving code maintainability.

Core Features & Use Cases

  • Type Safety: Ensures compile-time checks for type correctness, preventing runtime ClassCastException errors.
  • Code Reusability: Enables the creation of classes, methods, and interfaces that can operate on different types without code duplication.
  • Use Case: When building a generic data structure like a List or Map, generics ensure that only compatible types can be added, maintaining data integrity.

Quick Start

Use the java-generics skill to create a generic Box class that can hold any type of content.

Frequently Asked Questions about java-generics

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write type-safe Java code to prevent ClassCastException at runtime?

Type-safe Java code uses generics to enforce compile-time type checks, preventing runtime ClassCastException errors. By applying type parameters to classes and methods, you ensure only compatible types are added, maintaining data integrity without explicit casting.

What is the PECS principle and how do wildcards work in Java generics?

The PECS principle dictates using <? extends T> for reading producers and <? super T> for writing consumers. Java generics wildcards provide flexible type matching while maintaining type safety, allowing methods to operate on unknown parameter types without breaking compilation.

How do I create a generic class or method in Java for reusable code?

To create reusable Java code, define generic classes or methods by declaring type parameters like <T> before the return type. This allows the structure to operate on different types without duplication, such as building a generic Box class that holds any content type.

What are type bounds in Java generics and when should I use bounded types?

Type bounds in Java generics restrict a type parameter to a specific subtype or supertype using bounded types. You should use bounded types when you need to restrict acceptable parameter types to ensure specific methods or hierarchies are accessible within generic code.

Why does type erasure cause issues in Java generics and what are common pitfalls?

Type erasure removes generic type information at runtime, causing issues like inability to instantiate generic types or use instanceof with parameterized types. Common pitfalls include unchecked casts and array creation; following best practices and generic builders helps avoid these limitations.