In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the most common module interview questions in Java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the most common module interview questions in Java"?
seventy-four。 The difference between throw and throws?
Throws is used to declare all the exception information that may be thrown by a method. Throws declares but does not handle the exception, but uploads the exception, and I will give it to whoever calls it. Throw refers to a specific type of exception that is thrown.
seventy-five。 What's the difference between final, finally, and finalize?
Final can modify classes, variables, and methods. The modifier class indicates that the class cannot be inherited, the modifier method indicates that the method cannot be overridden, and the modifier variable indicates that the variable is a constant that cannot be re-assigned.
Finally generally acts in the try-catch code block, when dealing with exceptions, we usually must execute the code method finally code block, indicating that no matter whether there is an exception, the code block will be executed, generally used to store some closed resources of the code.
Finalize is a method, which belongs to a method of the Object class, and the Object class is the parent of all classes. This method is usually called by the garbage collector. When we call the gc () method of System, the garbage collector calls finalize () to collect the garbage.
seventy-six。 Which part of try-catch-finally can be omitted?
A: catch can be omitted
Reason:
To put it more strictly, try is only suitable for handling run-time exceptions, while try+catch is suitable for handling run-time exceptions + normal exceptions. In other words, if you only use try to handle normal exceptions without catch handling, compilation will not pass, because the compiler insists that if you choose to catch a normal exception, you must use catch to display the declaration for further processing. Run-time exceptions are not specified at compile time, so catch can be omitted, and it is understandable for you to add the catch compiler.
In theory, the compiler doesn't like any code and thinks there may be a potential problem, so even if you add try to all the code, the code will only add a layer of skin to the normal operation at run time. But once you add try to a piece of code, you explicitly promise the compiler to catch the exception that may be thrown by the code instead of throwing it up. If it is a normal exception, the compiler requires that it must be caught with catch for further processing; if the run-time exception is caught and then discarded and + finally sweeping, or catch capture is added for further processing.
As for adding finally, it is a "clean-up" process that is carried out regardless of whether an exception is caught or not.
seventy-seven。 In try-catch-finally, if there is return in catch, will finally still execute it?
A: it will be executed, before return.
Code example 1 java java interview question-if there is a return statement in the catch, will the code in the finally still be executed? * / public class FinallyDemo2 {public static void main (String [] args) {System.out.println (getInt ());}
Public static int getInt () {int a = 10; try {System.out.println (a / 0); a = 20;} catch (ArithmeticException e) {a = 30; return a; / * return a when the program is executed at this stage, this is not return a but return 30 This return path forms * but, uh, it finds that there is a finally behind it, so it continues to execute the contents of finally, axi40 * goes back to the previous path again, continues to take return 30, and after the return path is formed, the a here is not a variable, but a constant 30 * /} finally {aq40;}.
/ / return a;}}
Implementation result: 30
Code sample 2:package com.java_02
/ * * java interview question-if there is a return statement in catch, will the code in finally still be executed? * / public class FinallyDemo2 {public static void main (String [] args) {System.out.println (getInt ());}
Public static int getInt () {int a = 10; try {System.out.println (a / 0); a = 20;} catch (ArithmeticException e) {a = 30; return a; / * return a when the program is executed at this stage, this is not return a but return 30 This return path forms * however, it finds that there is a finally behind it, so it continues to execute the contents of finally, axi40 * returns to the previous path again, continues to take return 30, and after the return path is formed, the a here is not a variable, but a constant 30 * /} finally {aq40; return a. / / if so, a return path is re-formed. Since it can only be returned through 1 return, 40} is returned directly here.
/ / return a;}}
Implementation result: 40
seventy-eight。 What are the common exception classes?
NullPointerException: this exception is thrown when the application attempts to access an empty object.
SQLException: provides exceptions about database access errors or other error information.
IndexOutOfBoundsException: indicates that a sort index (such as sorting an array, string, or vector) is out of range.
NumberFormatException: this exception is thrown when the application attempts to convert a string to a numeric type, but the string cannot be converted to the appropriate format.
FileNotFoundException: throws this exception when an attempt to open a file represented by the specified pathname fails.
IOException: throw this exception when some kind of Ithumb O exception occurs. This class is a generic class for exceptions generated by failed or interrupted Istroke O operations.
ClassCastException: this exception is thrown when attempting to cast an object to a subclass that is not an instance.
ArrayStoreException: the exception that is thrown when trying to store an object of the wrong type into an array of objects.
IllegalArgumentException: the exception thrown indicates that an illegal or incorrect parameter was passed to the method.
ArithmeticException: throw this exception when an exception operation condition occurs. For example, when an integer is divided by zero, an instance of this class is thrown.
NegativeArraySizeException: if the application attempts to create an array of negative size, this exception is thrown.
NoSuchMethodException: throw this exception when a particular method cannot be found.
SecurityException: an exception thrown by the security manager that indicates a security violation.
UnsupportedOperationException: throws this exception when the requested operation is not supported.
RuntimeExceptionRuntimeException: superclasses of exceptions that may be thrown during normal operation of the Java virtual machine.
At this point, I believe you have a deeper understanding of "what are the most common module interview questions in Java?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.