I have been working at Oracle for nearly 7 years and have interviewed Java engineers ranging from junior to very senior. Due to the characteristics of the Tasks in the Java group, I attach great importance to the interviewees’ computer science foundation and depth of understanding of programming language, so Java proficiency is not required. So, if you have a very systematic and in-depth knowledge of other languages such as C/C++, it will also meet the requirements.
Over the years of working and in interviews, I have often experienced that some interviewees really do work hard, but frankly they are not competent enough to pass the interview, usually for two reasons:
1. “Know what it is and don’t know why.” I’ve been doing technology for many years, and I’ve developed a lot of business applications, but I don’t seem to have thought about the logic behind the various technology choices. To be honest, I’m not sure I trust him with a task of some depth.
Knowledge fragmentation, not a system. In interviews, it seems that candidates are unable to fully and clearly describe the systems they have developed or the technologies they use. You might be working hard, or getting too wrapped up in implementation details, and not looking up at the technology.
Predecessors have fallen the pit, then the students do not “forward and backward”!
I sorted out 8 classic Java interview questions from my column “Java Core Technology 36 lectures”, will analyze this question from “typical answers”, “test point analysis”, “knowledge expansion” three aspects of the context and knowledge points. However, most of the following sections are selected from the “test point analysis” section. If you are interested in “typical answers” and “knowledge expansion”, you can scroll to the end of this article, scan the code or click “Read the original article” to subscribe to my column.
As for why choose “test point analysis”, teach people to fish is better than teach people to fish, I hope you can through the test point analysis and guidance, independent thinking to find out the answer.
Java based
What is your understanding of the Java platform? Is it true that “Java is interpreted execution”?
1.
For general questions like this, you want to show that you are thinking deeply and systematically, and that you have a comprehensive understanding of Java. You want to avoid the impression that you don’t “know what you know”. After all, understanding the basic components and mechanisms is the basis for many things in daily work, from problem diagnosis to performance tuning. I believe that no employer would not like a candidate who “loves to learn and think”.
Returning to the topic, the understanding of the Java platform can be briefly discussed from many aspects, such as: Java language features, including generics, Lambda and other language features; Basic class libraries, including collections, IO/NIO, network, concurrency, security and other basic class libraries. For our daily work more application of class library, before the interview can be systematically summarized, helpful to play on the spot.
Below is my summary of a relatively broad blueprint for your reference.
2. Compare Exception with Error, and what is the difference between run-time exceptions and normal exceptions?
1.
By analyzing the difference between Exception and Error, we examine the Java processing mechanism from a conceptual perspective. Generally speaking, it is still at the level of understanding, and the interviewer just needs to make it clear.
In our daily programming, how to deal with exceptions is quite a test of skills, I think we need to master two aspects.
First, understand the design and classification of Throwable, Exception, and Error. For example, learn which subclasses are most widely used and how to customize exceptions.
Many interviewers will ask for further details, such as, what errors, exceptions, or runtimeExceptions do you know? I have drawn a simple class diagram, and listed typical examples, can give you as a reference, at least do the basic know.
Second, understand the elements and practices of the Java language for manipulating Throwable. Basic syntax, such as try-catch-finally blocks, throws, throws, and so on, is a must. At the same time, know how to handle typical scenarios.
3. Talk about Java reflection. What is dynamic proxy based on?
1.
My first impression of this topic is that it is a bit of a temptation to subconsciously assume that dynamic proxy is implemented by reflection, which is not wrong but slightly incomplete. Functionality is the goal, and there are many ways to achieve it.
In summary, this topic examines another fundamental mechanism of the Java language: reflection, a magic that introduces runtime introspection and gives the Java language surprising power to manipulate information that can only be determined at runtime by manipulating metadata or objects at runtime. Dynamic proxy is an extension of a widely used technology in product development, a lot of tedious repetitive programming, can be gracefully solved by dynamic proxy mechanism.
This question involves a lot of knowledge, so the interviewer can expand or dig deeply into the content. For example:
Test your understanding and mastery of reflex mechanisms.
What problems does dynamic proxy solve and what are the application scenarios in your business system?
How do JDK dynamic proxies differ in design and implementation from cglib and other approaches, and how to choose between them?
4. What IO methods does Java provide? How does NIO achieve multiplexing?
In the actual interview, from traditional IO to NIO and NIO 2, there are many places that can be expanded, and the investigation points involve various aspects, such as:
Basic API functions and design, InputStream/OutputStream and Reader/Writer relationship and difference.
Basic components of NIO and NIO 2.
Given the scene, implement with different models respectively, analyze the design and implementation principle of BIO, NIO and other modes.
What are the principles behind the high-performance data manipulation provided by NIO, and how can it be used?
Or, from a developer’s perspective, what problems do you see with NIO’s implementation itself? Any ideas for improvement?
IO has a lot of content, which is difficult to explain in a column. IO is more than just multiplexing, and NIO 2 is more than just asynchronous IO, especially in the data manipulation section, which will be examined in more detail in the next column.
By the way, I recommend an architecture exchange group: 617434785, which will share some videos recorded by senior architects
5. How to make containers thread-safe? How does ConcurrentHashMap achieve efficient thread safety?
Typical answers:
Java provides different levels of thread-safety support. Inside the traditional Collections framework, in addition to synchronous containers such as Hashtable, there are so-called Synchronized wrappers that we can invoke Wrapper methods provided by the Collections utility class, To obtain a synchronized packaging container (such as Collections. SynchronizedMap), but they are using a very coarse-grained synchronously, under the condition of high concurrency, performance comparison is low.
Alternatively, a more common option is to take advantage of the thread-safe container class provided by the package, which provides:
Various concurrent containers, such as ConcurrentHashMap and CopyOnWriteArrayList.
Various thread-safe queues (queues/deques), such as ArrayBlockingQueue, SynchronousQueue.
Thread-safe versions of various ordered containers, etc.
Methods for ensuring thread safety range from the simple Synchronize method to more sophisticated concurrent implementations such as ConcurrentHashMap based on a separate lock implementation. The specific choice depends on the scenario requirements developed, and overall, the container common scenario provided in the package is far superior to the earlier simple synchronization implementation.
What is the difference between an interface and an abstract class?
1.
This is a very high frequency Java object-oriented basic question, it seems very simple question, if the interviewer dig a little deeper, you will find a lot of interesting places, can be from different angles to comprehensively examine your understanding and mastery of the basic mechanics.
Such as:
Is the syntax of the basic elements of Java understood correctly? Whether we can define syntactically correct interfaces, abstract classes, or related inheritance implementations involves Overload and Override, there are different topics.
Use interfaces and abstract classes properly in software design and development. You at least know the typical application scenarios and the use of important interfaces in the base library. Master design methods and be able to review code to see obvious designs that are not conducive to future maintenance.
Master the evolution of Java language features. With so many frameworks already based on Java 8 and gradually supporting newer versions, it’s important to understand the syntax and design purpose.
Java advanced
7. How to implement synchronized at the bottom level? What are the upgrades and downgrades of locks?
1.
Today’s question is about your knowledge of Java built-in lock implementation and is a classic topic for concurrency. The typical answer I gave earlier covers some basic concepts. If the foundation is not solid, some concepts will be difficult to understand. I suggest that you try your best to understand and master them. Even if you don’t understand them, you don’t have to worry about them.
In my opinion, a basic understanding of these concepts and mechanisms is sufficient for most concurrent programming. After all, most engineers do not necessarily engage in lower-level, more basic research and development. In many cases, it is a matter of knowing or not.
I will analyze it further later:
From the source level, slightly expand the underlying implementation of synchronized, and supplement some details lacking in the above answer, some students feedback this part is easy to be asked. If you’re interested in the underlying Java source code but haven’t yet found a starting point, this is a good place to start.
Understand and in the process of contract awarding Java. Util. Concurrent. Other locks, lock with Java, after all, is not only already a explicit lock type, I will combine the code analysis its use.
8. What’s the difference between synchronized and ReentrantLock? Is it true that synchronized is the slowest?
1.
Today’s topic is a common foundation for examining concurrent programming, and the typical answers I give are a relatively comprehensive summary.
For concurrent programming, different companies or interviewers have different interview styles. Some big companies like to keep asking you about the extension or underlying mechanism, and some like to start from a practical point of view, so you need to be patient in preparing for concurrent programming.
In my opinion, as one of the basic tools for concurrency, you need to know at least:
Understand what thread safety is.
Basic use of synchronized, ReentrantLock and other mechanisms and cases.
To go one step further, you need to:
Master the low-level implementation of synchronized and ReentrantLock; Understand lock inflation and degradation; Understand the concepts of skew lock, spin lock, lightweight lock, heavyweight lock, etc.
In the process of contract awarding master and Java. Util. Concurrent. Lock all kinds of different implementation and case analysis.
Typical answers:
Synchronized is a built-in Java synchronization mechanism, which is also referred to as Intrinsic Locking. Synchronization provides mutually exclusive semantics and visibility. When one thread has acquired the lock, other threads attempting to acquire the lock must wait or block.
Prior to Java 5, synchronized was the only means of synchronization, and synchronized could be used in code to modify methods or in specific code blocks, Essentially a synchronized method is equivalent to wrapping all of the method statements in synchronized blocks.
ReentrantLock, commonly translated as reentry lock, is a lock implementation provided by Java 5 that has essentially the same semantics as synchronized. Reentry locks are obtained by code calling the lock() method directly, and code writing is more flexible. At the same time, ReentrantLock offers a number of useful ways to achieve detailed control that synchronized cannot, such as controlling the fairness, or defining conditions. However, it is important to note that the unlock() method must be explicitly called to release the lock, otherwise the lock will always be held.
The performance of synchronized and ReentrantLock cannot be regarded as the same. The early version of Synchronized has a great difference in performance in many scenarios, and has made many improvements in subsequent versions. In low-competition scenarios, synchronized may perform better than ReentrantLock.