关于泛型和注释,有哪些好的 Java 面试问题和答案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2883314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are some good java interview questions and answers regarding generics and annotations?
提问by Daniel Honig
What are some good java interview questions and answers regarding generics and annotations?
关于泛型和注释,有哪些好的 Java 面试问题和答案?
回答by Mark Peters
Since Java 5 came out, I have seen dozens of people not understand why, given an interface I, and classes Aand B extends Ayou can't pass an I<B>where an I<A>is required. A lot of people find it counter-intuitive.
自从Java 5问世以来,我已经看到很多人不明白为什么,给定一个 interfaceI和 classesA而B extends A你不能传递一个I<B>where an I<A>is required。很多人认为这违反直觉。
To test a person's ability to reasonabout Generics, then I would first ask them if it ispossible to assign an I<B>to an I<A>reference as described above. If not, why not? If they get it wrong, tell them they're wrong and ask them to try to fill in the blanks here to show why this example would be type un-safe if it could compile:
为了测试一个人的能力的原因有关泛型的话,我会先问他们,如果它是可能的一个分配I<B>到I<A>如上所述参考。如果没有,为什么不呢?如果他们弄错了,告诉他们他们错了,并让他们尝试填写此处的空白,以说明如果此示例可以编译,为什么会键入不安全:
//...
List<String> list = new LinkedList<String>();
someMethod(list);
//blank 1
}
public void someMethod(List<Object> list) {
//blank 2
}
At this point it should be pretty easy and I would be a bit worried if they couldn't construct such an example. An example is
在这一点上应该很容易,如果他们不能构建这样一个例子,我会有点担心。一个例子是
//blank 1
String item = list.get(0);
//blank 2
list.add(Integer.valueOf(5));
回答by Vadim
This test:
本次测试:
http://tests4geeks.com/test/java
http://tests4geeks.com/test/java
contains some questions about annotations.
包含一些关于注解的问题。
It does not contain any questions about generic. But instead of it, there are some other interesting themes like:
它不包含任何关于泛型的问题。但除此之外,还有一些其他有趣的主题,例如:
Multi-Threading,
多线程,
Memory,
记忆,
Algorithms and Data Structures,
算法和数据结构,
OOP,
面向对象,
etc.
等等。
回答by Thomas Jung
- What does the
Class Enum<E extends Enum<E>>ensure? - Is
<T> List<? extends T> x()a useful signature? - Annotation retentions policies and why they are in place?
- Suppose you would like to reuse a class in different contexts would you use annotations or external configuration? (i.e. annotation introduce dependencies).
- 什么是
Class Enum<E extends Enum<E>>保证? - 是
<T> List<? extends T> x()一个有用的签名? - 注释保留政策以及为什么要实施这些政策?
- 假设您想在不同的上下文中重用一个类,您会使用注释还是外部配置?(即注解引入依赖)。
Harder:
更难:
- Examples of a valid generic type that cannot be expressed with the Java type system and will lead to compiler warnings.
- Examples where the java compiler will/will not infere the generic type? (examples of code where the compiler will infere the wrong type)
- What generic type information is not erased and can be retrieved from the byte code? (Super type tokesare one application)
- What's APT (use cases when not to use reflection)?
- 无法用 Java 类型系统表示的有效泛型类型的示例,将导致编译器警告。
- java 编译器将/不会推断泛型类型的示例?(编译器会推断出错误类型的代码示例)
- 哪些通用类型信息没有被擦除,可以从字节码中检索?(超级类型令牌是一种应用程序)
- 什么是 APT(不使用反射的用例)?
回答by dbyrne
Question:How can you determine what type of object a generic is using at runtime?
问题:如何确定泛型在运行时使用的是什么类型的对象?
Answer:It is not possible to determine the type because of type erasure.
答:由于类型擦除,无法确定类型。
回答by LoudNPossiblyWrong
- Generic:
- 通用的:
Q: What is the difference between a Hashmap and a Hashtable?
问:Hashmap 和 Hashtable 有什么区别?
A: Hashtable is synchronized, Hashmap is not.
A:Hashtable 是同步的,Hashmap 不是。
- Annotations:
- 注释:
Q: Describe serializing java objects with the javax.xml.bind.Marshaller interface and annotations.
问:描述使用 javax.xml.bind.Marshaller 接口和注释序列化 java 对象。
A: Describing something like this in a meaningful context ought to be acceptable:
A:在有意义的上下文中描述这样的事情应该是可以接受的:
@XmlRootElement
class Employee {
...
}
@XmlRootElement
class Employee {
...
}
回答by Uri
If you are looking for a rock star Java programmer, you could create quite a few advanced questions from the Generics chapter in Bloch's Effective Java. Things like the homogeneous or heterogenous containers (the bird cage and lion cage examples from one of the java tutorials), erasure, etc.
如果您正在寻找一名摇滚明星 Java 程序员,您可以从Bloch 的 Effective Java的泛型一章中创建相当多的高级问题。诸如同质或异质容器(Java 教程之一中的鸟笼和狮子笼示例)、擦除等。
In less senior folks I primarily look for an understanding of why one would want generics (you'd be surprised how many folks don't appreciate that and believe the Java 2 way of doing things still rules), and things like that.
在不太资深的人中,我主要是想了解为什么人们会想要泛型(您会惊讶于有多少人不欣赏这一点并相信 Java 2 的做事方式仍然规则),以及诸如此类的事情。
In annotations, I like asking about the "Override" annotation and its benefits. I also like having a deeper discussion about the pros and cons of annotations and when it is appropriate to use them - I'm not a fan of overzealous meta programming. It's also a good chance to see if someone used Hibernate or JUnit with annotations.
在注释中,我喜欢询问“覆盖”注释及其好处。我也喜欢更深入地讨论注释的优缺点以及何时使用它们是合适的——我不喜欢过度热心的元编程。这也是一个很好的机会,看看是否有人使用带有注释的 Hibernate 或 JUnit。
回答by John Feminella
Here are a few I just made up:
以下是我刚刚编的一些:
-- [Cagey generics] Would uncommenting any of these lines cause problems? Which ones, if any? Why or why not?
-- [Cagey generics] 取消注释这些行中的任何一行会导致问题吗?哪些,如果有?为什么或者为什么不?
public class Cage<T> { ... }
public class Animal { ... }
public class Bear extends Animal { ... }
// Cage<Animal> c = new Cage<Bear>();
// Cage<Bear> c = new Cage<Animal>();
// Cage<?> c = new Cage<Animal>();
// Cage<Animal> c = new Cage<?>();
-- [Constraints] Only Animalsshould go into Cages. How can we tighten up the class definitions above so that they reflect this new requirement?
-- [约束] 只Animals应进入Cages. 我们如何收紧上面的类定义,以便它们反映这一新要求?
-- [Legal troubles] You cannot instantiate an array of a generic type. Thus, something like new List<Animal>[10]is illegal. Can you think of a scenario where, if this werelegal, you would run into trouble?
-- [法律问题] 不能实例化泛型类型的数组。因此,类似的事情new List<Animal>[10]是非法的。你能想出一个场景,如果这是合法的,你会遇到麻烦吗?
Answers left as an exercise to the reader -- you're not going to learn anything if you don't figure them out yourself! But here are some hints.
答案留给读者作为练习——如果你自己不弄清楚,你就不会学到任何东西!但这里有一些提示。
- [Cagey generics]: What does
?mean? Do you remember the term "covariance"? - [Constraints]: Java lets you constrain the values of a type parameter. Do you remember the syntax for this?
- [Legal troubles]: Suppose you could do
Object[] arr = new List<String>[]. Can you write some code that puts something intoarr'sListsthat shouldn't go in there?
- 【Cagey泛型】:什么
?意思?你还记得术语“协方差”吗? - [约束]:Java 允许您约束类型参数的值。你还记得这个的语法吗?
- [法律问题]:假设你可以做到
Object[] arr = new List<String>[]。你可以写一些代码,将一些东西放入arr的Lists,不应该在那里去了?
回答by Dean J
"What type of things are annotations good at?" and "what type of things are annotations bad at?" comes to mind.
“注释擅长什么类型的东西?” 和“注释不擅长什么类型的东西?” 想到。
Annotations are good at meta-programming, but as a possible best-practice, code that worked withthe annotations in should still work if you take all of them out.
注释擅长元编程,但作为一种可能的最佳实践,如果您将所有注释都去掉,与注释一起工作的代码仍然可以工作。
Or not. Your mileage may vary, but you probably want all of your senior developers to agree on that one.
或不。您的里程可能会有所不同,但您可能希望所有高级开发人员都同意这一点。
回答by Hank Gay
Generics: Ask a question designed to see if they understand type erasure.
泛型:提出一个旨在了解他们是否理解类型擦除的问题。
Annotations: Ask them what their favorite annotation is, and how it works (you don't need a detailed technical explanation, but you're looking for something more than "magic").
注释:询问他们最喜欢的注释是什么,以及它是如何工作的(您不需要详细的技术解释,但您正在寻找的不仅仅是“魔法”)。
回答by Little Bobby Tables
Annotations: What are the risks? (the compiler can get into an infinite loop and jam the build process).
注释:有哪些风险?(编译器可以进入无限循环并阻塞构建过程)。
Generics: How to build a mixin using generics? (write a generic class that takes a parameter, and then extend it with the sub-class as the parameter).
泛型:如何使用泛型构建 mixin?(编写一个带参数的泛型类,然后用子类作为参数对其进行扩展)。
Also, +1 on type erasure.
此外,+1 类型擦除。

