惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
量子位
Blog — PlanetScale
Blog — PlanetScale
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
D
Docker
罗磊的独立博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
IT之家
IT之家
MyScale Blog
MyScale Blog
Microsoft Azure Blog
Microsoft Azure Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Generics in Java Interview Q&A
Tapas Pal · 2026-05-13 · via DEV Community

Tapas Pal

Java Generics is one of the MOST important Java concepts.
If you understand Generics properly:
collections become safer
code becomes reusable
compile-time bugs reduce dramatically

A generic type is a generic class or interface that is parameterized over types.

What Are Generics?
Generics allow classes, interfaces, and methods to work with:
different data types safely
without rewriting code.
The Diamond
In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond. For example, you can create an instance of Box with the following
statement:

Box<Integer> integerBox = new Box<>();

Enter fullscreen mode Exit fullscreen mode

Generic Class

class Box<T> {
    T value;
    void set(T value) {
        this.value = value;
    }
    T get() {
        return value;
    }
}

Enter fullscreen mode Exit fullscreen mode

Usage

Box<String> box =
        new Box<>();
box.set("Hello");
String s = box.get();

Enter fullscreen mode Exit fullscreen mode

What Does Mean?
T means:Type parameter
Placeholder for actual type.
Common Generic Type Names
Symbol - Meaning
T - Type
E - Element
K - Key
V - Value
N - Number

Same class reused.

Box<Integer> intBox = new Box<>();
Box<String> strBox = new Box<>();

Enter fullscreen mode Exit fullscreen mode

DISADVANTAGES OF GENERICS
Now let us discuss the IMPORTANT limitations.

1. Type Erasure
MOST IMPORTANT limitation.
Java removes generic type information at runtime.
Example

List<String> list = new ArrayList<>();

Enter fullscreen mode Exit fullscreen mode

At runtime becomes: List
Generic type removed. Why?
Backward compatibility with old Java.
Consequences of Type Erasure
Cannot Do This

if(obj instanceof List<String>)

Enter fullscreen mode Exit fullscreen mode

Compile-time error.

Cannot Create Generic Array

T[] arr = new T[10];

Enter fullscreen mode Exit fullscreen mode

Not allowed. Why?
Because runtime type unknown.

2. Primitive Types Not Allowed
Cannot use: List<int>
Only objects allowed.
Correct List<Integer>
Uses wrapper classes.
Problem
Wrapper classes add:
memory overhead
boxing/unboxing cost
Example

List<Integer> list = new ArrayList<>();
list.add(10);

Enter fullscreen mode Exit fullscreen mode

Actually becomes:
Integer.valueOf(10)
Auto-boxing.

3. Cannot Create Static Generic Fields
Invalid

class Test<T> {
    static T value;
}

Enter fullscreen mode Exit fullscreen mode

Why?
A static field belongs to the class,
but a generic type belongs to the object instance/type parameter.

These two concepts conflict.
First Understand Static Variables
Static variables are:shared across ALL objects
Only ONE copy exists.