Five questions per day (Java Basics)

Source: Internet
Author: User

The difference between HashMap and Hashtable.

For:
HashMap is a lightweight implementation of Hashtable (non-thread-safe implementation), they all complete the map interface, the main difference is that the HASHMAP allows null (NULL) key value (key), because of non-thread security, the efficiency may be higher than Hashtable.
HashMap allows NULL to be used as a entry key or value, and Hashtable is not allowed.
HashMap hashtable contains method removed, changed to Containsvalue and ContainsKey. Because the contains method is easy to cause misunderstanding.
Hashtable inherits from the dictionary class, and HashMap is an implementation of the map interface introduced by Java1.2.
The biggest difference is that the Hashtable method is synchronize, and HashMap is not, when multiple threads access Hashtable, they do not need to synchronize their methods, and HashMap must provide external synchronization.
The Hash/rehash algorithms used by Hashtable and HashMap are probably the same, so there is no big difference in performance.

the points of knowledge involved in the above answer are:

1, Dictionary class, for the abstract class, used to store key value pairs, the role and map similar. (This class is obsolete).
2, thread safety. This problem is also related to thread safety, and all about thread safety can be seen in the thread safety knowledge points mentioned in my previous blog post.

Second, final, finally, finalize the difference.

For:
final is used to declare properties, methods, and classes, respectively, that the property is immutable, that the method is not overridden, and that the class is not inheritable.
finally is part of the exception-handling statement structure, which indicates that it is always executed.
Finalize is a method of the object class that, when executed by the garbage collector, calls this method of the reclaimed object, and can override this method to provide garbage collection of other resource recycles, such as closing the file.

the points of knowledge involved in the above answer are:

1. Is there a finalize () method for each class? Why?
A: There is this method. Because Finalize () belongs to the object class, all classes have this method, and any subclass of object can override (override) The method, freeing the system resources or doing other cleanup work, such as shutting down the input and output stream.

What is the difference between sleep () and wait ()?

For:
Sleep is a threading class (thread) method that causes this thread to pause execution for a specified time, giving the execution opportunity to other threads, but the monitoring state remains and is automatically restored when it is finished. Calling sleep does not release the object lock.
Wait is a method of the object class that calls the wait method on this object to cause this thread to discard the object lock, enter the waiting lock pool waiting for this object, and only after the Notify method (or Notifyall) is issued for this object the thread enters the object lock pool ready to get the object lock into the running state.



Iv. the difference between overload and override. Can the overloaded method change the type of the return value?

For:
The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism. Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading is a representation of polymorphism in a class. If you define a method in a subclass that has the same name and arguments 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 called, and for it the definition in the parent class is "masked". If more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types, which is called a method overload (overloading). The overloaded method is to change the type of the return value.

V. What is the difference between error and exception?

For:
Error indicates a serious problem in situations where recovery is not impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such situations.
Exception represents a design or implementation issue. That is, it means that if the program runs normally, it never happens.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Five questions per day (Java Basics)

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.