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 interview questions related to Java generics?

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

Share

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

This article introduces the relevant knowledge of "Java generics-related interview questions". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

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

Generics is a new feature of Java SE 1.5. The essence of generics is parameterized typing, that is, the data type being manipulated is specified as a parameter.

Benefits:

Type safety, providing type detection during compilation

2, before and after compatibility

3. Generalization code, code can be reused more

High performance, code written with GJ(generic JAVA) can bring more type information to java compilers and virtual machines, which provides conditions for further optimization of java programs.

140 How does Java's generics work? What is type erasure? How does it work?

Type checking: Provide type checking before bytecode generation

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, generate a bridge method in the subclass to solve it.

If the return type of a generic method call is erased, insert a cast when the method is called

Type of erasure:

All type parameters are replaced with their qualified types:

T->Object ? extends BaseClass->BaseClass

How it works:

Generics are implemented through type erasure, where the compiler erases all type-related information at compile time, so there is no type-related information at runtime. For example, a List is represented by only one List at runtime. This is done to ensure compatibility with binary libraries developed prior to Java 5. You cannot access type parameters at runtime because the compiler has converted generic types to primitive types. Depending on how you answer this generics question, you'll get follow-up questions about why generics are implemented by type erasure or show you faulty generic code that can cause compiler errors.

141, can you pass a List to a method that accepts a List argument?

To anyone unfamiliar with generics, this Java generic title may seem confusing, because at first glance String is an Object, so List should work where List is needed, but it doesn't. Doing so will cause compilation errors. If you think about it a little further, Java makes sense to do this because Lists can store any type of object including String, Integer, etc., whereas Lists can only store String s.

List objectList;

List stringList;

objectList = stringList; //compilation error incompatible types

142, How to prevent type-unchecked warnings in Java?

If you mix generics with primitive types, such as the following code, java 5's javac compiler will generate type unchecked warnings, such as

List rawList = newArrayList()

Note: Hello.java uses unchecked or otherwise unsafe operations;

Such warnings can be masked using the @SuppressWarnings("unchecked") annotation.

What is the difference between List and primitive List in Java?

The main difference between primitive types and parameterized types is that the compiler does not type-safety checks on primitive types at compile time, but does on parameterized types, 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 problem is that the original type of generics in the correct understanding of the point. The second difference between them is that you can pass any type with arguments to the original type List, but you can't pass List to methods that accept List because of compilation errors.

144, Write a generic program to implement LRU cache?

This is an exercise for Java programmers. To give you a hint, LinkedHashMap can be used to implement a fixed-size LRU cache that will move the oldest key-value pairs out of the cache when the LRU cache is full.

LinkedHashMap provides a method called removeEldestEntry() that is called by put() and putAll() to delete the oldest key-value pairs. Of course, if you've written a JUnit test that works, feel free to write your own implementation code.

145, can you use generics in Array?

This is probably one of the easiest Java generic interview questions, provided you know that Array does not actually support generics, which is why Joshua Bloch recommends using List instead of Array in his book Effective Java, because List provides compile-time type safety, while Array does not.

How do I write a generic method that accepts generic arguments and returns generic types?

Writing generic methods is not difficult; you need to replace the original type with a generic type, such as T, E or K,V, etc., which are widely accepted type placeholders. In the simplest case, a generic method might look something like this:

public V put(K key, V value) {

return cahe.put(key,value);

}

What is the difference between C++ templates and Java generics?

Java generic implementations are rooted in the concept of "type elimination." This technique eliminates parameterized types when source code is converted to Java Virtual Machine bytecode. With Java generics, the things we can do don't really change much; it just makes the code prettier. For this reason, Java generics are sometimes referred to as "syntactic sugar."

This is very different from C++ templates. In C++, a template is essentially a set of macros, but under a different name, and 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 primitive data types such as int. Java does not work, you must use Integer instead.

In Java, you can restrict the parameter types of a template to certain types.

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

In Java, type arguments cannot be used for static methods (?) and variables because they are shared by instances specified by 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 arguments are erased at runtime. In C++, instance variables differ depending on the type parameter.

"Java generics related interview questions what" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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