Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Java generic interview questions?

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the Java generic interview questions". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the Java generic interview questions?"

one hundred and thirty nine。 What are generics in Java? What are the benefits of using generics?

Generics are a new feature of Java SE 1.5. generics are parameterized types in nature, that is, the data type being operated on is specified as a parameter.

Benefits:

1. Type safety, providing type detection during compilation

2. Compatibility

3. Generalize the code, and the code can be reused more.

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

How does the generics of 140Jol Java work? What is type erasure? How do you work?

1. Type checking: provide type checking before generating bytecode

Type erasure: all type parameters are replaced with their qualified types, including classes, variables, and methods (type erasure)

3. If there is a conflict between type erasure and polymorphism, a bridge method is generated in the subclass to solve the problem.

4. If the return type that calls a generic method is erased, a cast is inserted when the method is called

Type erase:

All type parameters are replaced with their qualified types:

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 is represented by only one List at run time. The purpose of this is to ensure compatibility with binary class libraries developed before Java 5. You cannot access type parameters at run time because the compiler has converted generic types to primitive types. Depending on your answer to this generics question, you will get some follow-up questions, such as why generics are implemented by type erasure or show you some error generics code that can cause compiler errors.

141.Can you pass List to a method that accepts List parameters?

For anyone who is not familiar with generics, this Java generics topic looks confusing, because at first glance String is a kind of Object, so List should be used where List is needed, but this is not the case. Doing so will result in compilation errors. If you think about it further, you will find that Java makes sense, because List can store any type of objects, including String, Integer, etc., while List can only be used to store String s.

List objectList

List stringList

ObjectList = stringList; / / compilation error incompatible types

142.How do I prevent unchecked warnings for types in Java?

If you mix generics with primitive types, such as the following code, the javac compiler for java 5 will generate a warning that the type is not checked, such as

List rawList = newArrayList ()

Note: Hello.java uses unchecked or unsafe operations

This warning can be shielded using the @ SuppressWarnings ("unchecked") annotation.

What is the difference between List and primitive type List in 143Jet Java?

The main difference between primitive types and types with parameters is that at compile time, the compiler does not do a type safety check on the original type, but does check the type with parameters, and by using Object as the type, you can tell the compiler that the method can accept any type of object, such as String or Integer.

The focus of this question is the correct understanding of the primitive types in generics. The second difference between them is that you can pass any type with parameters to the original type List, but you cannot pass List to a method that accepts List because of a compilation error.

Write a generic program to implement LRU caching.

For people who like Java programming, this is the equivalent of an exercise. 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 called by put () and putAll () to delete the oldest key-value pair. Of course, if you have written a runnable JUnit test, you can also write your own implementation code at will.

Can generics be used in 145MagneArray?

This is probably the simplest of the Java generic interview questions, of course, as long as you know that Array doesn't actually support generics, which is why Joshua Bloch suggests using List instead of Array in his book Effective Java, because List provides compile-time type safety, while Array can't.

146, how do you write a generic method that accepts generic parameters and returns a generic type?

It is not difficult to write a generic method. You need to replace the original type with a generic type, such as using widely accepted type placeholders such as T, E or KMagol V, etc. 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 the 147GramCategory + template and the java generics?

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 Java generics, what we can do doesn't really change much; it just makes the code more beautiful. For this reason, Java generics are sometimes called "grammatical sugars".

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

Due to differences in architectural design, Java generics and C++ templates differ in many ways:

C++ templates can use basic data types such as int. Java is not, you have to switch to Integer.

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

In C++, type parameters can be instantiated, but java does not support it.

In Java, type parameters cannot be used in static methods. And variables, because they are shared by instances specified by different types of parameters. These classes are different in duration, 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, the instance variables are also different.

At this point, I believe you have a deeper understanding of "what are the Java generic interview questions?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report