---
title: "Java Generic Types"
author: "ymanyang"
date: "2012-11-16T18:10:12Z"
tags: []
categories: ["编程语言"]
slug: "java-generic-types"
draft: false
lastmod: "2012-11-16T18:10:12Z"
id: 1728171
url: "https://buckyang.iteye.com/blog/1728171"
---

A *generic type* is a generic class or interface that is parameterized over types. It can be used to generalize the type of parameters and return value in coding without the risk to pass a wrong type to its methods. Like the following example:

```
/**
 * Generic version of the Box class.
 * @param <T> the type of the value being boxed
 */
public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}
```

 When initiate this class or use it, it only need to replace the type parameter with desired class type.