English 中文(简体)
Java generics and "..."
原标题:
  • 时间:2009-12-10 02:34:19
  •  标签:
  • java
  • java-5
  1. What do T and S mean?
  2. public void main(String... abc) ; what does ... mean? Is ... called generic as well?
最佳回答
  1. That are parameterized types. You can use any identifier here to represent a certain object type.
  2. That are varargs. You can pass a single String, or multiple Strings, or a String array in.
问题回答

T and S are generic classes. They can be any type of class that you want. For example, Map<K, V> uses the K for the key class and V for the value class.

Map<Integer, String> map = new HashMap<Integer, String>

As for the String..., it means any number of String parameters.

  1. Please read the documentation. Briefly, they are type parameters so that generic types and methods can know what type of objects they are acting on.
  2. That indicates that the method can accept a variable number of arguments. See varargs. It s basically sugar around an array.

Sun s Java Generics documentation can be a bit hard to understand at times, so I tried to write a simpler tutorial on Java Generics. You can find it here:

http://tutorials.jenkov.com/java-generics/index.html

complementing: String... is almost the same as String[].
On the method side it is the same,
on the calling side the re is a difference: the compiler creates the array from the parameters.

void method(String... args) {
    // args is an array: getClass() returns [java.lang.String
    if (args.length >  0) {
        System.out.println(args[0]);
...
    method();               // same as method(new String[0]);
    method("1", "2", "3");  // same as method(new String[] {"1", "2", "3"});

T and S means that the class itself does not know what classes they are, but things that use the class do.

Take java.util.List. The list class does not know anything about T and makes no assumptions about T. Things that use the List class:

List<MyBean> l = new ArrayList<MyBean>();

Know what s in it.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签