Home >Java >JavaInterview questions >10 Frequently Asked Java Interview Questions
The following are 10 questions that are often asked in recent interviews. They are now compiled and shared for reference.
If you are not sure about yourself or your proficiency in Java, then this is a very Tough questions. If you are a Java expert, you should keep it low. After this, you may get questions depending on the level you admit to. So, for example, if you said 10 but couldn't answer a fairly difficult question, that would be a disadvantage.
Honestly, there are a lot of differences. Here, if you can list the most important ones, it will be enough. You should explain the new features in Java 8. For a complete list, visit the original website: Java 8 JDK.
The most important thing you should know is:
Lambda expressions, a new language feature, have been introduced in this version. Lambda expressions allow you to treat functions as method parameters or code as data. Lambda expressions allow you to express instances of single-method interfaces (called functional interfaces) more compactly.
Method references provide easy-to-read lambda expressions for methods that already have names.
Default methods allow new functionality to be added to a library's interfaces and ensure binary compatibility with code written for older versions of these interfaces.
Repeating annotationsProvides the ability to apply the same annotation type multiple times to the same declaration or type usage.
Type annotationsType annotations provide the ability to apply annotations anywhere a type is used, not just in declarations. This feature supports improved code type checking when used with a pluggable type system. 3. Do you know what collection types there are?
ArrayList
LinkedList
HashMap
HashSet
After this, you may have some questions like when should you use this particular collection type, what are the benefits compared to other types, how is it stored data, and how the data structures work behind the scenes.
The best approach here is to learn as much as possible about these collection types, as the variety of questions is almost inexhaustible.
4. What methods does the Object class have?Object class, which belongs to the java.lang package, is at the top of the class hierarchy tree. Every class is a direct or indirect descendant of the object class. Every class you use or write inherits an object's instance methods. You are not required to use any of these methods, however, if you choose to do so, you may need to override them with class-specific code.
The methods inherited from the Object discussed in this section are:
There are five methods:
(Recommendations for more related interview questions:
java interview questions and answers)2. If the string is not immutable, then it will cause serious problems to the application. security threats. For example, the database username and password are passed as strings to get the database connection, socket programming host and port details passed as strings. Because String is immutable, its value cannot be changed. Otherwise, any hacker may change the reference value, causing security issues in the application.
3. Since String is immutable, it is safe for multi-threading, and a single string instance can be shared between different threads. For thread safety, avoid using synchronization; strings are implicitly thread safe.
4. Strings are used in Java class loaders. Immutability provides the security that the correct class is loaded by the class loader. For example, consider an instance where you are trying to load java.sql. The connection class is connected, but the referenced value is changed to myhacking. Connection class, which can perform unnecessary operations on the database.
5. Because String is immutable, its hashcode will be cached when created and does not need to be calculated again. This makes it a good candidate for keys in a map, and it is processed faster than other HashMap key objects. This is why String is the most commonly used object for HashMap keys.
This question is my favorite.
final keyword is used in several contexts to define an entity that can only be allocated once.
Java finally block is a block used to execute important code, such as closing connections, streams, etc. Regardless of whether the exception is handled or not, the Java finally block is always executed. Java finally block follows try or catch block.
Finalize is a method called by the GarbageCollector (garbage collector) before deleting/destroying an object that is eligible for garbage collection to perform cleanup activities.
The diamond problem reflects why multiple inheritance is not allowed in Java. If there are two classes that have a shared super class with a specific method, then it will be overridden in both subclasses. Then, if you decide to inherit from either subclass, the language has no way of deciding which method you want to call if you want to call it
We call this problem Diamond question. Its name comes from the image above, which depicts the warning.
I think this is a very difficult problem. You need to make some modifications to your class to achieve immutability:
1. Declare the class as final so that it cannot be extended.
2. Make all fields private so that direct access is not allowed.
3. Do not provide setter methods for variables
4. Make all variable fields final so that their values can only be assigned once.
5. Initialize all fields through the constructor that performs deep copying.
6. Perform object cloning in the getter method to return a copy instead of returning the actual object reference.
Singleton is a class that only allows one instance of itself to be created and provides access to the created instance. It contains static variables that can hold unique and private instances of itself. It can be used when the user wishes to limit the instantiation of a class to one object. This is often helpful when a single object is needed to coordinate operations across systems.
This is the first question you must know when working in Java EE or Spring. Inversion of Control (IoC) is a design principle in object-oriented programming that can be used to reduce the coupling between computer codes. The most common method is called Dependency Injection (DI), and the other method is called Dependency Lookup. Through inversion of control, when an object is created, an external entity that controls all objects in the system passes the reference of the object it depends on to it. It can also be said that dependencies are injected into the object.
The component does not perform positioning queries and only provides ordinary Java methods for the container to determine dependencies. The container is solely responsible for the assembly of components. It will pass objects that meet dependencies to the required objects through JavaBean properties or constructors. The practice of injecting dependencies through JavaBean properties is called Setter Injection; the practice of passing dependencies as constructor parameters is called Constructor Injection
In this article, we discuss the top 10 Java interview questions, which I believe, based on my experience, are the most important questions today. If you know this, I believe you will have a huge advantage in the recruiting process.
If you have had similar experiences with this topic, or if you have some success stories, please share them in the comments below.
Related recommendations: java video tutorial
The above is the detailed content of 10 Frequently Asked Java Interview Questions. For more information, please follow other related articles on the PHP Chinese website!