In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the 10 common interview questions in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the 10 common interview questions in Java".
1. What is a concurrent modification exception?
What is a concurrent modification exception: when we traverse the collection of collection and iterator interfaces (List, Set, Map), we can traverse the index or iterator. When we use the iterator to iterate through the collection, we get the iterative object of the current collection. There are remove methods that encapsulate iterators and remove methods that come with the collection. If we call the remove method of the iterator object, there is no problem, but when we call the remove method of the collection, there will be a ConcurrentModificationException concurrent modification exception. That is, when we traverse the collection through an iterator, we do not allow structural changes in the collection itself.
two。 What is CopyOnWriteArrayList and how is it different from ArrayList?
CopyOnWriteArrayList:CopyOnWriteArrayList is a thread-safe variant of ArrayList, and its principle can be understood as follows: there is only one container during initialization, and often for a period of time, when the data and quantity of this container does not change, everyone (multiple threads) reads the data in the same container assuming that only read operations take place during this period of time, so the data you read is unique, consistent and safe. But later, someone added a piece of data to it. At this time, the principle of CopyOnWriteArrayList's underlying implementation is to first copy a container that can be referred to as a copy, then add the new data to the new container, and finally assign the reference address of the new container to the old container address, but during the period of adding this data, other threads still read the data in the old container. Vector ArrayList CopyOnWriteArrayList these three collection classes all inherit List interface 1, ArrayList is not thread-safe; 2, Vector is relatively old thread-safe, but the performance is not good; 3. CopyOnWriteArrayList not only takes into account thread safety, but also improves concurrency, and its performance is much better than Vector.
3. What is the difference between iterators and enumerations?
In the Java collection, we usually traverse the collection through "Iterator" or "Enumeration". Enumeration is an interface, its source code is as follows: package java.util;public interface Enumeration {boolean hasMoreElements () E nextElement ();} Iterator is also an interface, its source code is as follows: package java.util;public interface Iterator {boolean hasNext (); E next (); void remove ();} difference: 1 function interface is different from Enumeration there are only two function interfaces. With Enumeration, we can only read the data of the collection, not modify the data. Iterator has only three function interfaces. Iterator can not only read the data of the collection, but also delete the data. 2.Iterator supports the fail-fast mechanism, while Enumeration does not. Enumeration is an interface added by JDK 1.0. The functions that use it include classes such as Vector, Hashtable, and so on, which are added in JDK 1.00.The purpose of Enumeration is to provide a traversal interface for them. Enumeration itself does not support synchronization, but synchronization is added when Vector and Hashtable implement Enumeration. Iterator is a new interface added by JDK 1.2. it also provides traversal interfaces for collections such as HashMap, ArrayList, and so on. Iterator supports the fail-fast mechanism: fail-fast events may occur when multiple threads operate on the contents of the same collection. The Java API specification recommends that Iterator take precedence over Enumeration for newer programs because "Iterator replaces Enumeration in the Java collection framework." How does 64.Hashmap synchronize? 1. Use the synchronized keyword, which is also the most primitive method. Synchronized (anObject) {value = map.get (key);} 2. Use the lock Java.util.concurrent.locks.Locklock.lock () provided by JDK1.5; value = map.get (key); lock.unlock (); 3. You can use the read-write lock java.util.concurrent.locks.ReadWriteLockrwlock.readLock (). Lock () provided by JDK1.5; value = map.get (key); rwlock.readLock (). Unlock (); 4. Using the java.util.concurrent.ConcurrentHashMap class provided by JDK1.5, the storage space of Map is divided into several blocks, each with its own lock, which greatly reduces the situation in which multiple threads compete for the same lock value = map.get (key); 1. Asynchrony is indeed the fastest, as expected. 2. Among the four synchronization methods, ConcurrentHashMap is the fastest and is close to the case of non-synchronization. 3. The synchronized keyword is very slow. 4. Read locks that use read-write locks are slightly slower than ordinary ones. 1. If ConcurrentHashMap is sufficient, use ConcurrentHashMap. 2. If you need to implement synchronization by yourself, use the locking mechanism provided by JDK1.5 to avoid using the synchronized keyword.
The difference between 5.IdentityHashMap and HashMap?
The former compares key with "reference equality" and the latter with "object equality", that is, for K1 and K2, IdentityHashMap considers two key equal when k1==k2, while HashMap considers two key equal only when k1.equals (K2) = = true. 2.IdentityHashMap allows null to be used as key and value. There is no guarantee of the order between any Key-value pairs, let alone that their order will not change over time. 3.IdentityHashMap has special uses, such as serialization or deep replication. Or record object proxy. For example, all objects in jvm are unique, even if two objects are the same class object and the data of the two objects are exactly the same, for jvm, they are also completely different. If you want to use a map to record objects in such jvm, you need to use IdentityHashMap instead of other Map implementations.
6. How do I get a date that is the last day of the month?
Import java.util.Calendar;public class Test {public static void main (String [] args) {System.out.println (daysCount (2010, 2));} public static int daysCount (int year, int month) {Calendar cal = Calendar.getInstance (); cal.set (Calendar.YEAR, year); cal.set (Calendar.MONTH, month); cal.set (Calendar.DATE, 0); return cal.get (Calendar.DATE);}}
Will there be a memory leak in 7.java? please briefly describe
The so-called memory leak means that an object or variable that is no longer used by the program has been occupied in memory. There is a garbage collection mechanism in Java, which ensures that when an object is no longer referenced, that is, when the object is programmed as an orphan, the object will be automatically cleared from memory by the garbage collector. Because Java uses directed graph for garbage collection management, it can eliminate the problem of reference loop, for example, there are two objects that reference each other, as long as they and the root process are not reachable, then GC can also recycle them. Memory leaks in java: memory leaks are likely to occur when long-lifecycle objects hold references to short-lifecycle objects. Although short-lifecycle objects are no longer needed, they cannot be recycled because long-lifecycle objects hold its references. This is the scenario where memory leaks occur in java. Generally speaking, programmers may create an object and never use it again. This object has always been referenced, that is, this object is useless but cannot be reclaimed by the garbage collector, which is a situation where memory leaks can occur in java. For example, in a cache system, we load an object and put it in the cache (for example, in a global map object) and then never use it again. This object has been referenced by the cache but is no longer used. Check for memory leaks in java, be sure to let the program execute all kinds of branches to the end of the program, and then see if an object has been used, and if not, it can be determined that the object belongs to a memory leak. If the method of an instance object of an external class returns an instance object of the inner class, the inner class object is referenced for a long time, even if the external class instance object is no longer used, but because the inner class persists the instance object of the external class, the external class object will not be garbage collected, which will also cause memory leaks. Another case of memory leak: when an object is stored in the HashSet collection, the fields in the object that participate in calculating the hash value cannot be modified, otherwise, the modified hash value of the object will be different from the hash value originally stored in the HashSet collection, in which case, even if the contains method uses the object's current reference as a parameter to retrieve the object from the HashSet collection The result that the object cannot be found will also be returned, which will also prevent the current object from being deleted separately from the HashSet collection. What is the mechanism for implementing polymorphism in the memory leak 68.java? Depending on the reference of the parent class or interface to the object of the subclass or implementation class, the method called is the method of the object that is running in memory. There are three necessary conditions for Java to achieve polymorphism: inheritance, rewriting, and upward transformation. Inheritance: there must be subclasses and parents with inherited relationships in polymorphisms. Override: the subclass redefines some methods in the parent class, and when these methods are called, the methods of the subclass are called. Upward transformation: in polymorphism, you need to assign a reference to the subclass to the parent object, so that the reference can have the skill to call the methods of the parent class and the methods of the subclass. Only when the above three conditions are met can we use unified logic implementation code to deal with different objects in the same inheritance structure, thus performing different behaviors. The principle followed by the polymorphic mechanism is that when a superclass object references a variable to a subclass object, the type of the referenced object rather than the type of the referenced variable determines whose member method is called, but the called method must be defined in the superclass, that is, the method overridden by the subclass, but it still confirms the method according to the priority of the method call in the inheritance chain. The priority is: this.method (O), super.method (O), this.method ((super) O), super.method ((super) O).
What is the mechanism for implementing polymorphism in 8.java?
Depending on the reference of the parent class or interface to the object of the subclass or implementation class, the method called is the method of the object that is running in memory. There are three necessary conditions for Java to achieve polymorphism: inheritance, rewriting, and upward transformation. Inheritance: there must be subclasses and parents with inherited relationships in polymorphisms. Override: the subclass redefines some methods in the parent class, and when these methods are called, the methods of the subclass are called. Upward transformation: in polymorphism, you need to assign a reference to the subclass to the parent object, so that the reference can have the skill to call the methods of the parent class and the methods of the subclass. Only when the above three conditions are met can we use unified logic implementation code to deal with different objects in the same inheritance structure, thus performing different behaviors. The principle followed by the polymorphic mechanism is that when a superclass object references a variable to a subclass object, the type of the referenced object rather than the type of the referenced variable determines whose member method is called, but the called method must be defined in the superclass, that is, the method overridden by the subclass, but it still confirms the method according to the priority of the method call in the inheritance chain. The priority is: this.method (O), super.method (O), this.method ((super) O), super.method ((super) O).
9. What is the difference between local variables and member variables?
The difference between member variables and local variables 1, the position of different member variables in the class: local variables outside the method in the class: in the method or code block, or on the declaration of the method 2, the position in memory is different. Member variables: in the heap (static area in the method area) local variables: in the stack 3, life cycle different member variables: exist with the creation of the object Local variables disappear with the disappearance of the object: exist with the call of the method or the execution of the code block, disappear with the end of the method call or the execution of the code block 4. Initial value member variable: local variable with default initial value: no default initial value, need to be assigned before use, otherwise the compiler will report an error
10. What are anonymous classes and what are the benefits?
To put it simply: an anonymous inner class is an inner class without a name. Under what circumstances do you need to use anonymous inner classes? It is appropriate to use an anonymous inner class if the following conditions are met: only one instance of the class is used. Class is used immediately after definition. Naming a class that is very small (less than 4 lines of code is recommended by SUN) does not make your code easier to understand. When using anonymous inner classes, keep the following principles in mind: anonymous inner classes cannot have constructors. Anonymous inner classes cannot define any static members, methods, and classes. Anonymous inner class cannot be public,protected,private,static. Only one instance of an anonymous inner class can be created. An anonymous inner class must be behind new, implicitly implementing an interface or implementing a class. Because the anonymous inner class is a local inner class, all restrictions on the local inner class take effect. Thank you for your reading, the above is the content of "what are the 10 common interview questions in Java". After the study of this article, I believe you have a deeper understanding of what the 10 common interview questions in Java are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.