---
title: "5.24 消除未检查的警告"
author: "bingooh"
date: "2013-08-08T14:44:40Z"
tags: []
categories: ["编程语言"]
slug: "5-24"
draft: false
lastmod: "2013-08-08T14:44:40Z"
id: 1922033
url: "https://bingooh.iteye.com/blog/1922033"
---

以下代码在编译时将给出一个未检查的警告（unchecked warning）：

```
//警告信息提示ArrayList应使用泛型
List<String> list=new ArrayList();
```

 

任何一个未检查的警告在运行时都可能导致抛出ClassCastException，因此应该尽量消除警告信息，如上代码应修改为：

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

 

如果确定代码不会触发ClassCastException，那么可以使用@SuppressWarnings("unchecked")去除未检查警告。此注释应该用在尽可能小的范围