Java face test-generic 14 __java

Source: Internet
Author: User
Tags java se
Java face test questions-generic article 14

139. What is generics in Java? What are the benefits of using generics?

Generics are a new feature of the Java SE 1.5, the essence of which is a parameterized type, which means that the data type being manipulated is specified as a parameter.

Benefits:

1. Type safety, providing type detection during compilation

2, before and after compatibility

3, generalization code, the code can be more re-use

4, the performance is high, the code written by the (generic Java) can bring more type information to the Java compiler and the virtual machine, this information provides the condition for the Java program to make further optimization.

How does the generics of 140,java work? What is type erase? How to work.

1. Type checking: Provides type checking before generating bytecode

2. Type erase: All type parameters are replaced with their qualified type, including classes, variables, and methods (type erase)

3. If type erasure and polymorphism conflict, the bridge method is generated in the subclass to resolve

4. If the return type of the calling generic method is erased, the coercion type conversion is inserted when the method is called

Type erase:

All type parameters are replaced with their qualified type:

Like T->object? Extends Baseclass->baseclass

How to work:

Generics are implemented by type Erasure, and the compiler erases all type-related information at compile time, so there is no type-related information at run time. For example, list<string> is represented by only one List at run time. The goal is to make sure that you can develop a binary class library compatible with the version prior to Java 5. You cannot access the type parameter at run time because the compiler has converted the generic type to the original type. Depending on your response to this generic question, you'll get some follow-up questions, such as why generics are implemented by type Erasure or show you some of the wrong generic code that can cause compiler errors.

141, can you pass list<string> to a method that accepts list<object> parameters?

For anyone unfamiliar with generics, this Java generic topic looks confusing because at first glance String is an Object, so list<string> should be used where list<object> is needed, But that is not the case. If you do this, you will cause a compilation error. If you think about it a little further, you'll find that Java makes sense, because list<object> can store any type of object, including string, Integer, and so on, whereas list<string> can only be used to store string s.

List<object> objectList;

List<string> stringlist;

ObjectList = stringlist; Compilation error incompatible types

142, how do I prevent a warning of type unchecked in Java?

If you mix generics with the original type, such as the following code, the Javac compiler for Java 5 produces a warning of type unchecked, such as

list<string> rawlist = Newarraylist ()

Note: The Hello.java uses an unchecked or unsafe operation;

This warning can be masked using @suppresswarnings ("unchecked") annotations.

What is the difference between list<object> and the original type list in 143,java?

The main difference between the original type and the parameter type <Object> is that the compiler does not type security checks on the original type at compile time, but checks the type with the parameter, and by using object as the type, you can tell the compiler that the method can accept objects of any type. such as String or integer.

The point of this question is the correct understanding of the original type in the generic form. The 2nd difference between them is that you can pass any type with a parameter to the original type List, but you cannot pass list<string> to a method that accepts list<object> because a compilation error occurs.

144, write a generic program to implement the LRU cache?

This is equivalent to a practice for people who like Java programming. To give you a hint, linkedhashmap can be used to implement a fixed-size LRU cache, and when the LRU cache is full, it moves the oldest key-value pair out of the cache.

Linkedhashmap provides a method called Removeeldestentry () that is used by put () and Putall () to delete the oldest key-value pairs. Of course, if you've written a running JUnit test, you can also write your own implementation code at will.

Can I use generics in 145,array?

This is probably the simplest of the Java generic interview questions, assuming, of course, that the array does not actually support generics, which is why Joshua Bloch in the effective Java book suggests using list instead of array, Because the list can provide type-safe guarantees for compile-time, array does not.

146, how do I write a generic method that can accept generic parameters and return generic types?

Writing generic methods is not difficult, you need to replace the original type with a generic type, such as a widely recognized type placeholder using T, E or K,V. In the simplest case, a generic method might look like this:

Public V-Put (K key, V value) {

Return Cahe.put (Key,value);

}

What is the difference between a 147,c++ template and a Java generic?

The Java generic implementation is rooted in the concept of "type elimination". This technique eliminates parameterized types when the source code is converted to Java Virtual machine bytecode. With the Java generics, what we can do doesn't really change much; he just makes the code prettier. In view of this, Java generics are sometimes referred to as "syntactic sugars."

This is very different from the C + + template. In C + +, a template is essentially a set of macro instructions, except for a different name, the compiler creates a copy of the template code for each type.

Because of architectural design differences, Java generics and C + + templates have many different points:

C + + templates can use basic data types such as int. Java is not, you must instead use integer.

In Java, you can limit the parameter type of a template to a specific type.

In C + +, type parameters can be instantiated, but not supported by Java.

In Java, type parameters cannot be used in static methods (?) and variables, because they are shared by instances specified by different type parameters. In C + +, these classes are different, so the type parameters can be used for static methods and static variables.

In Java, all instance variables are of the same type, regardless of the type parameter. Type parameters are erased at run time. In C + +, the type parameters are different and the instance variables are different.


Java Companion public number push Java development in some of the necessary skills, including but not limited to the database, Java Core, popular framework, management tools and server-related, at the same time for you select popular open source projects, will be the interview topics, practicing projects, high-quality video resources. Let your spare time consolidate your knowledge, unconsciously improve their development level.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.