Generic Java basic knowledge and operation-related interview questions

. Why use a generic (following problems)?
1): store any type of data in the collection, but of type Object are taken out, at this time have a strong rotation.
2): Constraint set of stored elements It must be the same data type (the same type of data can be compared, such as TreeSet class).
3): a design point (point) class to encapsulate the coordinate position, the coordinate position required to support the String type .Integer / Double type.

Second, what is generics?
Generics (GenericType), from Java5 began to support the new syntax:
1): extensive generic type.
2): Code template type of uncertainty, who call the sections of the code, specify the type who is what.

Three generics:
Here Insert Picture Description
. Generic classes: directly in the generic class / interface defined
using generic:
the same type before and after guaranteed.
List List new new = the ArrayList (); // set only store the String List type the element
because the same type of front and back, so from the beginning Java7 exit generic diamond syntax <>.
List List = new new ArrayList <> ();
generic inheritance relationship does not exist (error below).
List List = new new ArrayList () ; // error
Since then, the use of a collection had to use generics to constrain the type of the elements in the collection.
decompile, found: generic actually syntactic sugar , the bottom is still not generic, but still with a strong turn.
Here Insert Picture Description
four definitions and use generic methods:
generic method: in the generic method declarations.
in general, as the custom generic return type of the method makes sense, but in this case must be a generic parameter setting incoming in.
If no generic set of parameters to the particular type, the method at this time is generally designed to return to Object.
example asList () method of class Arrays tools
Here Insert Picture Description
V. generic wildcards and understanding only upper and lower limits on the line :( )
generics wildcards: I do not know what type When received, this time may be used to indicate unknown wildcard??.
In this case only receives the data, the data can not be stored into the set.
Here Insert Picture Description
Generics upper and lower limits: Type elements must be used to define the class of X or same subclass, X or the same parent.

VI and erase generic conversion:
generic Erase:
1): After compiling generic disappeared (generic auto-erase);
2): When the set with generic assigned without generic set, the generic case is erased (erase manually).
heap corruption:
single generic method used when both the use of variable parameters, then easily lead to contamination problems stack.
as: Arrays class in asList methods: public static List asList (T ... a).
Here Insert Picture Description
interview questions:
1.Java generics What are the benefits of using generics is??
generics is a parameterized type mechanism. It can be made applicable to various types of code, so that more write common code, for example, the frame set.
Generics is a compile-time type of confirmation mechanism. It provides compile time type safety, and to ensure that generic type (usually a generic collection) Use only the correct type of the object on to avoid ClassCastException at run time.

2, how Java's generics work? What is the type erasure?
Generic work is dependent on the compiler to compile the source code when the first type checking, and then type erasure and inserted in place of the type parameters appear cast the relevant instructions to achieve.
The compiler erase all types of relevant information at compile time, so any type of relevant information does not exist at runtime. For example, only a List List at run-time type to represent. Why should erase it? This is to avoid the type of expansion.

3. What is a generic of limited and non-limited wildcard wildcards?
Limited wildcard types were limited. Defining two wildcards, one is <? ** extends ** T> by ensuring type must be bound to a subclass of T ** set type, and the other is <? ** super * * T> by ensuring type T parent class must be set lower bound type ** **. Generic type must be initialized with types within the limit, otherwise it will cause a compiler error. On the other hand <?> Represents a non-limiting wildcard, since <?> Can be used instead of any type.

4.List <? Extends T> and List <? Super T> What is the difference between?
Examples of these two List declarations are limited wildcard, List <? Extends T> you can accept any type T inherits from the List while List <? super T> List T can accept any parent class composed. For example List <? Extends Number> can accept List or List. You can find more information in this connection paragraph that appears.

5. How to write a generic method so that it can accept generic parameter and returns a generic type?
To write a generic method is not difficult, you need to use generic types instead of primitive types, such as using T, E or K, V and other widely recognized type of placeholder. See an example of a generic method of Java collections framework. In the simplest case, a generic method might look like this:
public V PUT (Key K, V value) {
return cache.put (Key, value);
}

7. write a generic program to implement LRU cache?
For people who like the Java programming This is the equivalent of an exercise. Give you a hint, LinkedHashMap LRU cache can be used to implement a fixed size, when the LRU cache is full, when it will be removed oldest cache key-value pairs. LinkedHashMap provides a method called removeEldestEntry (), the method is put () and putAll () call to delete the oldest pairs.

8. You can List List passed to the method that accepts parameters do?
For anyone not familiar with generics, this Java generics topic seems confusing, because at first glance String is an Object, so List should be used where needed List, but it is not. Really do so will result in a compilation error. If you then consider the deep step, you will find that Java makes sense, because the List can store any type of object, including String, Integer, etc., while List can only be used to store Strings.
ObjectList List;
List stringList;
objectList = stringList; // Compilation error Incompatible types

9.Array can use generics it?
Array fact does not support generics, it is recommended to use List instead of Array, since List can provide the type of security guarantees compile, and Array can not.

11, the distinction between primitive types in Java List and List?
The main difference between the original type and type parameters, the compiler does not perform type security checks at compile-time type of the original, but with a type parameter will be check, by using as the Object type, the compiler may inform the process can accept any type of object, such as a String or an Integer . This question is to examine points correct understanding of the generic type of the original. The second point of difference between them is that you can pass any generic type arguments to accept raw type List method, but it can not pass a List to List of accepted method, because it will produce a compilation error.

What is the difference between 12, Java in List <?> And List are?
List <?> Is a type of List is unknown, but in fact any type of List List. You can put List, List assigned to List <?>, Can not be assigned to List List.
<?> List listOfAnyType;
List listOfObject = new new ArrayList ();
List listOfString = new new ArrayList ();
List listOfInteger = new new ArrayList ();

listOfAnyType = listOfString; //legal
listOfAnyType = listOfInteger; //legal
listOfObjectType = (List) listOfString; //compiler error - in-convertible types

13, and the distinction between primitive types List List.
The question like "What is the difference between the original type and type parameters." With the parameter type is type-safe , but it is the type of security guarantee by the compiler, but the raw type List is not type-safe. You can not put any other type of outside Object type String String into a List, and you can put any type of object stored in the original List. Use of the generic type with parameters you do not need to be cast, but for primitive types, you will need explicit type conversions.
ListOfRawTypes the ArrayList = new new List ();
listOfRawTypes.add ( "ABC");
listOfRawTypes.add (123); // this allows the compiler - Shique abnormal operation
String item = (String) listOfRawTypes.get ( 0); // explicit type conversion required
item = (String) listOfRawTypes.get (1 ); // throw ClassCastException, can not be converted because the Integer String

ListOfString ArrayList = new new List ();
listOfString.add ( "ABCD");
listOfString.add (1234); // compile error, better than throwing an exception at run-time
item = listOfString.get (0); // no explicit type conversion - compiler automatically converted

Published 99 original articles · won praise 2 · Views 2606

Guess you like

Origin blog.csdn.net/weixin_41588751/article/details/105299622