1. What is the difference between an abstract class and an interface?
1.abstract classin theJavaThe language represents an inheritance relationship, and a class can only use one inheritance relationship at a time. However, a class can implement multipleInterface.
2.in theAbstract classcan have their own data members, or can have non-ABSTARCTthe member method, while theInterfacecan only have static data members that cannot be modified (that is, must beStatic Final, but inInterfacedata members are not defined in general, all member methods areAbstractthe.
3.abstract classand theInterfacereflect the different design concepts. ActuallyAbstract classindicates that the"Is-a"relationship,Interfaceindicates that the"Like-a"relationships.
4. classes that implement abstract classes and interfaces must implement all of these methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface.
5. public static final
6. friendly
7. public,abstract type of.
2. The difference between final,finally,finalize.
(1) final-modifier (keyword) can modify variables, classes and methods
If a class is declared final, it means that it can no longer derive a new subclass and cannot be inherited as a parent class. Therefore, a class cannot be declared abstract and declared final.
Declaring variables or methods as final ensures that they are not changed in use. A variable declared as final must be given an initial value at the time of declaration, and can only be read in subsequent references and cannot be modified.
The method that is declared final is also used only and cannot be overloaded.
(2) 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 the control enters the finally block, if any.
(3) Finalize-method name. Java technology allows the use of the Finalize () method to do the necessary cleanup before the garbage collector clears objects from memory. This method is called by the garbage collector to this object when it determines that the object is not referenced. It is defined in the Object class, so all classes inherit it. Subclasses override the Finalize () method to organize system resources or perform other cleanup work. The Finalize () method is called on the object before the object is deleted by the garbage collector.
3 What is the difference between the equals () method and the "= =" operator? What other methods do we need to rewrite when overriding the equals () method? Why?
(1) "= =" compares the address value of an object in memory, and whether the contents of the stack are the same.
(2) equals () is referenced, compares the contents of the reference type, and the contents of the heap are the same
When overriding the Equals () method, the Hashcode method needs to be overridden so that when compared, the hash value of two objects is compared, and the hash value of different objects is different.
Example of overriding the Equals method:
public boolean equals (Object obj) {
Determine if the usename and Useid of the Usefor class object are the same
if (obj instanceof usefor) {
Usefor usefor= (usefor) obj;
int Id=usefor.getuseid ();
String Name=usefor.getusename ();
if (this. Useid==id&&this. Usename.equals (name))
{
return true;
}
}
3. Briefly describe the String and stringbuffer,ArrayList , and , hashmap and
characteristics and differences of HashTable .
A: (1) The length of a string object cannot be changed once it is defined, and the modification of a string object that already exists is to create a new object and then save the new value, and the string class cannot be inherited. StringBuffer is a mutable object that is not re-established as a string when it is modified. It can only be used to build objects from constructors. In addition, StringBuffer is a thread-safe class.
(2) ArrayList is the implementation of a dynamic array-based data structure for , LinkedList is based on the data structure of the table, both of which can store references to objects. For random access get and set,arraylist better than LinkedList, because linkedlist to move the pointer. Add and remove,linkedlist are the dominant for new and deleted operations because ArrayList is moving the data.
(3) 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, in the case of only one thread access, More efficient 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. HashMap and Hashtable are mainly from three aspects.
I. Historical reasons: Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2
Two. Synchronization: Hashtable is thread-safe, that is, synchronous, and HashMap is a thread program is not secure, not synchronous
Three. Value: Only HashMap can let you use NULL as the key or value of an entry for a table
4. Brief description of the difference between static and non-static members
Static variables are declared using the static modifier, which is created when the class is instantiated and accessed through the class. Not with
Variables declared with the static modifier are called non-static variables, created when an object is instantiated, accessed through an object. One
The same static variable for all instances of a class is the same value, and the same non-static variable of a different instance of the same class can be a different value. Static functions cannot be implemented with non-static members, such as non-static variables, non-static functions, and so on.
Java easy to confuse knowledge points