90% of Java interview questions (with answers) will be asked at a big interview

Posted May 25, 20206 min read

Three characteristics of object-oriented

Encapsulation, inheritance, polymorphism

Benefits of polymorphism, how to implement polymorphism in code, how to implement polymorphism in virtual machine

Allow different types of objects to respond to the same message, the benefits are as follows:

Replaceability:Polymorphism is replaceable to existing code

Extensibility:adding new subclasses will not affect the existing class structure

Interface:Polymorphism is the superclass through the method signature, to provide an interface to the subclass, by the subclass to complete or rewrite to achieve

flexibility

Simplification

In the code:interface implementation, inherit the parent class rewrite method, method overloading in the same class

In the virtual machine:dynamic binding technology(dynamic binding), determine the actual type of the referenced object during execution, and call the corresponding method according to the actual type

The meaning of the interface, the meaning of the abstract class

Interface:specification, expansion, callback

Abstract class:provide a common type for other subclasses; encapsulate repeated definitions in subclasses; define abstract methods, although the subclasses have different implementations, they are consistent when they are defined

Can the static method of the parent class be overridden by the child class

Can't. Rewriting is only applicable to instance methods, not static methods, and subclasses contain static methods with the same signature as the parent class, which we generally call hidden.

What is an immutable object

Immutable objects mean that once an object is created, the state cannot be changed. Any modification will create a new object, such as String, Integer and other wrapper classes.

The difference between static variables and instance variables

Static variables exist in the method area and belong to the class. The instance variable is stored in the heap, and its reference exists in the current thread stack.

Can I create an immutable object that contains mutable objects

Yes, you need to be careful not to share references to mutable objects. If you need to change, return a copy of the original object. Example:Date object.

Several ways to create objects in Java

new, reflection, clone(), through serialization mechanism

Can the Switch use String as a parameter and can it act on byte and long

Before Java7, you could not use String as a parameter, you can use byte, short, char, int, and encapsulated classes and enumerations. After Java7, you can use String, you ca n t use long

Talk about string intern() method

The intern() method will first query whether the constant value exists from the constant pool, if it does not exist, create it, otherwise directly put it back

What are the common methods of Object

euqals(), toString(), clone(), wait(), notify(), nofifyAll(), getClass()

Four kinds of references in Java

Strong reference, soft reference, weak reference, virtual reference. The different reference types are mainly reflected on the GC:
Strong reference:If an object has a strong reference, it will not be collected by the garbage collector. Even if the current memory space is insufficient, the JVM will not reclaim it, but throw an OutOfMemoryError error, causing the program to terminate abnormally. If you want to break the association between a strong reference and an object, you can explicitly assign the reference to null, so that the JVM will recycle the object at the appropriate time.
Soft references:When using soft references, if the memory space is sufficient, the soft references can continue to be used without being collected by the garbage collector. Only when the memory is insufficient, the soft references will be collected by the garbage collector.
Weak references:Objects with weak references have a shorter life cycle. Because when the JVM performs garbage collection, once a weak reference object is found, regardless of whether the current memory space is sufficient, the weak reference will be recycled. However, because the garbage collector is a lower priority thread, it may not be able to quickly find weak reference objects.
Virtual reference:As the name implies, it is a form of fictitiousness. If an object only holds a virtual reference, then it is equivalent to no reference and may be recycled by the garbage collector at any time.

The difference between WeakReference and SoftReference

Although both WeakReference and SoftReference are beneficial to improve the efficiency of GC and memory, WeakReference, once the last strong reference is lost, will be recycled by the GC, and although soft references cannot prevent being recycled, they can be delayed until the JVM runs out of memory.

Why there are different reference types

Unlike the C language, we can control the application and release of memory. Sometimes in Java we need to control the timing of the object being recycled, so different reference types were born. It can be said that different reference types actually recycle GC. Uncontrollable compromise. There are several usage scenarios that can be fully explained:

Use soft and weak references to solve the OOM problem:use a HashMap to save the mapping relationship between the path of the picture and the soft reference associated with the corresponding picture object. When the memory is insufficient, the JVM will automatically reclaim the space occupied by these cached picture objects Thereby effectively avoiding OOM problems.
Caching of Java objects through soft references:For example, if we create a Person class, if we need to query a person s information every time, even if it has just been queried in a few seconds, we must rebuild an instance, which will cause The consumption of a large number of Person objects, and due to the relatively short life cycle of these objects, will cause multiple GCs to affect performance. At this time, the combination of soft reference and HashMap can build a cache and provide performance.

The difference between == and equals() in Java, the difference and connection between equals() and hashcode()

\ == is an operator that compares whether two variables are equal, and equals is a method of the Object class, used to compare whether two objects are equal.

The default equals method compares the addresses of two objects, the result is the same at this time, if you want to compare the content, you must rewrite the equals method

hashcode() is a method of the Object class that returns a hash value. If equals() is the same, hashcode() is the same, and vice versa.

Is it possible that two unequal objects have the same hashcode

It is possible that two unequal objects may have the same hashcode value, only that there will be conflicts in the hashmap, if the objects are equal, the hashcode is equal, and vice versa

Can I use random numbers in hashcode

No, the hashcode must be the same value

What is the difference between a == b and a.equals(b)

If a and b are both objects, a == b compares by reference. Only when a and b point to the same object in the heap will return true, and a.equals(b) performs logical comparison, usually oh rewrite the The method provides a logical ratio.

3 \ * 0.1 == 0.3 What is the return value

false, because some floating-point numbers cannot be represented accurately

Is there any difference between a = a + b and a + = b

  • = Operator will be converted implicitly, for example

short s1 = 1; s1 = s1 +1; whether the code is wrong, and how to modify it if there is an error

There is an error, s1 =(short)(s1 + 1);

short s1 = 1; s1 + = 1 whether there is an error, how to modify if there is an error

The + = operator will automatically force the result of the expression on the right to match the data type on the left, so yes

The difference between & and &&

The former is a bit operation, the latter is a logical operator

Can there be classes inside a Java?(Non-inner classes)

There can only be one public class within a Java, and there can be multiple default classes

How to exit multiple nested loops correctly

Use labels and break;

The role of inner classes

The inner class provides better encapsulation, except for the outer class, no other class can access

The difference between final, finally, finalize

fianl is a modifier that can modify classes, methods, and variables. Finally is a keyword, catching exceptions is used in conjunction with try, and will be executed. finalize is a method called before the object is recycled, giving the object its own last chance to resurrect, but when to call finalize is not guaranteed

Which class method is clone()

An iconic interface of java.lang.Cloneable, which does not contain any methods, the clone method is defined in the object, and the clone() method is a local method, which is implemented by c or c ++

What is the difference between deep copy and shallow copy

Shallow copy:only copy the object under consideration, not the object it refers to

Deep copy:all objects are copied

What are the uses of static

Static variables and static methods are shared by class instances.

Static block, initialization operation.

Decorate the inner class as a static inner class

Guide package, specify to import static resources in a class, and do not need to use the class name

What are the final uses

Methods modified by final cannot be inherited, modified methods cannot be rewritten, and modified variables cannot be changed. For the method modified by final, the JVM will try to associate it in order to improve the operating efficiency. The modified variable will be stored in the constant pool during the compilation phase.

The compiler must follow two rearrangement rules for the final field:

  1. The writing of a final field in the constructor and the subsequent assignment of the reference of the constructed object to a reference variable cannot be reordered between these two operations

  2. The first reading of a reference containing a final domain object, and the subsequent first reading of this final domain, these two operations cannot be reordered