What does the in java mean?

后端 未结 4 1980
野的像风
野的像风 2020-12-11 04:39

I have seen declarations, interfaces and classes that go TYPE

What does this do/mean?

相关标签:
4条回答
  • 2020-12-11 05:05

    Without evidence, I believe you're talking about Java's Generics support...

    Generics allow you to abstract over types

    Before Java 5 it was difficult to provide classes that were capable of supporting multiple different types of Objects without having to code for each specific situation, so it was common for people to pass Object instead.

    This leads to many difficult choices to make at runtime, you'd have to do a runtime check to see if it was possible to cast a given Object to a usable type...for example

    List myIntList = new LinkedList(); // 1
    myIntList.add(new Integer(0)); // 2
    Integer x = (Integer) myIntList.iterator().next(); // 3    
    

    Now, this is reasonably obvious, but if you were passed just a List, you'd have to check each and every element in the list for correctness...

    But now, we can do this...

    List<Integer> myIntList = new LinkedList<Integer>(); // 1'
    myIntList.add(new Integer(0)); // 2'
    Integer x = myIntList.iterator().next(); // 3'
    

    This is a contract that basically says "This list only contains Integer type's of objects".

    With generics you can construct a single class that is capable of handling multiple different data types or a family of data types (ie constraint the parameter so that it must be extended from a particular parent type).

    Iterator<? extends Number> itNum;
    

    Basically says, this will contain objects that inherit from Number, include Integer, Long, Double, Float...

    Often in method and class decelerations you will see something similar to...

    public class MyGenericClass<T> {...}
    

    or

    public class MyGenericClass<T extends MyBaseObject> {...}
    

    This allows you to refer to T as if it were a concrete object type, for example...

    public class MyGenericClass<T extends MyBaseObject> {
        private T value;
        public MyGenericClass(T value) {
            this.value = value;
        }
    }
    

    This allows the compiler (and JVM) to essentially "replace" the marker T with a concert type (okay, it's a little more complicated then that, but that's the magic)...

    This allows to do things like...

    ... new MyGenericClass<MySuperObject>(new MySuperObject());
    ... new MyGenericClass<MySuperSuperObject>(new MySuperSuperObject());
    

    And know that it will only ever accept the type of object I specify...

    Have a read through the link in the first paragraph, I'm sure it can do more justice then I can ;)

    0 讨论(0)
  • 2020-12-11 05:10

    that is generic types check it here. Simple examples would be

    List<String>
    
    Map<Integer, String>
    
    0 讨论(0)
  • 2020-12-11 05:13
    public class Grid<E> {
    

    That's how you define a generic class in Java.Grid is the class and E is a formal type parameter.

    If you are really interested in learning about it, you will find a very good reference here - Java Generics FAQs - Frequently Asked Questions

    0 讨论(0)
  • 2020-12-11 05:20

    It's unclear what you are asking without looking at what exactly you are seeing. But it's likely you are seeing Generics in Java. Learn more about it here

    The idea is basically to make stronger type-safety in Java. So, a declaration like List<Integer> intList means intList has Integers in it. And if you try to put a, say, String -- it will throw compilation error.

    0 讨论(0)
提交回复
热议问题