Java---java face question (ii)

Source: Internet
Author: User
Tags stringbuffer

1 switch can I use string to make arguments?
Prior to Java 7, switch could 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 = =:
= = Determines whether two variables or instances are pointing to the same memory space
Equals is to determine if the value of the memory space pointed to by two variables or instances is the same
What are the common methods for 3 object?

    1. Method equals tests whether two objects are equal
    2. Method clone for object copy
    3. Method GetClass Returns the class object associated with the current object
    4. The notify,notifyall,wait method is used for thread synchronization of a given Object.

4 Java Four kinds of references, soft and weak, used to the scene

  1. Solve 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
  2. Caching of Java objects through Soft-and object-regain methods: for example, we created 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
  3. 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 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
  4. 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.
  5. 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
  6. 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.
  7. Usage scenarios:

5 The role of hashcode, what is the difference with equal

    • 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 the use of equal to compare, if there are 1000 elements, you will be a new element, 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

    1. The main performance difference between string and StringBuffer types is actually that string is an immutable object
    2. StringBuffer and StringBuilder are implemented by the char[] array.
    3. StringBuffer is thread-safe, and StringBuilder is thread insecure

7 The meaning of override and overload to distinguish

    1. 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.
    2. It is ride (overriding) that when a subclass inherits a parent class, it is possible to define a method that has the same name and parameters as its parent class, and when the subclass calls the function, the child class is called automatically, and the parent class is overwritten (overridden).

8 Differences between abstract classes and interfaces

    1. A class can inherit only a single class, but may implement multiple interfaces
    2. Interfaces emphasize the implementation of a particular feature, while an abstract class emphasizes the owning relationship
    3. 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

    1. Dom: 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, mobile phone is not good enough, the phone may crash directly
    2. SAX: High resolution, memory-intensive, event-driven: more simply, sequential scanning of documents notifies the event handler when the document begins 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.
    3. Sax: similar to sax and event-driven, we can call its next () method to get the next parse event (that is, start the document, end the document, start the tag, end the Tag) and call Xmlpullparser when it is in an element getattributte () method to get the value of the property, or call its nexttext () to get the value of this Node.

The difference between ten wait () and sleep ()

    1. Sleep comes from the thread class, and wait comes from the object class
    2. During the call to the sleep () method, the thread does not release the object Lock. Calling the wait method thread frees the object lock
    3. Sleep does not sell system resources, wait to let system resources other threads can consume CPU
    4. 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

    1. A reference to a base data type that is more than a variable and an object is allocated on the stack
    2. Heap memory is used to hold objects and arrays created by new
    3. Class variable (static modified variable), the program allocates memory to the class variable in the heap at the time of loading, and the memory address in the heap is stored in the stack
    4. 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", Life cycle of instance variables-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
    5. 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

    • 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, program call method in the runtime only dynamic binding, tracing the source can be found that the JVM through the automatic transformation of parameters to find the right way.

JAVA garbage collection mechanism

  1. Tag recycling: traverse an object graph and record reachable objects to remove unreachable objects, typically using single-threaded work and potentially generating memory fragmentation
  2. 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
  3. 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.
  4. Generational recycling: The memory space is divided into two or more domains, such as the young 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
  5. 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
  6. 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
  7. What is a garbage collector: frees memory for objects that are no longer holding references
  8. How do I tell if an object needs to be collected?
  9. 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 differences: ArrayList and vector Bottom is the use of arrays 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 bottom source of the 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 modify the map, then the concurrentmodificationexception is thrown, which is the so-called 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:

*************************************************************************************************************** *************

Java---java face question (ii)

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.