Win in the interview Java generics (12)

Source: Internet
Author: User
Tags java se

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

Generics are a new feature of Java SE 1.5, and the nature of generics is a parameterized type, meaning that the data type being manipulated is specified as a parameter.

Benefits:

1. Type safety, providing type detection during compilation

2, front and rear compatible

3, the generalization code, the code can more reuse

4, high performance, the code written in GJ (generic Java) can bring more type information to the Java compiler and virtual machine, which provides the conditions for further optimization of the Java program.

How does the 140,java generics work? What is type erase? How does it work?

1. Type check: Provide 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 the type erasure and polymorphism conflict, then the generation of the bridge method in the subclass resolves

4. If the return type of the calling generic method is erased, the forced 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 only by a List at run time. The purpose of this is to make sure that the Binary class library is compatible with the version developed 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 answer 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 causes the compiler to make an error.

141, can you pass the 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 the String is an Object, so list<string> should be used where it needs to be list<object>, But that is not the case. If you do this, you will cause a compilation error. If you take it one step further, you'll find that Java does this to make sense, because list<object> can store any type of object including string, Integer, and so on, while list<string> can only store string s.

List<object> objectList;

List<string> stringlist;

objectList = stringlist; //compilation error incompatible types

142, how do I block a warning from a type that is not checked in Java?

If you mix generics with primitive types, such as the following code, Java 5 's Javac compiler produces warnings that are not checked by the type, such as

list<string> rawlist = Newarraylist ()

Note: Hello.java uses an operation that is not checked or is called unsafe;

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

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

The main difference between the original type and the parameter type <Object> is that at compile time the compiler does not type safety checks on the original type, but the type with the parameter is checked, 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 problem is the correct understanding of the primitive types in generics. 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 the method that accepts List<object>, because a compilation error occurs.

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

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

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

Can I use generics in 145,array?

This is probably the simplest of the Java generic interview questions, but if you want to know that the array does not actually support generics, that's why Joshua Bloch suggests using list instead of array in the effective Java book. Because list can provide a type-safety guarantee for the compile period, array does not.

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

Writing a generic method is not difficult, you need to replace the original type with a generic type, such as using a widely recognized type placeholder such as 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?

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

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

Java generics and C + + templates have many different points due to differences in schema design:

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

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

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

In Java, type parameters cannot be used with static methods (?). and variables, because they are shared by the instances specified by the different type parameters. In C + +, these classes are different, so 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.

Win in the interview Java generics (12)

Related Article

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.