The 8 basic data types supported by the Java language are:
byte short int long float double Boolean char
Auto-Boxing is a conversion of the Java compiler between the base data type and the corresponding object wrapper type. For example: Convert int to integer,double into double, and so on. The reverse is automatic unpacking.
What is the difference between an interface and an abstract class?
All methods in an interface are implicitly abstract. Abstract classes can contain both abstract and non-abstract methods.
Classes can not implement all methods of abstract classes and interface declarations, and of course, in this case, the class must also be declared abstract.
An abstract class can implement an interface without providing an interface method implementation.
The variables declared in the Java interface are final by default. An abstract class can contain non-final variables.
The member functions in the Java interface are public by default. The member functions of an abstract class can be private,protected or public.
An interface is absolutely abstract and cannot be instantiated. An abstract class can also not be instantiated, but it can be called if it contains the main method.
There are three ways to create threads:
Inherit the thread class
Implementing the Runnable Interface
Applications can use the executor framework to create a thread pool
Several available states for a thread
1. New ( new ): A new Thread object was created. 2. ( runnable ): After the thread object is created, other threads (such as the main thread) call the start () method of the object. The thread in the state is in a pool of running threads, waiting for the thread to be dispatched to check for cpu 's use . 3. Run ( running ): A thread with a running status ( runnable ) Gets the cpu time slice ( timeslice ) Execute the program code. 4. blocking ( block ): The blocking state means that the thread has given up cpu use rights for some reason, that is to give up the  CPU timeslice temporarily stop running. Until the thread enters the operational ( runnable ) state, there is a chance to get cpu timeslice again to the run ( running ) state. There are three types of blocking: (a) . wait for blocking: the thread execution o . wait () method of Run ( running ), jvm will Cheng the line Into the wait queue ( waitting queue ). (b) . synchronous blocking: When a thread running ( running ) acquires an object's synchronization lock, if the synchronization lock is occupied by another thread, JVM puts the thread into the lock pool ( lock ). (iii) . other blocking: Run ( running ) thread execution Thread . sleep ( long ms ) or t . join () method, or when a I / O request is made,, jvm will put the thread in a blocking state. When the sleep () status expires, join () waits for the thread to terminate or timeout, or when I/O finishes, the thread is re-transferred to the operational ( runnable ) state. 5. Death ( dead ): Thread run (), main () method execution ends, or an exception exits the run () method, The thread ends the life cycle. The thread of death cannot be resurrected again.
How do you make sure that n threads can access n resources without causing a deadlock?
When using multithreading, a very simple way to avoid deadlocks is to specify the order in which locks are acquired and force threads to acquire locks in the order specified. Therefore, if all the threads lock and release the lock in the same order, there will be no deadlock.
What is an iterator (Iterator)?
The iterator interface provides many ways to iterate over a collection element. Each collection class contains an iterative method that can return an iterator instance. Iterators can delete elements of the underlying collection during the iteration, but they cannot call the collection's remove (Object Obj) directly, and can be removed through the Remove () method of the iterator.
What is the difference between iterator and listiterator?
The iterator can be used to traverse the set and list collection, but Listiterator can only traverse the list.
Iterator to a collection can only be forward traversal, listiterator can be either forward or back.
Listiterator implements the iterator interface and includes other functions such as adding elements, replacing elements, getting the index of the previous and subsequent elements, and so on.
What is the difference between fast failure (fail-fast) and security failure (fail-safe)?
Iterator's security failure is based on a copy of the underlying collection, so it is not affected by modifications on the source collection. All of the collection classes below the Java.util package are fast failures, and all classes under the Java.util.concurrent package fail safely. A fast-failing iterator throws an concurrentmodificationexception exception, and a security-failed iterator never throws such an exception.
What's the difference between HashMap and Hashtable?
HashMap and Hashtable both implement the map interface, so many features are very similar. However, they have the following different points:
HashMap allows keys and values to be null, while Hashtable does not allow keys or values to be null.
The Hashtable is synchronous, while HashMap is not. Therefore, HashMap is more suitable for single-threaded environments, while Hashtable is suitable for multithreaded environments.
HashMap provides a collection of keys that can be used for iteration, so HashMap is a quick failure. On the other hand, Hashtable provides an enumeration of keys (enumeration).
Hashtable is generally considered to be a legacy class.
What do the comparable and comparator interfaces do? List the differences between them.
Java provides a comparable interface that contains only one CompareTo () method. This method can be used to sort two objects in a single order. Specifically, it returns a negative number, 0, a positive number to indicate that the input object is less than, equal to, greater than the already existing object.
Java provides a comparator interface that contains two methods of compare () and Equals (). The Compare () method is used to sort two input parameters, return a negative number, 0, and a positive number indicates that the first parameter is less than, equal to, greater than the second argument. The Equals () method requires an object as a parameter that determines whether the input parameters are equal to comparator. This method returns true only if the input parameter is also a comparator and the input parameter is the same as the current comparator's ordering result.
What is the Java priority queue?
Priorityqueue is an unbounded queue based on a priority heap whose elements are sorted in natural order (natural orders). At the time of creation, we can provide it with a comparator that is responsible for ordering the elements. Priorityqueue do not allow null values because they do not have a natural order, or they do not have any associated comparators. Finally, Priorityqueue is not thread-safe, and the time complexity of the queue and the queue is O (log (n)).
What are the best practices for the Java Collection Class framework?
It is important for performance to correctly select the type of collection to use according to the needs of the application, such as: if the size of the element is fixed and can be known beforehand, we should use array instead of ArrayList.
Some collection classes allow you to specify the initial capacity. Therefore, if we can estimate the number of elements stored, we can set the initial capacity to avoid recalculation of hash values or expansion.
For type safety, the reason for readability and robustness is always to use generics. Also, using generics avoids classcastexception at run time.
Using the invariant Class (immutable Class) provided by the JDK as the key to the map avoids implementing the hashcode () and Equals () methods for our own classes.
The interface is better than the implementation when programming.
The underlying collection is actually empty, the return length is a collection of 0 or an array, do not return null.
33 Questions
Java Knowledge points