O'Reilly logo

Java, A Beginner’s Guide, 5th Edition by Herbert Schildt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Type Inference with the Diamond Operator

Beginning with JDK 7, it is possible to shorten the syntax used to create an instance of a generic type. To begin, think back to the TwoGen class shown earlier in this chapter. A portion is shown here for convenience. Notice that it uses two generic types.

Image

For versions of Java prior to JDK 7, to create an instance of TwoGen, you must use a statement similar to the following:

Image

Here, the type arguments (which are Integer and String) are specified twice: first, when tgOb is declared, and second, when a TwoGen ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required