The interview is each of us to experience the thing, most people and more than once, here to summarize the common face test, so that everyone in the search for work can be more effective.
1 switch can I use string to make arguments?
A. Prior to Java 7, switch can only support Byte,short,char,int or its corresponding wrapper class and Enum type. In Java 7, String support was added.
2 the difference between equals and = =:
a.== is to determine if two variables or instances are pointing to the same memory space equals is to determine whether the value of the memory space pointed to by two variables or instances is the same
What are the common methods for 3 object?
A. Method equals tests whether two objects are equal
B. Method clone for object copy
C. Method GetClass returns the class object associated with the current object
D. Method notify,notifyall,wait are used for thread synchronization of a given object
4 Java Four kinds of references, soft and weak, used to the scene
A. Solving an oom problem with soft and weak references: A hashmap is used to save the path of the picture and the mapping between the soft references associated with the corresponding picture object, and the JVM automatically reclaims the space occupied by these cached picture objects when the memory is low, effectively avoiding the problem of oom
B. Implement caching of Java objects by means of the soft-and-Object-regain method: For example, we create a class for employee, if we need to query an employee's information every time. It takes a lot of time to rebuild an instance if it's just been queried in a few seconds. We can use the combination of soft reference and HASHMAP, first save the reference aspect: a soft reference to the instance of an Employee object reference and save the reference to HashMap, key for this employee's Id,value for this object's soft reference, on the other hand is to take out the reference, Whether there is a soft reference to the employee instance in the cache, if any, obtained from a soft reference. If there is no soft reference, or if the instance obtained from the soft reference is NULL, rebuild an instance and save a soft reference to the new instance
C. Strong references: If an object has a strong reference, it will not be reclaimed by the garbage collector. Even if there is not enough memory space, the JVM does not reclaim it, but instead throws a outofmemoryerror error that causes the program to terminate unexpectedly. If you want to break the association between a strong reference and an object, you can explicitly assign a reference to NULL, so that the JVM reclaims the object at the appropriate time
D. Soft reference: When using soft references, if the memory space is sufficient, soft references can continue to be used without being reclaimed by the garbage collector, and soft references are reclaimed by the garbage collector only when there is insufficient memory.
E.. Weak reference: An object with a weak reference has a more ephemeral life cycle. Because when the JVM is garbage collected, weak references are recycled whenever a weak reference object is found, regardless of whether the current memory space is sufficient. However, because the garbage collector is a low-priority thread, it is not always possible to quickly discover weak reference objects
F. Virtual reference: As the name implies, is the form of a dummy, if an object only holds a virtual reference, then it is equivalent to no reference, at any time may be reclaimed by the garbage collector.
G. Usage Scenarios:
5 The role of Hashcode, what is the difference with equal
A. The same is used to identify whether 2 objects are equal, the Java collection has a list and set two classes, where set does not allow the element to be repeated implementation, the method is not allowed to repeat the implementation, if you use equal to compare, if there are 1000 elements, you new one element out, It is necessary to call 1000 times equal to compare them with each other to see if they are the same object, which will greatly reduce the efficiency. Hashcode is actually the storage address of the returned object, if there is no element in this position, the element is stored directly above, if the element already exists in this position, then call the equal method to compare with the new element, the same will not be saved, hash to the other address
6 The difference between String, StringBuffer and StringBuilder
The main performance difference between the a.string type and the StringBuffer type is actually that String is an immutable object
B.stringbuffer and StringBuilder are implemented by the char[] array.
C.stringbuffer is thread-safe, and StringBuilder is thread insecure
7 The meaning of override and overload to distinguish
A.overload as the name implies is a reload, it can show the polymorphism of the class, can be the function can have the same function name but the parameter name, return value, type cannot be the same, or can change the parameter, type, return value but the function name remains unchanged.
B. Is the meaning of Ride (rewrite), when the subclass inherits the parent class, you can define a method that has the same name and parameters as its parent class, and when the subclass calls the function, the subclass's method is called automatically, and the parent class is equivalent to overwritten (overridden).
8 Differences between abstract classes and interfaces
A. A class can inherit only a single class, but it is possible to implement multiple interfaces
B. The interface emphasizes the implementation of a particular feature, while the abstract class emphasizes the owning relationship
C. All methods in an abstract class are not necessarily abstract, and you can choose to implement some basic methods in an abstract class. And the interface requires that all methods must be abstract.
9 principles and features of several ways to parse xml: DOM, SAX, pull
A.dom: Consume memory: First read the XML document into memory, then use the DOM API to access the tree structure and get the data. This is easy to write, but it consumes memory. If the data is too big, mobile phone is not good enough, the phone may crash directly
B.sax: High resolution, memory-intensive, event-driven: more simply, sequential scanning of documents notifies the event handler when the document starts and ends, elements (element) Start and end, document ends, and so on. The event handler acts accordingly, and then continues the same scan until the end of the document.
C.sax: Similar to SAX and event-driven, we can call its next () method to get the next parse event (that is, start document, end document, start tag, end tag), When you are in an element, you can call Xmlpullparser's Getattributte () method to get the value of the property, or you can call its nexttext () to get the value of this node.
The difference between ten wait () and sleep ()
Sleep comes from the thread class, and wait comes from the object class
During the call to the sleep () method, the thread does not release the object lock. Calling the wait method thread frees the object lock
Sleep does not sell system resources, wait to let system resources other threads can consume CPU
Sleep (milliseconds) needs to specify a sleeping time when the time is automatically awakened
The difference between heap and Stack in Java, the memory mechanism of Java
A. A reference to a base data type that is more than a variable and an object is allocated on the stack
B. Heap memory used to hold objects and arrays created by new
C. class variables (static modified variables), the program allocates memory for class variables in the heap at the time of loading, and memory addresses in the heap are stored on the stack
D. Instance variables: When you use the Java keyword New, the system in the heap is not necessarily contiguous space allocated to the variable, is based on the fragmented heap memory address, the hash algorithm is converted to a long series of numbers to characterize the variable in the heap "physical location", instance variable life cycle – When a reference to an instance variable is lost, it is included in the recyclable "list" by the GC (garbage collector), but the memory in the heap is not released immediately
E. Local variables: By declaring in a method, or a piece of code (such as a For loop), execution to it in the stack to open up memory, when the local variable one but out of scope, memory immediately released
Implementation principle of Java polymorphism
A. In abstract terms, polymorphism means that the same message can be used in a variety of different ways depending on the sending object. (Sending a message is a function call)
B. The principle of implementation is dynamic binding, the method of the program call is dynamically bound in the runtime, tracing the source code can be found that the JVM through the automatic transformation of parameters to find the right way.
JAVA garbage collection mechanism
A. Tag recovery method: Traverse the object graph and record reachable objects to remove unreachable objects, typically using single-threaded work and potentially generating memory fragmentation
B. Tag-Compression recovery method: Earlier and the first method is the same, just one more step, all the surviving objects are compressed to one end of the memory, so that the memory fragments can be synthesized a large number of reusable memory area, improve memory utilization
C. Replication recycling: Divides the existing memory space into two parts, while the GC runs, it copies the reachable object to the other half of the space, and then empties the entire object of the space in use. This method is suitable for short-lived objects, and continuous replication of long-lived objects results in reduced efficiency.
D. Generational recycling: The memory space is divided into two or more domains, such as the younger generation and the old age, the young generation is characterized by the object will be quickly recycled, so in the younger generation use more efficient algorithms. When an object is still alive after several recoveries, the object is put into memory space called old age, and the older generation takes the tag-compression algorithm
E. Reference count (the simplest and oldest method): The process of saving the referenced number of resources (which can be objects, memory, or disk space, and so on) and releasing them when the number of references becomes 0 o'clock
F. Object reference traversal (now used by most JVMs): Object reference traversal starts with a set of objects and recursively determines the reachable (reachable) object along each link on the entire object graph. If an object cannot arrive from one (at least one) of these root objects, it is garbage collected
G. What is a garbage collector: frees memory for objects that are no longer holding references
H. How can I tell if an object needs to be collected?
I. Several garbage collection mechanisms
14 How many kinds of collections are there in Java, and what is the difference?
A.hashtable older, is based on the Dictionary class implementation, HashTable is based on the map interface implementation
B.hashtable is thread-safe, HASHMAP is thread insecure
C.hashmap can let you use NULL as the key or value of an entry for a table
D.arraylist, LinkedList, vector difference: ArrayList and Vector bottom is to use an array to store data, Vector because of the use of the Synchronized method (thread safety) so that performance than ArrayList, LinkedList using a doubly linked list to achieve storage, random access is relatively slow
E.hashmap the underlying source code implementation: When we put the element in the HashMap, we first recalculate the hash value according to the key hashcode, according to the hash is worth the position of the element in the array (that is, subscript), if the array is already stored in the position of other elements, Then the elements in this position will be stored in the form of a linked list, the newly added ones placed in the chain head, the first to join in the end of the chain. If the array has no elements at that position, the element is placed directly at that position in the array.
F.fail-fast mechanism: In the process of using iterators, other threads have modified the map, then the concurrentmodificationexception is thrown, which is called the fail-fast mechanism. The implementation of this mechanism in the source code is through the Modcount domain, modcount as the name implies is the number of changes, the HashMap content will increase this value, then in the iterator initialization process will assign this value to the expectedmodcount of the iterator. In the iterative process, it is judged whether Modcount and expectedmodcount are equal, and if not equal, there are already other threads that have modified the map.
The difference between G.hashmap and HashTable
Common Java Surface question finishing