java basic interview questions

When interviewing an intern today, I found a few basic interview questions:

1. If there is a return statement in try{}, will the code in finally{} following this try be executed, when will it be executed, before or after return?

Answer: It will execute, before the method returns to the caller. It is not good for Java to allow changing the return value in finally, because if there is a finally code block, the return statement in try will not return to the caller immediately, but record the return value and wait until the finally code block is executed before calling it. The user returns its value, and then if the return value is modified in finally, it will cause a lot of trouble to the program. The real situation is that the code executed in the finally code block that changes the return value does take effect, but the return is to execute The return value recorded earlier.

2. Is it possible to inherit the String class?

Answer: The String class is final and cannot be inherited.

3. What are the uses of the final keyword in Java?

Answer: (1) Modified class: Indicates that the class cannot be inherited; (2) Modified method: Indicates that the method cannot be overridden; (3) Modified variable: Indicates that a variable can only be assigned once and its value cannot be modified, which is equivalent to a constant.

4. What is the difference between final, finally, finalize?

Answer: final: The modifier (keyword) has three uses: if a class is declared final, it means that it can no longer derive new subclasses, that is, it cannot be inherited, so it is an antonym to abstract. Declaring variables as final can ensure that they will not be changed during use. Variables declared as final can only be read and cannot be modified after a given initial value. Methods that are declared final can also only be used and cannot be overridden in subclasses. finally: usually placed after try...catch, the code block is always executed, which means that whether the program is executed normally or an exception occurs, the code here can be executed as long as the JVM is not closed, and the code to release external resources can be written in finally in the block. finalize: A method defined in the Object class. In Java, the finalize() method is allowed to do the necessary cleanup work before the garbage collector clears the object from memory. This method is called by the garbage collector when the object is destroyed. By overriding the finalize() method, you can clean up system resources or perform other cleanup work.

5. What is the difference between Error and Exception?

Answer: Error represents system-level errors and exceptions that the program does not have to handle, which is a very serious error problem; such as memory overflow, it is impossible to expect the program to handle such a situation; Exception represents the exception that needs to be caught or needs to be handled by the program. A design or implementation problem; that is, it represents a situation that would never have happened if the program was running correctly.

6. What happens if the static modifier of the main method is removed?

Answer: The program can be compiled normally. A NoSuchMethodError exception will be thrown at runtime. This is because the method modified by the static modifier is an instance object that does not depend on the class and can be accessed directly through the class name, but after the static modifier is removed, the method may not be found.

7. Can a call to a non-static method be issued from within a static method?

Answer: No, static methods can only access static members, because non-static method calls need to create objects first, so objects may not be initialized when non-static methods are called. If you really want to call a non-static method in a static method, you can first instantiate the object where the non-static method is located, and use this object to call the non-static method in it.

8. What is the difference between static variables and instance variables?

Answer: A static variable is a variable modified by the static modifier, also known as a class variable. It belongs to a class and does not belong to any object of the class. No matter how many objects a class creates, there is only one copy of a static variable in memory. ; Instance variables must depend on an instance, you need to create an object and then access it through the object. Static variables allow multiple objects to share memory. In Java development, there are usually a large number of static members in context classes and utility classes.

9. How is Java's "write once, run anywhere" implemented?

Answer: Java programs are compiled into class files consisting of bytecodes, which can run on any platform, so Java is platform independent.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324694758&siteId=291194637