Speed and efficiency! The latest compilation of Java back-end interview questions (with answers)

In this article, the editor has compiled an article about the latest content of Java back-end interview questions for everyone. Friends who need it can refer to it.

#### We learn java knowledge, in addition to basic program operation, it is inevitable to encounter some theoretical investigations in the interview. Some small partner programs do well, but they are lacking in theory. The editor here has compiled some common back-end interview questions, hoping to be helpful to the friends, let's take a look.
image.png

1. The size of the eight basic data types and their encapsulation classes.
byte(Byte) 1 ,short(Short) 2 ,int(Integer) 4 ,long(Long) 8 ,float(Float) 4 ,double(Double)8,boolean(Boolean),char(Character)2

2. Can the Switch use string as a parameter?
The variable type in the switch statement can be byte, short, int, char. The String type can be used after jdk1.7, which is judged by converting String to int through String.hashcode in switch.

Three, the difference between equals and ==.
The == operator is used to compare whether the values ​​of two variables are equal, that is, to compare whether the storage addresses of the variables in the memory are the same. In the equals() method, the String class is inherited from the Object class and is used to detect two objects Are the contents the same.

Fourth, String s=new String('xyz'); How many object objects
are created ? A String variable s will be created. If the literal "xyz" does not appear before the class is loaded here, loading here will create a String constant object corresponding to "xyz". On a compliant JVM, the new keyword will create a String object when executed.

5. What are the public methods of Object?
1. clone() creates a bin and returns a copy of this object

2, equals() judgment

3. getclass() returns the running class of object

4. hashcode() returns the hash code value of the object

5. notify() wakes up a single process that is waiting for the object listener

6, notifyAll () wake up all processes that are waiting for the object listener

7. wait() causes the current thread to wait until another thread calls the notify() method or notifyAll() method of the object.

8. toString() returns the string representation of this object

9, finalize () when the garbage collection determines that the object is not needed, the garbage collector calls this method

Six, four kinds of Java references, strong and weak, and the scenes used.
Strong references: garbage collector will not recycle

Soft reference: If the memory space is sufficient, the garbage collector will not recycle, if the memory space is insufficient, the garbage collector will recycle

Weak references: Once an object with only weak references is found, the garbage collector will recycle it.

Virtual reference: If it is found that the object still has a virtual reference, the virtual reference will be added to the reference queue associated with it before the object is recycled.

Seven, the difference between static variables and instance variables.
Add the keyword static before static variables, but not instance variables.

An instance variable is an attribute of an object, and an instance object must be created before the instance variable can be allocated space before the instance variable can be used. Static variables do not belong to any instance objects, but belong to classes, also called class variables. As long as the program loads the bytecode of the class without creating any instance objects, space will be allocated. In short, static variables can be used directly without creating any objects, while instance variables need to create instance objects before they can be used.

8. The difference between Overload and Override:
Overload means that there can be multiple methods with the same name in the same class, but the parameter lists of these methods are different, that is, the parameter parameters or parameter types are different. The return value of overloading can of course be different, but if the parameter list is completely consistent, the overloading cannot be achieved through inconsistent return types. This is not possible.

Override means that the method in the subclass can be exactly the same as the method name and parameters in the parent class. When calling this method through the object created by the subclass, the method defined in the subclass will be called, that is, the method in the subclass The method overrides the method of the parent class. When the subclass overrides the superclass method, it can only throw fewer or smaller exceptions than the superclass. The return of the overridden method must be consistent with the return of the overridden method.

Nine, the difference between abstract classes and interfaces.
An abstract class can have a default method for implementation, can have a constructor, can have a main method for operation, and can directly add the implemented method to the class. The interface does not have a default method for implementation, and there is no constructor. You cannot use the main method for implementation. Run, when adding methods to the interface, you need to add methods to the specific implementation class.

10. The difference between String, StringBuffer and StringBuilder.
String represents a string whose content cannot be modified, StringBuffer represents a string whose content can be modified, String covers the equals() method and the hashcode() method, and StringBuffer does not cover the two methods, so when the StringBuffer object is stored in the java collection class There will be problems.

StringBulider also represents a string whose content can be modified, but its thread is unsafe and runs efficiently.

The above is the whole content of this article, I hope it will be helpful to everyone's study, and I hope everyone will support you

The latest high-frequency interview questions collected in 2021 (all organized into documents), there are a lot of dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations. There are also detailed learning plans and interviews. Questions, etc., friends who need to obtain these content, please add Q Junyang: 547998459

Guess you like

Origin blog.csdn.net/p1830095583/article/details/114877766