1 The difference between throws and throw
Parsing: Throws and throw are two common keywords in exception handling, and it is often easy for novice programmers to understand the roles and differences of throw and throws correctly, indicating that exception handling can be understood more deeply. Throw is used to throw an exception, and if a throw statement is executed, the program throws an exception and enters the exception handling mechanism. Throws is used to declare exceptions, which means that some types of exceptions may occur in this method, and the compiler will force the exception to be handled when this method is called. Many methods in the API use throws to declare exceptions, so when you use these methods the compiler prompts you to handle the exception.
Reference answer: Throw is used to throw an exception in the method body, and throws is used to declare an exception at the method declaration. These two keywords have a certain connection. If an exception is thrown with the throw keyword in a method, it is either immediately captured with the Try/catch statement or declared with throws, or a compilation error will occur. However
Instead of using the throws keyword after the throw keyword is used, any method can be syntactically used directly with the throws keyword, and the abstract method can be used.
2 collection interface with map interface
Parsing: Collection and map are the two basic collection types in the Java collection framework, to distinguish between different collections first starting with collection and map.
Reference answer: Thecollection interface is a traditional collection interface that can store individual objects in. The map interface is a mapping interface, which stores key-value pairs .
3 The difference between a list and a set
Parsing: List and set are the two sub-interfaces of the collection interface and are the two most used collection types. Programmers should be familiar with the basic features of the two interfaces and be able to select the correct collection type more accurately.
Reference to the Answer: List implements a collection of lists,the elements of which are ordered (ordered), and each element corresponds to an index value according to the order in which it is placed, and the collection Harajuku can be returned based on that index value. The set implements a set of non-repeating elements, that is, the elements in the set cannot be duplicated, and duplicate elements cannot be added to the collection .
4 What are the implementations of the list?
Parsing: List is a more collection type, and list is an interface that is used when using the list collection type to implement the class. Each implementation class of the list interface also implements different data structures, each with different characteristics, the most common of which is the list implementation class.
Reference answer: There are several common implementations in the list interface
1 ArrayList: The characteristics of dynamic arrays are realized.
2 LinkedList: The data structure of the linked list is realized, and linkedlist can also be used as a stack or queue.
3 vector: vectors.
4 ArrayList
The function of the vector class ArrayList can be achieved, the difference is that the vector is the current synchronization.
5 How to create a thread object
Parsing: The Java language is capable of supporting multithreaded programming, encapsulating threads into objects of type thread, as long as the thread-type object is created, it is easy to start threads and execute thread bodies. Programmers must first master the common methods of creating threads.
Reference answer: The Java language often uses two methods to create threads. ① creates a subclass of the thread class, overwriting the run method of the thread class in the subclass, implementing the threading body, as long as the child class object is created as a thread object. ② Create an implementation class for the Runable interface, overriding the Run method in the interface in the implementation class to implement the thread's running body. The thread object can be created using the thread (runable) construction method, and the parameter is an instance of the Runable interface implementation class.
6 What is a generic, role?
Parsing: Generics are an added feature in JDK5, and there are a number of generic interfaces, generic classes, and generic methods in the API. Programmers need to understand the role of generics, especially when using a collection framework, which is often required to use generics.
Reference answer: The essence of generics is the parameterized type, which means that the data type is specified as a parameter. Where you want to declare a data type, you can use this parameter instead of specifying a specific type. In this way, it is possible to specify the specific type when it is used, thus realizing the "arbitrariness" of the parameters. The benefit of generics is that type safety can be checked at compile time, and all casts are automatic and implicit, increasing the rate of reuse of the code.
7 Automatic packing? Unpacking?
Parsing: Automatic boxing/unpacking refers to the conversion problem of eight wrapper classes and eight basic types. However. This feature needs to be used with caution because, while seemingly very handy, there is essentially still a need to create objects, perform transformations, and so on, which can degrade performance.
Reference answer: Auto-boxing refers to the ability to directly convert the base data type to the wrapper type , which means that the wrapper type can be converted directly to the base data type , as shown in the following code:
int m=100; Integer im=int N=im;
The appeal code directly assigns m to the wrapper object IM, which is automatically boxed and then directly assigns the IM to the basic data type N, which is the automatic unpacking. Although the code on the surface is simple and convenient, however, in essence, the container still uses new to create the object, while unpacking is still called the method of operation. Therefore, do not use packing unboxing frequently when it is not necessary to reduce performance.
"--java" Interview questions (3)