Java face Test (1)

Source: Internet
Author: User
Tags stringbuffer

1. What is the difference between equals and = =?

  = = Determines whether two variables or instances are pointing to the same memory address

Equals is to determine whether the values in the memory address pointed to by two variables or instances are the same

2. What are the common methods of object?

  Method Equals () tests whether two objects are equal

Method Clone () for object copy

Method GetClass () Returns the class object associated with the current object

Method Notify (), Notifyall (), wait () are used for thread synchronization of a given object.

3, the use of soft reference or weak reference in the actual development of the scene:

  Solving oom problems with soft and weak references: a hashmap to save the path to the picture and the mapping between the corresponding picture objects associated soft references, the JVM automatically reclaims the space occupied by these cached picture objects when the memory is low, effectively avoiding the problem of oom

Implement caching of Java objects by using soft-Reference object overloading methods: For example, we create a class for employee, if we need to query an employee's information every time. Even if the query was just a few seconds ago, it would take a lot of time to rebuild an instance. We can use the combination of soft reference and Haspmap, first save the reference aspect: a soft reference to the instance of an Employee object reference and save the reference to HashMap, key for the employee's Id,value for this object soft reference, on the other hand is to take out the reference, Whether there is a soft reference to the employee instance in the buffer, if any, obtained from the soft reference. If there is no soft reference, or if the instance obtained from the soft reference is NULL, re-create an instance and save a reference to the new instance.

4, the role of hashcode, and equal what is the difference:

  The same is used to determine whether two objects are equal, the Java collection has a list and set two classes, where set does not allow the object to be repeated implementation, this method does not allow the implementation of the repetition, if the equal () method to compare, if there are 1000 elements, a new element, It is necessary to call 1000 times the Equal () method to compare them to 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 word will not be saved, hash on the other address.

5. The difference between String, StringBuffer and StringBuilder:

  The main performance difference between string and StringBuffer types is that the string is immutable and StringBuffer is a mutable object

StringBuffer and Stringbuild are implemented by the char[] array.

StringBuffer is thread-safe, and stringbuild is thread insecure.

6. The difference between override and overload meanings:

  Overload reload means that it can represent the polymorphism of a class, which can have the same function name in the function, but the parameter name, return value, type cannot be the same, or you can change the parameter, type, return value, but the function name remains unchanged.

Override overrides the meaning that when a subclass inherits the parent class, the subclass can define a method that has the same name and parameters as its parent class, when the subclass calls the function automatically, and the parent class is overwritten (overridden).

7. The difference between abstract classes and interfaces:

  A class can have only one class, that is, a subclass can have only one parent class, but may inherit multiple interfaces

Interfaces emphasize the implementation of a particular feature, while an abstract class emphasizes the owning relationship

All methods in an abstract class are not necessarily abstract methods, and you can choose to implement some basic methods in an abstract class. The interface requires that all methods must be abstract.

8. Principles and features of several ways of parsing xml: DOM, SAX, pull

  Dom (document Object Model): Consumes 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, the mobile phone is not good enough, it may crash directly.

SAX (Simple API for XML): High resolution, memory-intensive, time-driven: more simply, sequential scanning of documents, when scanning to document start and end, elements (element) Start and end, When the document ends, the event handler is notified, the event handler acts accordingly, and the same scan continues until the end of the document.

Pull (): Similar to sax and event-driven, we can invoke 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 GetAttribute () method to get the value of the property, or you can call its nexttest () to get the value of this node.

9. The difference between 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 give up system resources after sleeping, wait () to let system resources other threads can consume CPU

Sleep (milliseconds) needs to specify a sleeping time, and the time must be automatically awakened

10, Java Multi-state implementation principle:

  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).

The implementation of the principle is dynamic binding, the method of program calls in the run period of the dynamic binding, tracing source can be found that the JVM through the automatic transformation of parameters to find the right way.

11. Java garbage collection and memory allocation policy:

(1) What is garbage collection?

   is to release memory that is not in the object that holds the reference.

(2) How can I tell if an object needs to be collected?

   Reference count (the simplest and oldest method): The process of saving a 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.

