1

I'm a biginner in JSP and I'm confused about the difference between Enumeration and Enumeration<type> I'm learning with this Korean book and the example source in it says Enumeration with the eclipse neon version it doesn't work. It works only when it write Enumeration<String>. Can someone tell me the difference?

<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; 

charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>헤더 목록 출력</title>
</head>
<body>
<% 
    Enumeration<String> headerEnum = request.getHeaderNames();
    while(headerEnum.hasMoreElements()){
        String headerName = (String)headerEnum.nextElement();
        String headerValue = request.getHeader(headerName);

%>
<%=headerName %> = <%=headerValue %> <br>
<%
    }
%>

</body>
</html>
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Possible duplicate of [What does <> (angle brackets) mean in Java?](http://stackoverflow.com/questions/6607550/what-does-angle-brackets-mean-in-java). – Ole V.V. Jan 12 '17 at 09:54

1 Answers1

1

Just give it a look at the Enumeration documentation. Also review the generic types documentation.

By usingEnumeration you are using Enumeration<Object> as it is the default. What this <Object> does is just indicate the Enumeration class that in that particular instance, the type that it calls E (in the Enumeration Documentation) will be resolved to Object. By using <String> happens the same: the type called E will be resolved to String.

If you check the nextElement() signature it returns E. So, by using Enumeration or Enumeration<Object> that method will return Object and you will need the cast you did:

 String headerName = (String)headerEnum.nextElement();

By using Enumeration<String> the method will return an String, so you can directly do this:

 String headerName = headerEnum.nextElement();
Rubasace
  • 939
  • 8
  • 18