Java interview questions, Java interview questions

Source: Internet
Author: User

Java interview questions, Java interview questions
Interview is something that everyone has to go through. Most people have more than one interview. Here we will summarize the latest interview questions for 2016, so that you can get twice the result with half the effort when looking for a job in 2017. 1. Can a Switch use string as a parameter? A. Before Java 7, the switch can only support byte, short, char, int, or its corresponding encapsulation class and Enum type. In JAVA 7, String Support is added. 2 equals and =:. = is to judge whether two variables or instances point to the same memory space equals is to judge whether two variables or instances point to the same memory space value is the same 3 What are the common methods of Object? A. the equals method tests whether two objects are equal. B. method clone to copy objects c. the getClass method returns the Class Object d related to the current object. methods notify, policyall, and wait are four types of references for thread synchronization of a given object, which are weak and soft Virtual. Scenario a is used. solve the OOM problem by using soft reference and weak reference: Use a HashMap to store the ing between the image path and the soft reference associated with the corresponding image object. When the memory is insufficient, JVM automatically recycles the space occupied by these cached image objects, effectively avoiding the OOM Problem B. implement High-speed cache of Java objects through soft and object re-acquisition methods: for example, if we create a class of one Employee, We need to query the information of one Employee each time. It takes a lot of time to build a new instance, even if it was just queried in a few seconds. We can use soft references and HashMap to save references. First, we can use soft references to reference the instance of an Employee object and save the reference to HashMap, key is the id of this Employee. value is the soft reference of this object. On the other hand, it is used to retrieve the reference and check whether the soft reference of this Employee instance is in the cache. If yes, it is obtained from the soft reference. If there is no soft reference, or the Instance obtained from the soft reference is null, re-build an instance and save the soft reference to the new instance. If you want to learn Java, you can come to this group. The first is the second, the second, and the sixth. There are a lot of learning materials to download. C. Strong reference: if an object has a strong reference, it will not be recycled by the garbage collector. Even if the current memory space is insufficient, the JVM will not recycle it, but throw an OutOfMemoryError to terminate the program. If you want to interrupt the association between a strong reference and an object, you can explicitly assign the reference value to null. In this way, the JVM will reclaim the object at the appropriate time. soft reference: When soft reference is used, if the memory space is sufficient, soft reference can continue to be used instead of being recycled by the garbage collector. Only when the memory is insufficient, soft reference will be recycled by the garbage collector. E. Weak references: objects with weak references have shorter lifecycles. This is because when the JVM executes garbage collection and finds a weak reference object, the weak reference will be recycled regardless of whether the current memory space is sufficient. However, since the garbage collector is a thread with a lower priority, it may not be able to quickly find the weak reference object f. virtual Reference: As the name implies, it is just a virtual object. If an object only holds a virtual reference, it is equivalent to no reference and may be recycled by the garbage collector at any time. G. use Cases: 5. What is the difference between Hashcode and equal?. it is also used to identify whether two objects are equal. java sets have two types: list and set. set does not allow repeated implementation of elements. This method does not allow repeated implementation, if equal is used for comparison, if there are 1000 elements, you need to call equal 1000 times to compare them with the same object one by one, this will greatly reduce the efficiency. Hashcode is actually the storage address of the returned object. If there are no elements at this position, the elements are directly stored. If there is already an element at this position, at this time, the equal method is called to compare with the new element. If the same method is used, it will not be stored. The differences between String, StringBuffer and StringBuilder are hashed to other addresses.. the main performance difference between the String type and the StringBuffer type is that String is an immutable object B. the bottom layer of StringBuffer and StringBuilder is the c implemented by the char [] array. stringBuffer is thread-safe, while StringBuilder is thread-insecure. 7. What is the difference between Override and Overload?. overload, as its name implies, is a reload, which can represent the class polymorphism. It can be a function with the same name but the parameter name, return value, and type cannot be the same; or you can change the parameters, types, and returned values, but the function name remains unchanged. B. this is the meaning of ride (override). When the subclass inherits the parent class, the subclass can define a method with the same name and parameters as its parent class, when a subclass calls this function, it automatically calls the subclass method, and the parent class is overwritten. 8 differences between abstract classes and interfaces. A class can only inherit a single class, but can implement multiple interfaces B. the interface emphasizes the implementation of specific functions, while the abstract class emphasizes the relationship c. all methods in the abstract class are not necessarily abstract. You can implement some basic methods in the abstract class. The interface requires that all methods must be abstract 9. principles and features of parsing XML: DOM, SAX, PULLa. DOM: memory consumption: Read all xml documents to the memory, and then use DOM APIs to access the tree structure and obtain data. This is easy to write, but it consumes a lot of memory. If the data is too large, the mobile phone is not powerful enough, and the mobile phone may crash. B. SAX: High Resolution efficiency and low memory usage. event-driven: Simply put, documents are scanned sequentially. When a document is scanned) the event processing function is notified when the start and end of an element, the document ends, and so on. The event processing function performs the corresponding action and continues the same scan, until the end of the document. C. SAX: similar to SAX, it is also event-driven. We can call its next () method to obtain the next parsing event (that is, start document, end document, and start tag, when an element is present, you can call the getAttributte () method of XmlPullParser to obtain the attribute value, or call its nextText () to obtain the value of the current node. 10 difference between wait () and sleep () sleep comes from the Thread class, And the Thread does not release the Object lock when the Object class calls the sleep () method. The thread that calls the wait method will release the object lock and sleep, so the system resources will not be transferred. The wait will allow other threads to occupy CPUsleep (milliseconds) and need to specify a sleep time, the difference between heap and stack in JAVA will be automatically awakened at the first time. Let's talk about the java memory mechanism. compared with variables and object references, the basic data type is allocated on the stack B. heap memory is used to store objects and arrays created by new. c. when a program loads a class variable, it allocates memory for the class variable in the heap. The memory address in the heap is stored in the stack d. instance variable: when you use the java keyword new, the system opens up in the heap and does not necessarily allocate continuous space to the variable. It is based on the scattered heap memory address, the hash algorithm is used to convert a long string of numbers to indicate the "physical location" of the variable in the heap. the life cycle of the instance variable-when the reference of the instance variable is lost, it will be GC (Garbage Collector) included in the recyclable "list", but not immediately released the heap memory e. local variables: are declared in a method or a code segment (such as a for loop ).) To open up the memory in the stack when it is executed. When the local variable is out of scope, the memory immediately releases 12 JAVA polymorphism implementation principle. abstract, polymorphism means that the same message can adopt different behavior methods based on different sending objects. (Sending a message is a function call.) B. the implementation principle is dynamic binding, and the method called by the program is dynamically bound at runtime. tracing the source code can find that JVM can find a suitable method through automatic parameter transformation. 13 JAVA garbage collection mechanism. mark collection method: traverses object graphs and records reachable objects to delete inaccessible objects. Generally, a single thread is used to work and may generate memory fragments B. mark-compression and recycling method: In the early stage, the method is the same as the first method. It only takes one more step to compress all the surviving objects to one end of the memory, in this way, memory fragments can be merged into a large reusable memory area, improving the memory utilization. copy and recycle method: divides the existing memory space into two parts. During gc runtime, it copies the reachable objects to the other half of the space, and then clears all the objects in the space in use. This method is applicable to objects with a short lifetime, and objects with a long lifetime of continuous replication result in lower efficiency. D. generational recycling: memory space is divided into two or more domains, such as the young generation and the old generation. The characteristics of the young generation are that objects will be quickly recycled, therefore, the young generation uses highly efficient algorithms. When an object remains alive after several recycles, it is put into a memory space called the old age, and the mark-Compression Algorithm e is used in the old age. reference count (the simplest and oldest method): stores the number of times a resource (such as an object, memory, or disk space) is referenced, the process of releasing a referenced number when it becomes zero. object Reference traversal (the method currently used by most jvm): Object Reference traversal is performed recursively to determine accessible objects starting from a group of objects and following each link on the entire object graph. If an object cannot be reached from one (at least one) of these root objects, use it as a garbage collection g. what is a garbage collector: Release the memory of objects that no longer hold references. how can I determine whether an object needs to be collected? I. Several garbage collection mechanisms 14. What are the differences between Java collections? A. hashTable is relatively old and implemented based on the Dictionary class. HashTable is implemented based on the Map interface B. hashTable is thread-safe, while HashMap is c. hashMap allows you to use a null value as the key or valued of a table entry. the differences between ArrayList, list, and Vector: the bottom layer of ArrayList and Vector stores data in arrays. Because Vector uses the synchronized Method (thread-safe), its performance is inferior to that of ArrayList, using a two-way linked list for storage, slow Random Access e. underlying source code implementation of HashMap: when we put the elements in HashMap, re-calculate the hash value based on the hashCode of the key, according to the hash value, the position of this element in the array (I .e. the subscript). If the array already contains other elements, then the elements at this position will be stored in the form of a linked list, newly added Put it in the chain header, and put it first at the end of the chain. If no element exists in the position of the array, the element is directly placed in this position of the array. F. Fail-Fast mechanism: if other threads modify the map when using the iterator, ConcurrentModificationException will be thrown. This is the so-called fail-fast mechanism. The implementation of this mechanism in the source code is through the modCount field. As the name suggests, modCount is the number of modifications. This value will be added to all modifications to the HashMap content, this value will be assigned to the expectedModCount of the iterator during the iterator initialization process. During iteration, determine whether modCount and expectedModCount are equal. If they are not equal, other threads have modified Map.

Related Article

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.