41.: Compare Java and javasciprt?
A: In fact, the most important difference between Java and JavaScript is a static language, a dynamic language:
(1) Object-based and object-oriented: Java is a true object-oriented language, and even if you are developing a simple program, you must design the object; JavaScript is a scripting language that can be used to create complex software that is independent of the network and interacts with the user. It is an object-based (object-based) and event-driven (Event-driven) programming language, so it itself provides a very rich internal object for designers to use.
(2) Interpretation and compilation: Java's source code must be compiled before it is executed. JavaScript is an explanatory programming language whose source code does not need to be compiled and interpreted by the browser. (Almost all current browsers use JIT (Instant compilation) technology to improve the efficiency of JavaScript)
(3) Strongly typed variables and weakly typed variables: Java uses strongly typed variables to check that all variables must be declared before they are compiled, that the variables in JavaScript are weakly typed, that they are not even declared before they are used, and that the JavaScript interpreter checks to infer its data type at run time.
(4) Code format is not the same.
What is the difference between 42.Error and exception?
A: ①error represents system-level errors and exceptions that the program does not have to handle, a serious problem in situations where recovery is not impossible but difficult, such as memory overflow, which cannot be expected to be handled by the program;
②exception represents an exception that needs to be captured or handled by a program, which is a design or implementation problem, that is, if the program is running normally, it never happens.
43.try{} has a return statement, then the code immediately following the try finally{} will not be executed, when executed, before or after the return?
A: Execution occurs before the method returns the caller.
The difference between 44.throw and throws, and how to deal with it?
A: (1) Throw: Used to throw the exception object, followed by the exception object, throw is used in the method body;
(2) throws: Used to throw the exception class, followed by the exception class name, you can follow multiple, separated by commas. Throws used in methods.
(3) Exception handling mode: Throw throws, catch try-catch-finally.
(4) When do I define a try and when do I define throws?
①功能内部如果出现异常,如果可以处理,就用try; ②如果内部处理不了,就必须声明出来,让调用者处理。
45. What is the difference between a compile-time exception and a run-time exception?
A: (1) The compile-time exception is thrown inside the function, the function must be declared, or the compilation fails.
声明的原因:是需要调用者对该异常进行处理。必须进行处理,否则无法编译通过;(throws)
(2) Run-time exception if thrown inside a function, no declaration is required on the function.
不声明的原因:不需要调用者处理,运行时异常发生,已经无法再让程序继续运行,所以,不让调用处理的,直接让程序停止,由调用者对代码进行修正。(throw)
46.List, Set, map inherit from collection interface?
Answer: List, set Yes, map is not. Map is a key-value pair mapping container, distinct from the list and set, while the set stores the scattered elements and does not allow repeating elements (the same is true in mathematical collections), and list is a linear structure of the container, suitable for the case of accessing elements by numeric index.
47. Describe the storage performance and characteristics of ArrayList, vectors, and LinkedList.
Answer: (1) ArrayList:
①arraylist the bottom layer is realized by the array, compared with the LinkedList, the query is fast, and the deletion is slow;
The starting capacity of the ②arraylist is 10. When the array needs to grow, the new capacity is obtained as follows: the new capacity = (old capacity)/2+1, which means that each capacity will probably increase by 50%.
(2) Vector:
①vector: The underlying is also implemented through an array, compared to ArrayList, it is synchronous, thread-safe; generally: ArrayList is suitable for single threaded, Vector is suitable for multithreading.
The capacity growth of ②vector is related to "growth coefficient", if "growth factor" is specified and "Growth factor is valid (i.e., greater than 0)", then "new capacity" = "raw capacity + growth factor" when the capacity is insufficient each time. If the growth factor is invalid (that is, less than/equal to 0), then "new capacity" = "raw capacity x 2".
(3) LinkedList:
①linkedlist Bottom is through the chain list, compared with ArrayList, query slow, and delete quickly;
②linkedlist when adding a new element, it first finds the position in the doubly linked list where the node is to be inserted, index, and then inserts a new node after it is found. At the same time, the bidirectional list finds the node at the index position with an acceleration action: if the index < bidirectional list length is 1/2, it is searched from front to back;
48. Brief introduction to the collection architecture system?
What is the difference between 49.Collection and collections?
A: Collection is an interface, which is the parent interface of the set, list and other containers; collections is a tool class that provides a series of static methods to assist container operations, including searching for containers, sorting, threading, and so on.
50.List, MAP, set three interface access elements, what are the characteristics of each?
A: (1) The list accesses elements in a specific index and can have duplicate elements. Set cannot hold repeating elements (using the Equals () method of the object to distinguish whether the element is duplicated).
(2) Map holds the key-value pair (Key-value pair) mapping, and the mapping relationship can be one-to-many or a-to.
(3) Both the set and map containers have two implementations based on the hash store and the sorting tree, and the hash-based version theory accesses the time complexity to O (1), while the implementation based on the sort tree version will sort and remove the elements by the key (key) of the element or element when they are inserted or removed to achieve the sorting and deduplication effect.
how do 51.TreeMap and TreeSet compare elements when sorting? How does the sort () method in the Collections tool class compare elements?
A: (1) TreeSet requires that the class to which the object belongs must implement the comparable interface, which provides the CompareTo () method of the comparison element, which is called back when the element is inserted and the method compares the size of the element.
(2) TreeMap required to store the key value pair mapped keys must implement the comparable interface to sort the elements according to the key.
(3) The sort method of the collections tool class has two overloaded forms, the first one requiring the object to be placed in the incoming sorting container to implement the comparable interface to achieve the comparison of the elements; the second non-mandatory requirement that the elements in the container must be comparable, but require the second argument to be passed , the parameter is a subtype of the comparator interface (which needs to override the Compare method implementation element comparison), which is equivalent to a temporary defined collation, which is actually an algorithm that injects the size of the comparison element through the interface, and is also the application of the callback pattern (functional programming in Java).
52. What is the difference between wait () and sleep () for the thread class?
A: (1) The class is different: Wait () is the method in the object class; Sleep () is a method in the tread class;
(2) Slepp () does not release the sync lock, and wait () releases the sync lock;
(3) Slepp () must make time, while wait () is not used;
(4) Slepp () can be used anywhere, while wait (), notify (), Notifyall () can only be used in synchronous or synchronous code blocks;
(5) Slepp () must catch exceptions, while Wait (), notify (), Notifyall () do not.
53. What is the difference between the sleep () method of a thread and the yield () method?
A: The ①sleep () method gives other threads the opportunity to run without taking into account the priority of the thread, thus giving the low-priority thread a chance to run, and the yield () method only gives the same priority or higher priority thread the opportunity to run;
The ② thread executes the sleep () method and then goes into a blocking (blocked) state, and the yield () method is transferred to the ready State;
The ③sleep () method declaration throws interruptedexception, and the yield () method does not declare any exceptions;
The ④sleep () method is more portable than the yield () method, which is related to operating system CPU scheduling.
54. When a thread enters synchronized method A of an object, can other threads enter Synchronized method B for this object?
Answer: No. Other threads can only access the non-synchronous method of the object, and the synchronization method cannot enter. Because the synchronized modifier on a non-static method requires that the method be executed to obtain the lock on the object, if it has entered a method to indicate that the object lock has been taken away, then the thread attempting to enter the B method can only wait for the lock of the object in the lock pool (note not waiting for the pool OH).
55. Describe the five states of a thread?
A: (1) NEW: When a thread is in the new state, it is simply an empty thread object and the system does not allocate resources for it. Tread t = new Tread (new Runner ());
(2) Ready (runable): At this point the thread is in a state that can run at any time, and at any subsequent time, it may go into a running state. T.star ();
(3) Run (Running): The thread in this state consumes the CPU and executes the program code.
(4) Blocking (Blocked): The blocking state refers to a thread that abandons the CPU for some reason and temporarily stops running until the thread is back in the ready state. Wait, sleep, sync Lock is occupied;
(5) Death (Dead): When the thread exits the run () method, it goes to the death state and ends the thread's life cycle. The run () method may exit normally, or an exception may be encountered.
56. What are the two ways to create a multi-threaded phase difference?
A: (1) The first way: Inherit the thread class, overriding the Run method by subclasses. The steps are as follows:
① definition class inherits the thread class;
The purpose of ② is to rewrite the Run method, and the code to be executed is stored in the Run method;
③ creates a thread object by creating a subclass object of the thread class;
④ invokes the thread's Start method, opens the thread, and executes the Run method.
(2) The second way: Implement the Runnable interface. The steps are as follows:
The ① definition class implements the Runnable interface.
② the Run method in the overlay interface (used to encapsulate the code that the thread is running).
③ creating thread objects through the thread class;
④ implements the subclass object of the Runnable interface as an actual parameter to the constructor in the thread class.
⑤ invokes the Start method of the Thread object. Open the thread and run the running method in the Runnable interface subclass.
(3) Difference:
① Inherit thread:
Benefits: You can directly use the method in the tread class, the code is simple;
Cons: If the class already has a parent class, this method cannot be used;
② implements the Runnable interface:
Benefits: You can avoid the limitations of single inheritance, even if your own thread class has a parent class does not matter;
Cons: Cannot directly use the method in the tread class, to get the thread object first, the code is complex.
How to use 57.synchronized keywords?
A: The Synchronized keyword can mark an object or method as synchronous to achieve mutually exclusive access to objects and methods, and you can use synchronized (object) {...} Defines a synchronous code block, or synchronized the method as a modifier when declaring a method. The use of the Synchronized keyword has been shown in the example in question 60th.
58. Examples of synchronous and asynchronous.
A: If there are critical resources in the system (resources with fewer resources than the number of threads competing for resources), such as the data being written may be read by another thread later, or the data being read may have been written by another thread, Then the data must be accessed synchronously (the exclusive lock in the database operation is the best example). When an application calls a method that takes a long time to execute on an object and does not want the program to wait for the method to be returned, it should use asynchronous programming, which is often more efficient in many cases with asynchronous approaches. In fact, the so-called synchronization refers to the blocking operation, and asynchronous is non-blocking operation.
59. Does starting a thread call the run () or start () method?
A: Starting a thread is calling the start () method so that the virtual processor represented by the thread is in a running state, which means it can be dispatched and executed by the JVM, which does not mean that the thread will run immediately. The run () method is the method by which a callback (callback) is to be made after the thread starts.
60. What is the thread pool (thread pool)?
A: In object-oriented programming, it takes time to create and destroy objects because you create an object to acquire memory resources or other resources. More so in Java, virtual machines will attempt to track each object so that it can be garbage collected after the object is destroyed. Therefore, one of the means to improve the efficiency of the service process is to minimize the number of objects created and destroyed, especially the resource-intensive object creation and destruction, which is the cause of "pooling resource" technology. The thread pool, as its name implies, creates a number of executable threads in a pool (container), takes the threads out of the pool without having to create them, uses them without destroying the threads and puts them back into the pool, reducing the overhead of creating and destroying the thread objects.
"Java seconds technology seconds kill interviewer" Javase common Face question (iii)