Explore the most common ten interview questions in Java

Source: Internet
Author: User
Tags finally block

First, let's talk about the differences between final, finally, and finalize.
Final? Modifier (keyword) If a class is declared as final, it means that it cannot generate a new subclass and cannot be inherited as a parent class. Therefore, a class cannot be declared both abstract and final. Declare variables or methods as final to ensure that they are not changed during use. Variables declared as final must be declared with an initial value, which can only be read and cannot be modified in future references. Methods declared as final can only be used and cannot be reloaded.
Finally? Finally blocks are provided for troubleshooting. If an exception is thrown, the matched catch clause is executed, and the control enters the finally block (if any ).
Finalize? Method Name. Java technology allows you to use the finalize () method to clear objects from the memory before the Garbage Collector clears them. This method is called by the garbage collector when it determines that this object is not referenced. It is defined in the Object class, so all classes inherit it. Subclass overwrites the finalize () method to sort system resources or perform other cleanup tasks. The finalize () method is called before the Garbage Collector deletes an object.

Second, the difference between HashMap and Hashtable.
All belong to the Map interface class, which maps the unique key to a specific value.
The HashMap class is not classified or sorted. It allows a null key and multiple null values.
Hashtable is similar to HashMap, but does not allow null keys and null values. It is also slower than HashMap because it is synchronized.

Third, String s = new String ("xyz"); how many String objects are created?
Two objects, one being "xyx" and the other being referenced object s pointing to "xyx.

Fourth, what is the difference between sleep () and wait? Thread favorites
The sleep () method is used to stop a thread for a period of time. After the sleep interval expires, the thread may not resume execution immediately. This is because at that time, other threads may be running and not scheduled to give up execution, unless (a) the thread "wakes up" has a higher priority.
(B) The Running thread is blocked for other reasons.
When wait () is a thread interaction, if the thread sends a wait () call to a synchronization object x, the thread will pause the execution and the called object will enter the waiting state, wait until the wake-up or wait time is reached.

Fifth, short s1 = 1; s1 = s1 + 1; what is the error? Short s1 = 1; s1 + = 1; what is the error?
Short s1 = 1; s1 = s1 + 1; error: s1 is short type, s1 + 1 is int type, cannot be converted to short type explicitly. It can be changed to s1 = (short) (s1 + 1 ). Short s1 = 1; s1 + = 1 is correct.

Sixth, the difference between Overload and Override. Can the Overloaded method change the type of the returned value?
Overriding and Overloading are different manifestations of Java polymorphism. Overwriting Overriding is a manifestation of the polymorphism between the parent class and the Child class. Reloading Overloading is a manifestation of polymorphism in a class. If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). When a subclass object uses this method, it calls the definition in the subclass. For it, the definition in the parent class is "blocked. If multiple methods with the same name are defined in a class, they may have different numbers of parameters or have different parameter types, it is called Overloading ). The Overloaded method can change the type of the returned value.

7. The elements in the Set cannot be repeated. How can we identify whether the elements are repeated or not? Is = or equals () used ()? What are their differences?
The elements in the Set cannot be repeated, so the iterator () method is used to identify whether the elements are repeated or not. Equals () is used to determine whether two sets are equal.
The equals () and = Methods Determine whether the reference value points to the same object equals () is overwritten in the class, in order to return the true value when the content and type of the two separated objects match.

Eighth, what is the difference between error and exception?
Error indicates that recovery is not a serious problem that is impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such a situation.
Exception indicates a design or implementation problem. That is to say, it indicates that if the program runs normally, it will never happen.

Ninth, give me the most common runtime exception.
ArithmeticException, ArrayStoreException, except, failed, CannotRedoException, CannotUndoException, ClassCastException, cmcmexception, ConcurrentModificationException, DOMException, EmptyStackException, failed, IllegalStateException,
ImagingOpException, failed, MissingResourceException, failed, failed, NullPointerException, ProfileDataException, ProviderException, RasterFormatException, SecurityException, SystemException, UndeclaredThrowableException, UnmodifiableSetException, UnsupportedOperationException

10. The elements in the Set cannot be repeated. How can we distinguish whether the elements are repeated or not? Is = or equals () used ()? What are their differences?
The elements in the Set cannot be repeated, so the iterator () method is used to identify whether the elements are repeated or not. Equals () is used to determine whether two sets are equal.
The equals () and = Methods Determine whether the reference value points to the same object equals () is overwritten in the class, in order to return the true value when the content and type of the two separated objects match.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.