Explore the most common 10-way questions in Java

Source: Internet
Author: User
Tags finally block repetition sleep

First, talk about final, finally, finalize the difference.
final modifier (keyword) If a class is declared final, it means that it cannot derive a new subclass and cannot be inherited as a parent class. Thus a class cannot be declared as abstract and declared final. Declaring a variable or method final can guarantee that they will not be changed in use. A variable declared as final must be given an initial value at the time of declaration, and can only be read and not modified in future references. A method that is declared final is also used only and cannot overload
Finally? Provides a finally block to perform any cleanup operations when the exception is processed. If an exception is thrown, the matching catch clause executes, and then the control enters the finally block (if any).
Finalize? The name of the method. Java technology allows you to use the Finalize () method to do the necessary cleanup before the garbage collector clears the object out of memory. This method is invoked by the garbage collector on this object when it is determined that the object is not referenced. It is defined in the object class, so all of the classes inherit it. Subclasses override the Finalize () method to defragment system resources or perform other cleanup work. The Finalize () method is invoked on this object before the garbage collector deletes the object.

Second, the difference between HashMap and Hashtable.
are classes that are part of the map interface, which enables you to map unique keys to specific values.
HASHMAP classes are not categorized 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's also slower than HASHMAP, because it's synchronized.

Third, string s = new string ("XYZ"), several string Object created?
two objects, one is "XyX", and one is the reference object s that points to "XyX".

Four, what is the difference between sleep () and wait ()? Thread-loving favorite
The Sleep () method is a method that causes a thread to stop for a period of time. After the sleep interval expires, the thread does not necessarily resume execution immediately. This is because at that point, other threads may be running and are not scheduled to abort execution unless (a) the "waking" thread has a higher priority
(b) The running thread is blocked for other reasons.
When wait () is a thread interaction, if a thread makes a waiting () call to a synchronization object x, the thread suspends execution and the object is queued until it wakes up or waits.

V, short s1 = 1; S1 = s1 + 1; what's wrong? Short S1 = 1; S1 + 1; what's wrong?
short S1 = 1; S1 = s1 + 1; wrong, S1 is short, s1+1 is int and cannot be explicitly converted to short type. Can be modified to S1 = (short) (S1 + 1). Short S1 = 1; S1 = 1 correct.

VI, the difference between overload and override. Can the overloaded method change the type of the return value?
the overridden overriding and overloaded overloading of methods are different manifestations of Java polymorphism. Overriding overriding is a manifestation of polymorphism between parent classes and subclasses, and overload overloading is an expression of polymorphism in a class. If you define a method in a subclass with the same name and parameters as its parent class, we say that the method is overridden (overriding). When an object of a subclass uses this method, the definition in the subclass is invoked, and the definition in the parent class is "masked". If multiple methods with the same name are defined in a class, either with a different number of parameters or with different parameter types, they are called overloads of the Method (overloading). The overloaded method is to change the type of the return value.

Seven, the set elements can not be repeated, then what is the method to distinguish between repetition or not? Is it with = = or equals ()? What's the difference between them?
the elements in set cannot be duplicated, so the iterator () method is used to distinguish between duplicates. Equals () is to interpret whether two sets are equal.
The Equals () and = = methods Determine whether the reference value points to the same object equals () is overridden in the class in order to return true values when the contents and types of the two detached objects match.

What's the difference between error and exception?
error indicates that recovery is not an impossible but difficult situation with a serious problem. For example, memory overflow. It is impossible to expect the procedure to handle such a situation.
Exception represents a design or implementation problem. That is, it means that if the program is working correctly, it will never happen.

Ninth, give me one of your most common runtime exception.
ArithmeticException, Arraystoreexception, Bufferoverflowexception, Bufferunderflowexception, Cannotredoexception, Cannotundoexception, ClassCastException, Cmmexception, Concurrentmodificationexception, Domexception, Emptystackexception, IllegalArgumentException, Illegalmonitorstateexception, Illegalpathstateexception, IllegalStateException,
Imagingopexception, Indexoutofboundsexception, MissingResourceException, Negativearraysizeexception, Nosuchelementexception, NullPointerException, Profiledataexception, ProviderException, RasterFormatException, SecurityException, SystemException, Undeclaredthrowableexception, Unmodifiablesetexception, Unsupportedoperationexception

Tenth, the elements in set can not be repeated, then what is the method to distinguish between repetition or not? Is it with = = or equals ()? What's the difference between them?
the elements in set cannot be duplicated, so the iterator () method is used to distinguish between duplicates. Equals () is to interpret whether two sets are equal.
The Equals () and = = methods Determine whether the reference value points to the same object equals () is overridden in the class in order to return true values when the contents and types of the two detached 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.