1 exception and Error differences in Java
is a subclass of Throwable. RuntimeException inherits from exception.
The error and runtimeexception and their subclasses are not checked for exceptions, and other exceptions are checked.
Exception: An exception that needs to be captured or handled. Used for exceptions that the user program can catch.
Error: Defines an exception that is not expected to be caught by the user program (System crashes, virtual machine errors, insufficient memory, overflow of method call stacks, such as Stackoverflowerror, OutOfMemoryError). System-level errors and exceptions that the program does not have to handle are internal or hardware problems in the Java runtime environment, such as insufficient memory resources, and the program is largely powerless for this kind of error.
Run-time exception: Divisor is 0, error type conversion, array out of bounds, null pointer. (If this happens, it must be the wrong of the program Ape)
Checked exception: To have a try catch or throws declaration.
What is the difference between the 2 finally code block and the Finalize () method?
Note: when there are return,finally in try and catch, it will still be executed.
Finally is executed after the expression operation following return, so the function return value is determined before finally execution. Regardless of the code in Finally, the return value is determined to be the previously saved value.
Finally, it is best not to include return, or the program exits prematurely, the return value is not the return value saved in a try or catch, but the value returned in finally.
Finalize is a method of the object class that is called by a Java virtual machine before the objects are garbage collected.
3 Throw and Thrwos
Throw: Throws an exception in the code. Throws: Indicates that the method cannot handle the exception that is thrown, and that multiple exceptions are separated by commas.
4 finally
Whether or not an exception is thrown, the finally is executed. The main thing is to do something like release resources. For example: I/o buffer, bamboo trousers link.
5 What happens to the exception object when the exception is processed?
The next garbage collection process is recycled.
6 What are the two types of exceptions in Java? What difference do they have?
Checked exception (checked): Captured with Try-catch, or declared on method with throws.
Unchecked exception (unchecked): It is not required to be declared on a method or constructor, and an exception can be propagated outside the method.
7 Static BLOCK: Executes once when the class is loaded, and is commonly used to call out static variables and invoke static methods.
A inherits B,a a = new A ();
B Static code block-static block of code-"B non-static code block-" B constructor-"A non-static code block-" a constructor.
Summary: Static code blocks are always executed first.
A non-static block of code, like a non-static method, is related to an object, but non-static blocks of code are executed before the constructor.
The parent class is a non-static large code block, and after the constructor finishes executing (the parent class is constructed), the non-static code blocks and constructors of the subclasses begin to execute.
Java Interview (III)