简体   繁体   中英

Java - Maximum number of type parameters

I recently read that type parameter names must be exactly one uppercase letter.

If this is the case, then wouldn't there be a maximum number of type parameters?

Would said maximum be equal to the number of uppercase letters that are also valid Java identifiers?

The convention is to use one letter for a type. However, a type parameter is bound only by the hard limits of an identifier , which is to say, it has to be a valid Java identifier. Any hard limits would only come from that.

There are valid cases in which it's acceptable to break the convention. Specifically, back in the days of Java 7, if you were doing some kind of transformation which required two disjoint types, you could specify it as thus:

public <DatabaseDataFmt extends DatabaseDTO,
        ProjectionFmt extends ProjectionDTO,
        Result>
  Result transform(DatabaseDataFmt databaseFormat,
                   ProjectionFmt projectionFormat) {}

Ultimately it depends on what you want to accomplish with your identifiers. Convention helps , but if it doesn't help , then there's no reason to be bound by it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM