1. What is a Java virtual machine? Why is Java called a "platform-agnostic programming language"?
A Java Virtual machine is a virtual machine process that can execute Java bytecode. The Java source file is compiled into a bytecode file that can be executed by the Java virtual machine.
Java is designed to allow applications to run on arbitrary platforms without requiring programmers to rewrite or recompile each platform individually. The Java Virtual machine makes this possible because it knows the instruction length and other features of the underlying hardware platform.
What is the difference between 2.JDK and JRE?
The Java Runtime Environment (JRE) is the Java virtual machine that will execute the Java program. It also contains the browser plug-in needed to execute the applet. The Java Development Kit (JDK) is a complete Java software development package that includes a JRE, a compiler, and other tools (such as the Javadoc,java debugger) that enable developers to develop, compile, and execute Java applications.
3. What does the "static" keyword mean? Is it possible to overwrite (override) a private or static method in Java?
The "static" keyword indicates that a member variable or a member method can be accessed without an instance variable of the class to which it belongs.
The static method in Java cannot be overridden because method overrides are dynamically bound based on the runtime, while the static method is statically bound at compile time. The static method is not relevant to any instances of the class, so it is conceptually not applicable.
4. Can I access non-static variables in the static environment?
The static variable belongs to the class in Java, and it has the same value in all instances. Static variables are initialized when the class is airborne into Java virtual. If your code tries to access a non-static variable without an instance, the compiler will give an error because the variables have not been created yet and are not associated with any instances.
What are the data types supported by 5.Java? What is auto-unboxing?
The 8 basic data types supported in 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.
9. What is the difference between an interface and an abstract class?
Java provides and supports the creation of abstract classes and interfaces. Their implementations have something in common, and the difference is that:
- All methods in an interface are implicitly abstract. Abstract classes can contain both abstract and non-abstract methods.
- A class can implement many interfaces, but only one abstract class is inherited
- Class if you want to implement an interface, it must implement all the methods that the interface declares. However, a class can not implement all methods of an abstract class declaration, 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.
10. What is value passing and reference passing?
An object is passed by value, which means that a copy of the object is passed. Therefore, even if you change the object copy, the value of the source object is not affected.
object is passed by reference, meaning that it is not the actual object, but the reference to the object. Therefore, changes made externally to the referenced object are reflected on all objects.
12. How do I create threads in several different ways? Which one do you like? Why?
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
Implementing the Runnable interface is more popular because this does not require inheriting the thread class. This requires multiple inheritance (while Java does not support multiple inheritance) in cases where other objects have been inherited in the design of the application, and only interfaces can be implemented. At the same time, the thread pool is very efficient and easy to implement and use.
New Thread (new Runable () {public void run () {}}). Start ()//or new Thread (runable). Start (); Runable runable = new Runable () {public void run () {}};
13. A summary explanation of the various available states of the thread.
Threads can be in the following States during execution:
- Ready (Runnable): The thread is ready to run, not necessarily immediately start execution.
- Running (Running): The code of the thread that the process is executing.
- Waiting (Waiting): The thread is in a blocked state, waiting for the external processing to end.
- Sleep (sleeping): The thread is forced to sleep.
- I/O blocking (Blocked on I/O): Waits for I/O operation to complete.
- Synchronous blocking (Blocked on synchronization): Waits for a lock to be acquired.
- Death (Dead): The thread completed execution.
14. What is the difference between a synchronization method and a synchronized code block?
In the Java language, each object has a lock. A thread can use the Synchronized keyword to obtain a lock on an object. The Synchronized keyword can be applied at the method level (coarse-grained locks) or at the code block level (fine-grained locks).
(1) Synchronization method is the method of synchronized keyword modification. Because every object in Java has a built-in lock,, built-in locks protect the entire method when the method is modified with this keyword. You need to get the built-in lock before calling the method, otherwise it will be in a blocking state. code such as: public synchronized void Save () {} NOTE: The Synchronized keyword can also modify a static method, At this point, if the static method is called, the entire class (2) synchronous code block The statement block with the Synchronized keyword adornment will be locked. The statement block modified by this keyword is automatically added with a built-in lock to implement synchronization code such as: synchronized (object) { } Note: Synchronization is a high-overhead operation, so you should minimize the content of synchronization. There is usually no need to synchronize the entire method, using the synchronized code block to synchronize the key code. (3) using special domain variables (volatile) to implement thread synchronization a.volatile keyword provides a lock-free mechanism for access to domain variables, B. Using a volatile modifier domain is equivalent to telling a virtual machine that the domain might be updated by another thread, C. Therefore, each time the domain is used, it is recalculated instead of using the value in the Register D.volatile does not provide any atomic manipulation, nor can it be used to modify the final type of variable (4) thread synchronization using local variables If you use threadlocal to manage variables, Each thread that uses the variable gets a copy of the variable, replicas are independent of each other so that each thread can modify its own copy of the variable without affecting other threads. Common methods for ThreadLocal classes ThreadLocal (): Create a thread local variable get (): Returns the value in the current thread copy of this thread's local variable initialvalue (): Return this line Initial value set (T value) of the current thread of the local variable: sets the value in the current thread copy of this thread's local variable to value
15. How does thread synchronization work inside the monitor? What level of synchronization should the program do?
Monitors and locks are used in Java virtual machines. The monitor monitors a block of synchronization code to ensure that only one thread executes the synchronized code block at a time. Each monitor is associated with an object reference. The thread does not allow synchronization code to be executed until the lock is acquired.
16. What is a deadlock (deadlock)?
A deadlock occurs when two processes are waiting for the other party to execute until execution is complete. As a result, two processes are trapped in infinite waiting.
17. How do I ensure 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.
Java collection classes
What are the basic interfaces of the 18.Java collection class framework?
The Java Collection class provides a set of well-designed interfaces and classes that support the manipulation of a set of objects. The most basic interfaces within the Java Collection class are:
- Collection: Represents a set of objects, each of which is its child element.
- Set: A collection that does not contain duplicate elements.
- List: A sequence of collection, and can contain repeating elements.
- Map: You can map a key (key) to an object with a value, and the key cannot be duplicated.
19. Why does the collection class not implement the cloneable and serializable interfaces?
The collection class interface specifies a set of objects called elements. Each specific implementation class of the collection class interface can optionally save and sort the elements in its own way. Some collection classes allow duplicate keys, some are not allowed.
The semantics and meanings of cloning (cloning) or serialization (serialization) are related to specific implementations. Therefore, the specific implementation of the collection class should determine how to be cloned or serialized.
20. What is an iterator (Iterator)?
The iterator interface provides many ways to iterate over a collection element. Each collection class contains an instance of the iterator that can be returned
Iterative methods. Iterators can delete elements of the underlying collection during the iteration.
What is the difference between 21.Iterator and listiterator?
Their differences are listed below:
- 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.
22. 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.
How does the HashMap in 23.Java work?
HashMap in Java is a key-value pair (Key-value) that stores elements. HashMap requires a hash function that uses the hashcode () and Equals () methods to add and retrieve elements to the collection/collection. When the put () method is called, HashMap calculates the hash value of the key and stores the key-value pair on the appropriate index in the collection. If the key already exists, value is updated to the new value. Some of the important features of HashMap are its capacity (capacity), load factor (payload factor) and expansion limit (threshold resizing).
Where does the importance of the 24.hashCode () and Equals () methods manifest?
HashMap in Java uses the hashcode () and Equals () methods to determine the index of key-value pairs, which are used when the values are obtained by key. If these two methods are not implemented correctly, two different keys may have the same hash value, and therefore may be considered equal by the collection. Moreover, these two methods are also used to discover duplicate elements. So the realization of these two methods is very important to the accuracy and correctness of hashmap.
What is the difference between 25.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.
26. What is the difference between arrays (array) and list (ArrayList)? When should I use array instead of ArrayList?
The different points of the array and ArrayList are listed below:
- An array can contain primitive types and object types, and ArrayList can only contain object types.
- The array size is fixed, and the size of the ArrayList is dynamically changing.
- ArrayList provides more methods and features, such as AddAll (), RemoveAll (), iterator (), and so on.
- For basic type data, the collection uses automatic boxing to reduce the coding effort. However, this approach is relatively slow when dealing with fixed-size base data types.
What is the difference between 27.ArrayList and LinkedList?
ArrayList and LinkedList all implement the list interface, they have the following different points:
ArrayList is an index-based data interface, and its underlying is an array. It can randomly access elements with an O (1) time complexity. In contrast, LinkedList stores its data as a list of elements, with each element linked to its previous and subsequent elements, in which case the time complexity of finding an element is O (n).
It is faster to insert, add, and delete than Arraylist,linkedlist, because when an element is added anywhere in the collection, it is not necessary to recalculate the size as an array or to update the index.
LinkedList accounts for more memory than ArrayList because LinkedList stores two references for each node, one to the previous element, and one to the next.
You can also refer to ArrayList vs. LinkedList.
What are the 28.Comparable and comparator interfaces for? 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.
Java face question