Java common classic interview questions (two)

Java common classic interview questions (two)

11. How to use the difference between final, finally and finalize?
Answer: Difference: final is a keyword in Java that can be used to modify classes, methods, and variables (member variables or local variables), while finally is part of the exception handling statement structure, which means it is always executed. Finalize is a method of the Object class. When the garbage collector is executed, this method of the recycled object will be called for other resource recovery during garbage collection, such as closing files.
The use of final:
1. Modified class: When final When modifying a class, it indicates that the class cannot be inherited by other classes. All member methods in the final class are implicitly defined as final methods
. 2. Modification method: lock the method to prevent the inherited class from changing it. That is, this method cannot be rewritten
3. Modified variables: final member variables represent constants and can only be assigned once, and their value will not change after assignment.
The use of finally: used in a try/catch statement with a finally statement block, which means that this statement will always be executed in the end (regardless of whether an exception is thrown), and is often used in situations where resources need to be released, but pay attention The finally statement block will not necessarily be executed (when there is a return in try/catch, it will not be executed)
. The use of finalize: finalize() method is called when the GC cleans up its subordinate objects, if it is thrown in the process of executing it An uncaught exception occurs (uncaught exception, the GC will terminate the cleanup of the changed object, and the exception will be ignored; until the next time the GC starts to clean the object, its finalize() will be called again.

12. What is the difference between local variables and member variables?
Answer: 1. From the perspective of grammatical form: member variables belong to the class, while local variables are variables defined in the method or parameters of the method; member variables can be modified by modifiers such as public, private, static, etc. Local variables cannot be modified by access control modifiers and static; however, member variables and local variables can be modified by final.
2. If a member variable is not assigned an initial value: it will automatically be assigned with the default value of the type (an exception:
member variablesmodified by finalmust also be assigned explicitly), but local variables will not be automatically assigned Assignment.
3. From the perspective of the storage time of variables in memory: member variables are a part of the object, and they exist with the creation of the object, but the local variables disappear automatically with the call of the method.
4. From the perspective of the storage method of the variable in the memory: if the member variable is decorated with static, then the member variable belongs to the class, if it is not decorated with static, the member variable belongs to the instance. The object is stored in the heap memory. If the local variable type is a basic data type, it is stored in the stack memory. If it is a reference data type, it stores a reference to a heap memory object or an address in a constant pool.

13. What is the difference between a character constant and a string constant?
Answer: It is divided into three aspects
: 1. Formally: a character constant is a character surrounded by single quotation marks; a string constant is a character surrounded by double quotation marks
2. Meaning Top: A character constant is equivalent to an integer value (ASCII value), which can participate in expression calculations; a character string constant represents an address value (the character string is stored in the memory location)
3. Occupies a small amount of memory: a character constant only Occupies 2 bytes; string constants occupy some bytes (note: char occupies two bytes in Java)

14. Can the constructor be overridden?
Answer: Constructor cannot be overridden, but it can be overloaded, so you can see that there are multiple constructors in a class.

15. What is the difference between overloading and rewriting?
Answer: Reloading is the same method that can be processed differently according to the different input data. Different parameter
rewriting is the same way that the subclass inherits the self class. Method, input data is the same, but when you want to make a different response, you have to override the method

16. Talk about automatic packing and unpacking?
Answer: Boxing: Pack the basic types with their corresponding reference types; Unboxing: Convert the packaging type to the basic data type;

17. What is the difference between value transfer and reference transfer?
Answer: Java is all about passing by reference. For basic data types, passing is a copy of the value, not the value itself. For object types, passing is a reference to an object. When operating parameters in a method, you actually operate on the object pointed to by the reference.

18. The difference between equals and ==?
Answer: == The comparison is the (heap) memory address of the object stored in the variable (stack) memory, which is used to determine whether the addresses of two objects are the same, that is, whether they refer to the same object. The comparison is the pointer operation in the true sense.
1. The comparison is whether the operands at both ends of the operator are the same object.
2. The operands on both sides must be of the same type (it can be between parent and child classes) in order to compile and pass.
3. The comparison is the address. If it is a comparison of specific Arabic numerals, the value is equal to true, such as: int a=10 and long b=10L and double c=10.0 are the same (true), because they are both Point to the heap at address 10.
equals: equals is used to compare whether the contents of two objects are equal. Since all classes are inherited from the java.lang.Object class, they are applicable to all objects. If the method is not covered, the call is still The method in the Object class, but the equals method in Object returns the judgment of ==.
When all comparisons are equal, equals are used and when comparing constants, the constants are written in the front, because the equals object used by the object may be null, it means nothing

19. What is the difference between Hashcode and equals?
Answer: The equals method is used to compare whether the content of the object is equal (after coverage). The hashcode method is only used in the collection. The hashCode is to improve the efficiency of searching in the storage of the hash structure, and has no effect in the linear table. In Java, if the hashcode is the same, the object can be the same or different, but if the hashcode is different, the object must be different.

20. The process of creating an object in the JVM?
Answer: When the virtual machine encounters a new instruction, it will first check whether the parameter of this instruction can locate a symbol reference of a class in the constant pool, and check whether the class of the symbol reference table has been loaded, parsed and initialized. If not, you must first perform the corresponding class loading process. After the class loading check passes, the virtual machine will then allocate memory for the new object.

Guess you like

Origin blog.csdn.net/Z3068236782/article/details/115019587