Object reference traversal (now used by most JVMs): Object reference traversal starts with a set of objects, 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.

Reference count Flaw: reference count does not resolve circular reference problems: Suppose that objects A and B are already instantiated, let a=b,b=a, except that these two objects have no reference, the value of the counter will never be 0, but the reference counter cannot notify the GC to reclaim them.

(3) The difference between the four types of references in Java:

   Strong reference: 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 recycle it, but instead throws a outofmemoryerror error to terminate the program. If you want to break the connection 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.

Soft references: When using soft references, if there is enough memory space, 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.

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.

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.

(4) Introduce the garbage collection mechanism:

   Tag recycling: Traverse the object graph and record the reachable object, delete the Unreachable object once, and generally use single-threaded work and possibly generate memory fragmentation.

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 fragment can be synthesized a large number of reusable memory area, improve memory utilization.

Copy recovery method: divides the existing memory space into two parts, when 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.

Generational recovery: Divide the memory space into two or more domains, such as the young generation and the old age, the young generation is characterized by the object is quickly recycled, so the younger generation uses a higher efficiency algorithm. When an object is still in stock after several recoveries, the object is put into memory space called old age, and the old age takes the tag-compression algorithm.

(5) The difference between heap and Stack in Java:

  Basic data type variables and objects are referenced in the stack allocation

Heap memory is used to hold objects and arrays created by new

Class variable (static modified variable), the program allocates memory for the class variable in the heap at the time of loading, and the memory address in the heap is stored in the stack.

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 scattered heap memory address, the hash algorithm is converted to a long series of numbers to characterize the variable in the heap "physical location", The life cycle of an instance variable---when the reference to the 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.

Local variables: By declaring in a method, or a piece of code (such as a For loop), execute to it in the stack to open up memory, when the local variable once out of scope, memory immediately released.

12, Java Collection series problems

  (1) The difference between ArrayList, LinkedList and vector:

   ArrayList and vectors are stored in arrays, and vectors are less performance than ArrayList due to the use of the Synchronized method (thread safety)
LinkedList uses a doubly linked list for storage, and random access is slow
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.
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, determine whether Modcount and expectedmodcount are equal, and if not equal, indicates that another thread has modified the map

(2) The difference between HashMap and Hashtable

   HashTable older, is based on the Dictionary class implementation, HashTable is based on the map interface implementation
HashTable is thread-safe, HASHMAP is thread insecure
HashMap can let you use NULL as the key or value of an entry for a table

13, what is the thread pool, what is the role of the thread pool?

  A: The basic idea of thread pool is a kind of object pooling idea, open up a piece of memory space, it contains many (not dead) threads, the pool thread execution schedule is handled by the pool manager. When a thread task is taken from a pool, the threads object is pooled after execution, which avoids the performance overhead of repeatedly creating thread objects and saves system resources. Just like the original to the cafeteria to play rice is everyone to see who Rob Win, who first grab who first eat, have a thread after eating, is lined up, today I have a good relationship with you, you first come to dinner. For example: An application to work with the network, there are many steps need to access the network, in order not to block the main thread, each step to create threads, in-line and network interaction, with the thread pool is simple, the thread pool is a package of threads, make the thread easier to use, only need to create one thread pool, Put these steps into a thread pool like a task, as long as you call the Destroy function of the thread pool when the program is destroyed.
Disadvantages of a single thread: A. Each new thread creates a poor performance B. Threads lack unified management, may create unlimited new threads, compete with each other, and may consume too much system resources to cause a panic or oom,c. Lack of additional functionality, such as timed execution, periodic execution, and thread interruption.
The benefits of the four thread pools provided by Java are: A. Reuse the existing threads, reduce the cost of object creation, extinction, performance is good. B. Can effectively control the maximum number of concurrent threads, improve the utilization of system resources, while avoiding excessive resource competition, avoid clogging. C. Provide functions such as timed execution, periodic execution, single-threaded, concurrency control, etc.

From Link: http://www.imooc.com/article/19234

Java face Test (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.