In Java, what does it mean when a type is followed by angle brackets (as in List)?

后端 未结 7 828
闹比i
闹比i 2020-12-21 02:34

I saw sometimes a type object inside <> beside of another object type declaration. For instance:

NavigableMap colorMap = new Tree         


        
7条回答
  •  难免孤独
    2020-12-21 03:05

    They're called Generics, and allow the compiler to do type checking of contents of lists etc, and also reduces the amount of casting you have to do in your code.

    It's also helpful when reading code, as you know what type of object can be put into the item in question, or what type to expect out of it.

    Java's implementation isn't as thorough as C++, as Java's is only available at compile time.

    At runtime, the type information is no longer available.

提交回复
热议问题