by javinpaul
由javinpaul
复习这50个问题以破解Java编程面试 (Review these 50 questions to crack your Java programming interview)
编程面试中常见的Java问题列表。 (A list of frequently asked Java questions from programming job interviews.)
Hello, everybody! Over the past few years, I have been sharing a lot of Java Interview questions and discussion individually. Many of my readers have requested that I bring them together so that they can have them in the same spot. This post is the result of that.
大家好! 在过去的几年中,我一直在单独分享许多Java面试问题和讨论。 我的许多读者都要求我将他们汇集在一起,以便他们可以将它们放在同一地点。 这篇文章的结果。
This article contains more than 50 Java Interview questions covering all important topics like Core Java fundamentals, Java Collection Framework, Java Multithreading and Concurrency, Java IO, JDBC, JVM Internals, Coding Problems, Object-Oriented programming, etc.
本文包含50多个Java面试问题,涵盖所有重要主题,例如Core Java基础知识, Java Collection Framework , Java多线程和并发 , Java IO , JDBC , JVM内部 , 编码问题 , 面向对象的编程等。
The questions are also picked up from various interviews and they are, by no means, very difficult. You might have seen them already in your phone or face-to-face round of interviews.
这些问题也可以从各种访谈中找到,绝不是很难的。 您可能已经在电话或面对面的采访中看到了它们。
The questions are also very useful to review important topics like multithreading and collections. I have also shared some useful resources for further learning and improvement like The Complete Java MasterClass to brush up and fill gaps in your Java skills.
这些问题对于复习多线程和集合等重要主题也非常有用。 我还分享了一些有用的资源,供您进一步学习和改进,例如The Complete Java MasterClass,以弥补您的Java技能空白。
So what are we waiting for? Here is the list of some of the most frequently asked Java questions in interviews for both beginner and experienced Java developers.
那么,我们还等什么呢? 这是针对初学者和经验丰富的Java开发人员的访谈中一些最常见的Java问题的列表。
2至3年经验丰富的程序员的50多个Java面试问题 (50+ Java Interview Questions for 2 to 3 years Experienced Programmers)
So, without wasting any more of your time, here is my list of some of the frequently asked Core Java Interview Questions for beginner programmers. This list focuses on beginners and less experienced devs, like someone with 2 to 3 years of experience in Java.
因此,在不浪费您更多时间的情况下,这是我的一些初学者程序员常见的Core Java Interview Questions列表。 该列表主要针对初学者和经验不足的开发人员,例如具有2至3年Java经验的人。
1) How does Java achieve platform independence? (answer)hint: bytecode and Java Virtual Machine
1) Java如何实现平台独立性? ( 答案 )提示:字节码和Java虚拟机
2) What is ClassLoader
in Java? (answer)hint: part of JVM that loads bytecodes for classes. You can write your own.
2) Java中的ClassLoader
是什么? ( 回答 )提示:JVM的一部分,用于加载类的字节码。 您可以自己编写。
3) Write a Java program to check if a number is Even or Odd? (answer)hint: you can use bitwise operator, like bitwise AND, remember, even the number has zero at the end in binary format and an odd number has 1 in the end.
3) 编写Java程序以检查数字是偶数还是奇数? ( 答案 )提示:您可以使用按位运算符,例如按位与,请记住,偶数末尾为二进制格式为零,奇数末尾为1。
4) Difference between ArrayList
and HashSet
in Java? (answer)hint: all differences between List
and Set
are applicable here, e.g. ordering, duplicates, random search, etc. See Java Fundamentals: Collections by Richard Warburton to learn more about ArrayList, HashSet and other important Collections in Java.
4) 在Java中ArrayList
和HashSet
之间的区别? ( 答案 )提示: List
和Set
之间的所有区别都适用于此,例如排序,重复项,随机搜索等。请参阅Java基础知识: Richard Warburton的Collections ,以了解有关ArrayList,HashSet和Java中其他重要Collections的更多信息。
5) What is double checked locking in Singleton? (answer)hint: two-time check whether instances is initialized or not, first without locking and second with locking.
5) 什么是Singleton中的双重检查锁定? ( 答案 )提示:两次检查实例是否已初始化,第一次不带锁,第二个带锁。
6) How do you create thread-safe Singleton in Java? (answer)hint: many ways, like using Enum or by using double-checked locking pattern or using a nested static class.
6)如何在Java中创建线程安全的Singleton? ( 回答 )提示:很多方法,例如使用Enum或使用双重检查的锁定模式或使用嵌套的静态类。
7) When to use the volatile variable in Java? (answer)hint: when you need to instruct the JVM that a variable can be modified by multiple threads and give hint to JVM that does not cache its value.
7)何时在Java中使用volatile变量? ( 答案 )提示:当您需要指示JVM可以由多个线程修改变量并将提示提供给不缓存其值的JVM时。
8) When to use a transient variable in Java? (answer)hint: when you want to make a variable non-serializable in a class, which implements the Serializable interface. In other words, you can use it for a variable whose value you don’t want to save. See The Complete Java MasterClass to learn about transient variables in Java.
8)何时在Java中使用瞬时变量? ( 回答 )提示:当您想在一个类中使变量不可序列化时,该类实现了Serializable接口。 换句话说,您可以将其用于您不想保存其值的变量。 请参阅完整的Java MasterClass,以了解Java中的瞬时变量。
9) Difference between the transient and volatile variable in Java? (answer)hint: totally different, one used in the context of serialization while the other is used in concurrency.
9)Java中的瞬时变量和易失变量之间的区别? ( 回答 )提示:完全不同,一种在序列化的上下文中使用,而另一种在并发中使用。
10) Difference between Serializable and Externalizable in Java? (answer)hint: Externalizable gives you more control over the Serialization process.
10)Java中的Serializable和Externalizable之间的区别? ( 回答 )提示:外部化可让您更好地控制序列化过程。
11) Can we override the private method in Java? (answer)hint: No, because it’s not visible in the subclass, a primary requirement for overriding a method in Java.
11)我们可以在Java中重写private方法吗? ( 回答 )提示:否,因为它在子类中不可见,这是重写Java中的方法的主要要求。
12) Difference between Hashtable
and HashMap
in Java? (answer)hint: several but most important is Hashtable
, which is synchronized, while HashMap
is not. It's also legacy and slow as compared to HashMap
.
12) Hashtable
和HashMap
在Java中的区别? ( 答案 )提示:几个但最重要的是Hashtable
,它是同步的,而HashMap
不是。 与HashMap
相比,它既旧又慢。
13) Difference between List
and Set
in Java? (answer)hint: List
is ordered and allows duplicate. Set
is unordered and doesn't allow duplicate elements.
13)Java中的List
和Set
之间的区别? ( 回答 )提示: List
是有序的,并且允许重复。 Set
是无序的,并且不允许重复的元素。
14) Difference between ArrayList
and Vector
in Java (answer)hint: Many, but most important is that ArrayList
is non-synchronized and fast while Vector
is synchronized and slow. It's also legacy class like Hashtable
.
14) ArrayList
和Vector
在Java中的区别( 答案 )提示:很多,但最重要的是ArrayList
是非同步且快速的,而Vector
是同步且缓慢的。 它也是像Hashtable
这样的遗留类。
15) Difference between Hashtable
and ConcurrentHashMap
in Java? (answer)hint: more scalable. See Java Fundamentals: Collections by Richard Warburton to learn more.
15) Hashtable
和ConcurrentHashMap
在Java中有什么区别? ( 答案 )提示:更具可扩展性。 请参阅Richard Warburton的Java基础知识:合集 ,以了解更多信息。
16) How does ConcurrentHashMap
achieve scalability? (answer)hint: by dividing the map into segments and only locking during the write operation.
16) ConcurrentHashMap
如何实现可伸缩性? ( 回答 )提示:通过将映射划分为多个段,并且仅在写操作期间锁定。
17) Which two methods you will override for an Object
to be used as Key
in HashMap
? (answer)hint: equals and hashcode
17)对于在HashMap
用作Key
的Object
,您将重写以下两种方法? ( 回答 )提示:等于和哈希码
18) Difference between wait and sleep in Java? (answer)hint: The wait()
method releases the lock or monitor, while sleep doesn't.
18)Java中的等待和睡眠之间的区别? ( 回答 )提示: wait()
方法释放锁定或监视器,而睡眠则不释放。
19) Difference between notify
and notifyAll
in Java? (answer)hint: notify
notifies one random thread is waiting for that lock while notifyAll
inform to all threads waiting for a monitor. If you are certain that only one thread is waiting then use notify
, or else notifyAll
is better. See Threading Essentials Mini-Course by Java Champion Heinz Kabutz to learn more about threading basics.
19)Java中notify
和notifyAll
之间的区别? ( 回答 )提示: notify
通知一个随机线程正在等待该锁,而notifyAll
通知所有等待监视器的线程。 如果确定只有一个线程在等待,请使用notify
,否则notifyAll
更好。 请参阅Threading Essentials微型课程 由Java冠军Heinz Kabutz提供,以了解有关线程基础的更多信息。
20) Why you override hashcode, along with equals()
in Java? (answer)hint: to be compliant with equals and hashcode contract, which is required if you are planning to store your object into collection classes, e.g. HashMap
or ArrayList
.
20)为什么要覆盖哈希码以及Java中的equals()
? ( 回答 )提示:要符合equals和hashcode契约,如果您打算将对象存储到集合类(例如HashMap
或ArrayList
,则必须这样做。
21) What is the load factor of HashMap
means? (answer)hint: The threshold that triggers the re-sizing of HashMap
is generally 0.75, which means HashMap
resize itself if it's 75 percent full.
21) HashMap
的负载因子是什么意思? ( 答案 )提示:阈值触发大小调整的HashMap
一般是0.75,这意味着HashMap
调整自身的大小,如果是75%满。
22) Difference between ArrayList
and LinkedList
in Java? (answer)hint: same as an array and linked list, one allows random search while other doesn't. Insertion and deletion easy on the linked list but a search is easy on an array. See Java Fundamentals: Collections, Richard Warburton’s course on Pluralsight, to learn more about essential Collection data structure in Java.
22)Java中的ArrayList
和LinkedList
有什么区别? ( 回答 )提示:与数组和链表相同,一个允许随机搜索,而另一个则不允许。 在链表上插入和删除都很容易,但是在数组上搜索很容易。 请参阅Richard Warburton的有关Pluralsight的课程,《 Java基础知识:集合》 ,以了解有关Java中基本Collection数据结构的更多信息。
23) Difference between CountDownLatch
and CyclicBarrier
in Java? (answer)hint: You can reuse CyclicBarrier
after the barrier is broken but you cannot reuse CountDownLatch
after the count reaches to zero.
23) CountDownLatch
和CyclicBarrier
在Java中的区别? ( 回答 )提示:您可以在屏障被破坏之后重用CyclicBarrier
,但是在计数达到零后就不能重用CountDownLatch
。
24) When do you use Runnable
vs Thread
in Java? (answer)hint: always
24)什么时候在Java中使用Runnable
vs Thread
? ( 回答 )提示:总是
25) What is the meaning of Enum being type-safe in Java? (answer)hint: It means you cannot assign an instance of different Enum type to an Enum variable. e.g. if you have a variable like DayOfWeek
day then you cannot assign it value from DayOfMonth
enum.
25)Enum在Java中是类型安全的是什么意思? ( 回答 )提示:这意味着您不能将不同Enum类型的实例分配给Enum变量。 例如,如果您具有DayOfWeek
day之类的变量,则无法从DayOfMonth
枚举中为其分配值。
26) How does Autoboxing of Integer work in Java? (answer)hint: By using the valueOf()
method in Java.
26)Integer的自动装箱在Java中如何工作? ( 回答 )提示:通过使用Java中的valueOf()
方法。
27) Difference between PATH
and Classpath
in Java? (answer)hint: PATH
is used by the operating system while Classpath
is used by JVM to locate Java binary, e.g. JAR files or Class files. See Java Fundamentals: The Core Platform to learn more about PATH
, Classpath
, and other Java environment variable.
27) PATH
和Java中的Classpath
之间的区别? ( 回答 )提示:操作系统使用PATH
,而JVM使用Classpath
来定位Java二进制文件,例如JAR文件或Class文件。 请参阅Java基础知识:核心平台,以了解有关PATH
, Classpath
和其他Java环境变量的更多信息。
28) Difference between method overloading and overriding in Java? (answer)hint: Overriding happens at subclass while overloading happens in the same class. Also, overriding is a runtime activity while overloading is resolved at compile time.
28)Java中方法重载和重写之间的区别? ( 答案 )提示:重写发生在子类中,而重载发生在同一类中。 同样,覆盖是运行时活动,而重载在编译时得到解决。
29) How do you prevent a class from being sub-classed in Java? (answer)hint: just make its constructor private
29)如何防止某个类在Java中被子类化? ( 回答 )提示:只需将其构造函数设为私有
30) How do you restrict your class from being used by your client? (answer)hint: make the constructor private or throw an exception from the constructor
30)您如何限制班级被客户使用? ( 回答 )提示:将构造函数设为私有或从构造函数抛出异常
31) Difference between StringBuilder
and StringBuffer
in Java? (answer)hint: StringBuilder
is not synchronized while StringBuffer
is synchronized.
31)Java中的StringBuilder
和StringBuffer
之间的区别? ( 答案 )提示: StringBuilder
同步时, StringBuffer
不同步。
32) Difference between Polymorphism and Inheritance in Java? (answer)hint: Inheritance allows code reuse and builds the relationship between class, which is required by Polymorphism, which provides dynamic behavior. See Java Fundamentals: Object-Oriented Design to learn more about OOP features.
32)Java中的多态和继承之间的区别? ( 回答 )提示:继承允许代码重用并建立类之间的关系,这是提供动态行为的多态性所必需的。 请参阅Java基础知识:面向对象的设计 了解有关OOP功能的更多信息。
33) Can we override static method in Java? (answer)hint: No, because overriding resolves at runtime while static method call is resolved at compile time.
33)我们可以在Java中重写静态方法吗? ( 回答 )提示:否,因为重写在运行时解析,而静态方法调用在编译时解析。
34) Can we access the private method in Java? (answer)hint: yes, in the same class but not outside the class
34)我们可以使用Java访问私有方法吗? ( 回答 )提示:是,在同一个班级中,但不在班级外
35) Difference between interface and abstract class in Java? (answer)hint: from Java 8, the difference is blurred. However, a Java class can still implement multiple interfaces but can only extend one class.
35)接口和Java中的抽象类之间的区别? ( 回答 )提示:从Java 8开始 ,差异变得模糊。 但是,Java类仍然可以实现多个接口,但只能扩展一个类。
36) Difference between DOM and SAX parser in Java? (answer)hint: DOM loads whole XML File in memory while SAX doesn’t. It is an event-based parser and can be used to parse a large file, but DOM is fast and should be preferred for small files.
36)Java中的DOM和SAX解析器之间的区别? ( 回答 )提示:DOM将整个XML文件加载到内存中,而SAX则不加载。 它是一个基于事件的解析器,可用于解析大文件,但是DOM速度很快,应该是小型文件的首选。
37) Difference between throw and throws keyword in Java? (answer)hint: throws declare what exception a method can throw in case of error but throw keyword actually throws an exception. See Java Fundamentals: Exception Handling to learn more about Exception handling in Java.
37)Java中throw和throws关键字之间的区别? ( 回答 )提示:throws声明方法在发生错误时可以抛出的异常,而throw关键字实际上抛出了异常。 请参阅Java基础知识:异常处理以了解有关Java中异常处理的更多信息。
38) Difference between fail-safe and fail-fast iterators in Java? (answer)hint: fail-safe doesn’t throw ConcurrentModificationException
while fail-fast
does whenever they detect an outside change on the underlying collection while iterating over it.
38)Java中的故障安全迭代器和故障快速迭代器之间的区别? ( 回答 )提示:故障安全不会在迭代过程中在基础集合上检测到外部更改时抛出ConcurrentModificationException
而fail-fast
抛出ConcurrentModificationException
。
39) Difference between Iterator and Enumeration in Java? (answer)hint: Iterator also gives you the ability to remove an element while iterating while Enumeration doesn’t allow that.
39)Java中的迭代器和枚举之间的区别? ( 回答 )提示:迭代器还使您能够在迭代时删除元素,而枚举不允许这样做。
40) What is IdentityHashMap
in Java? (answer)hint: A Map
, which uses the ==
equality operator to check equality instead of the equals()
method.
40)Java中的IdentityHashMap
是什么? ( 答案 )提示:一个Map
,它使用==
相等运算符而不是equals()
方法来检查相等性。
41) What is the String
pool in Java? (answer)hint: A pool of String
literals. Remember it's moved to heap from perm gen space in JDK 7.
41)Java中的String
池是什么? ( 回答 )提示: String
文字池。 记住,它已从JDK 7中的perm gen空间移到堆中。
42) Can a Serializable
class contains a non-serializable field in Java? (answer)hint: Yes, but you need to make it either static or transient.
42) Serializable
类可以包含Java中的不可序列化字段吗? ( 回答 )提示:是的,但是您需要使其为静态或瞬态。
43) Difference between this and super in Java? (answer)hint: this refers to the current instance while super refers to an instance of the superclass.
43)this和super在Java中的区别? ( 回答 )提示:这是指当前实例,而super是指超类的实例。
44) Difference between Comparator
and Comparable
in Java? (answer)hint: Comparator
defines custom ordering while Comparable
defines the natural order of objects, e.g. the alphabetic order for String
. See The Complete Java MasterClass to learn more about sorting in Java.
44)Java中的Comparator
和Comparable
之间的区别? ( 答案 )提示: Comparator
定义自定义顺序,而Comparable
定义对象的自然顺序,例如String
的字母顺序。 请参阅完整的Java MasterClass,以了解有关Java排序的更多信息。
45) Difference between java.util.Date
and java.sql.Date
in Java? (answer)hint: former contains both date and time while later contains only date part.
45)Java java.util.Date
和java.sql.Date
之间的区别? ( 答案 )提示:前者同时包含日期和时间,而后者仅包含日期部分。
46) Why wait and notify method are declared in Object
class in Java? (answer)hint: because they require lock which is only available to an object.
46)为什么在Java的Object
类中声明了wait和notify方法? ( 回答 )提示:因为它们需要仅对对象可用的锁定。
47) Why Java doesn’t support multiple inheritances? (answer)hint: It doesn’t support because of a bad experience with C++, but with Java 8, it does in some sense — only multiple inheritances of Type
are not supported in Java now.
47)为什么Java不支持多重继承? ( 回答 )提示:由于对C ++的不良体验,它不支持,但是对于Java 8,从某种意义上说,它不支持-Java现在仅不支持Type
多个继承。
48) Difference between checked and unchecked Exception in Java? (answer)hint: In case of checked, you must handle exception using catch block, while in case of unchecked, it’s up to you; compile will not bother you.
48)Java中检查和未检查异常之间的区别? ( 回答 )提示:如果选中,则必须使用catch块处理异常,而如果未选中,则取决于您; 编译不会打扰您。
49) Difference between Error and Exception in Java? (answer)hint: I am tired of typing please check the answer
49)Java中错误和异常之间的区别? ( 回答 )提示:我厌倦了打字,请检查答案
50) Difference between Race condition and Deadlock in Java? (answer)hint: both are errors that occur in a concurrent application, one occurs because of thread scheduling while others occur because of poor coding. See Multithreading and Parallel Computing in Java to learn more about deadlock, Race Conditions, and other multithreading issues.
50)Java中竞态条件和死锁之间的区别? ( 答案 )提示:两者都是在并发应用程序中发生的错误,一个是由于线程调度而发生的,而另一个是由于编码不正确而发生的。 请参阅Java中的多线程和并行计算以了解有关死锁,竞争条件和其他多线程问题的更多信息。
结束语 (Closing Notes)
Thanks, You made it to the end of the article … Good luck with your programming interview! It’s certainly not going to be easy, but by following this roadmap and guide, you are one step closer to becoming a DevOps engineer.
谢谢,您到了本文的结尾……祝您编程采访顺利! 当然这并非易事,但是通过遵循此路线图和指南,您距离成为DevOps工程师仅一步之遥 。
If you like this article, then please share with your friends and colleagues, and don’t forget to follow javinpaul on Twitter!
如果您喜欢这篇文章,请与您的朋友和同事分享,不要忘记在Twitter上关注javinpaul !
其他资源 (Additional Resources)
P.S. — If you need some FREE resources to learn Java, you can check out this list of free Java courses to start your preparation.
PS —如果您需要一些免费的资源来学习Java,则可以查看此免费的Java课程列表以开始准备。
P.S. — If you need some FREE resources to learn Java, you can check out this list of free Java courses to start your preparation.
PS —如果您需要一些免费资源来学习Java,则可以查看此免费Java课程列表以开始准备。
P.S. — If you need some FREE resources to learn Java, you can check out this list of free Java courses to start your preparation.P. S. S. — I have not provided the answer to the interview questions shared in the image “ How many String objects are created in the code?” can you guess and explain?
PS —如果您需要一些免费的资源来学习Java,则可以查看此免费的Java课程列表以开始准备。 PSS —我没有提供图像“在代码中创建了多少个String对象?”中共享的采访问题的答案。 你能猜猜并解释吗?