As a Java programmer who is often asked in an interview, let me summarize some of the questions that I often don't ask. __java

Source: Internet
Author: User
Tags finally block
As a Java programmer who is often asked in an interview, let me summarize some of the questions that I often don't ask.






The difference between 1.HashMap and Hashtable


Often asked. Both are classes of the map interface, which enable you to map a unique key to a specific value.


HashMap class is not sorted and can have a null key and multiple null values


Hashtable, you cannot have null keys and null values. Slower than hash, because he is synchronized.


What's the difference between 2.abstract class and interface?


Often ask. A class that declares the existence of a method without implementing it is called an abstract class (abstract class), which is used to create a class that embodies certain basic behaviors and declares a method for that class, but it cannot implement the class in that class. Cannot create an instance of an abstract class. However, you can create a variable whose type is an abstract class and have it point to an instance of a specific subclass. Cannot have an abstract constructor or an abstract static method. Subclasses of an abstract class provide implementations for all abstract methods in their parent class, otherwise they are also abstract classes. Instead, implement the method in the subclass. Other classes that know their behavior can implement these methods in the class.


Interface (interface) is a variant of an abstract class. In an interface, all methods are abstract. Multiple inheritance can be achieved by implementing such an interface. All the methods in the interface are abstract, and none have a program body. Interfaces can only define static final member variables. The implementation of an interface is similar to a subclass, except that the implementation class cannot inherit the behavior from the interface definition. When a class implements a special interface, it defines the method (which is given to the program body) of all such interfaces. It can then invoke the method of the interface on any object of the class that implements the interface. Because of an abstract class, it allows you to use the interface name as the type of the reference variable. The usual dynamic binder will be in effect. A reference can be converted to an interface type or transformed from an interface type, and the instanceof operator can be used to determine whether a class of an object implements an interface.






3.Set elements can not be repeated, then what is the method to distinguish between repetition or not? Is it the difference between = = or equals ()?


A very basic problem.


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.


Equals () and = = methods Determine whether the reference value points to the same object


Equals () is overridden in the class to return a true value when the contents and types of two separate objects match.


The difference between 4.Overload and override. Can the overloaded method change the type of the return value?


Often ask. 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.






5. Talk about final, finally, finalize the difference


Most often asked.


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 be overloaded.


Finally, a finally block is provided for exception handling to perform any cleanup operations. If an exception is thrown, the matching catch clause executes, and then the control enters the finally block (if any).


The Finalize method name. 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.






6. Frequently seen runtime exception


This is a test of your experience in programming.


ClassCastException class conversion exception, when the type conversion.


Nosuchelementexception can't find a way


NullPointerException most common, null-value needles avoid nullpointerexception techniques and best practices in Java programs


Profiledataexception, ProviderException, Rasterformatexception, SecurityException, SystemException, Undeclaredthrowableexception, Unmodifiablesetexception, wait.
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.