Java Common trivia Point (continuous update ...)

Source: Internet
Author: User
Tags array length

1. In what scenario do I need to rewrite hashcode? How is the HashMap implemented?
    • If we override the Equals () method, Hashcode is generally overridden to ensure that the same object returns the same hash value
    • HashMap is actually a "chain-table hash" of the data structure, that is, the combination of arrays and linked lists
    • When we put the value in the HashMap, we first calculate the hash value of the key, and then the hash value to calculate the key in the array subscript (through the hash and array length-1 bit and operation, it is therefore, the map length is 2 of the second side, because to ensure that length-1 are If there are other elements in the array at that position, then the elements in this position will be stored as linked lists, the new ones placed in the chain head, and the first ones added at 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
    • Default length is 16, load factor 0.75
2.ConcurrentHashMap

Concurrenthashmap the map into a few segment,put and get when all according to the key hash, to calculate in which segment, each segment similar to a Hashtable, are thread-safe

3. Containers

(1) In Java, if a class is used to hold objects of other classes, the class is called a container, or a collection. A set is a whole that is formed by a collection of classes of the same or similar nature.
The container is required because the length of the array is difficult to expand and the data types in the array must be the same

(2) Difference between container and array

    • The container has no subscript
    • Array functions can be implemented using ArrayList.
    • Use Toarraylist to return an array
The difference between 4.ArrayList and LinkedList

ArrayList based on the array, find faster, LinkedList based on linked list, adding and deleting operations relatively fast

5.set Anti-weight
    • HashSet rules that do not deposit duplicate elements. Using Hashcode and Equals, if calculated, the join fails
    • Reeset the data structure of red-Haishi, the default is to sort the elements naturally. If two objects return a value of 0 at the time of comparison, then the element repeats
6.Collection Packaging Thread Safety

Collections.synchronizedlist (New ArrayList ());
Collections.synchronizedset (New HashSet ());

7.JVM memory Model The JVM defines a number of data regions used during the execution of a program, mainly divided into method area, heap, virtual machine stack, local method stack, program Counter 7.1 program counter

A program counter is a small amount of memory space that can be seen as the line number indicator of the bytecode that is being executed by the current thread. Base functions such as branching, looping, and exception jumps depend on it.
If you are executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed, and if you are performing a local method, this counter is empty.

7.2 Virtual Machine Stack

The thread is private and the life cycle is the same as the thread. The virtual machine stack describes the memory model that the Java method executes: Each method is executed and a stack frame is created.
The local variable table for storing method, Operation Stack, dynamic link, method exit and other information, Java method from call to execution completion, is a frame stack in the virtual machine stack into the stack process

    • Local Variables table: Local variables used to store method parameters and method internal definitions
    • Operand stacks: As with local variable tables, the operand stack is also organized into an array of word lengths, but not indexed to get the value, it is through the stack, the stack to do the data
      Access.

    • Dynamic linking: When a virtual machine is running, a constant pool holds a large number of symbol references. If it is converted to a direct application during the class loading phase or when it is first used, it becomes static parsing,
      Otherwise, it becomes a dynamic link.

    • Return address: One is normal exit, after exiting will be based on the definition of the method to determine whether to pass the return value to the upper level of the caller, one is the exception caused by the end of the method, the case is not to return the value to the upper layer of the calling method.
      The first call of the method corresponds to the stack frame in the virtual machine stack in the stack of the stack operation, so the method exits may do things include: Restore the upper method of the local variable table and the operand stack, if there is a return value, the return value is pressed into the stack frame of the caller stack, The value of the PC counter is also adjusted to the next instruction in the method invocation entry.

7.3 Local Method Stack

Like a virtual machine stack, a virtual machine stack exists to execute a Java method, and the local method stack is used to execute the native method into the stack.

7.4 Stacks

is the largest piece of memory managed by the JVM. shared by all threads, created at virtual machine startup, to hold object instances,-xms (min) and-xmx (maximum), default is 1/64 of computer physical memory
The heap is the main activity area of GC, because the GC basically uses the Generational collection algorithm, so it is divided into the new generation and the old age. New generation, the newly created object of the program is to allocate memory from the Cenozoic, after multiple GC still inventory objects will enter the old age,
The newly created object may also go directly into the old age, large objects or large array objects, and no external objects are referenced inside the array

7.5 Method Area

Stores data such as class information, constants, static variables, and instant compiler-compiled code that have been loaded by the virtual machine

8. GC Algorithm Correlation

GC General system random Trigger and System.GC (), and fires when Eden allocates a new object with insufficient memory to trigger MINORGC, or Eden transfers to old but old memory is not enough to trigger FULLGC
For the new generation, the general use of replication algorithms, general memory is divided into a larger Eden and 2 survivor, each using Eden and a survivor, the default of 8:1, each time the Eden and survivor live objects stored in another survivor,
Old age guarantee required (Handle Promotion)
For the old age, the mark-and-sweep algorithm was used to offset the available objects to one side of the memory and then remove the memory outside the boundary.

9. How to Judge an object as garbage when GC

Can be judged by reference counters, but the problem of circular references cannot be resolved
Accessibility analysis, GC root and this object do not have a path to reach

The circular reference demo is as follows

Class C {    public  Object x;   New  ==null

In this case, the counters for both obj1 and Obj2 are not 0

10.sql optimization
    • Avoid full table scans as much as possible by indexing on the Where and order by
    • Avoid null judgments in where, use! = or <> operators sparingly, avoid or replace with union all, use in exists instead of in, and use less functions in the WHERE clause

Java Common trivia Point (continuous update ...)

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.