String x=”abc”; Different from String x=new String(” ABC “)

String x=” ABC “memory will look for the permanent generation (constant pool), if the constant pool does not have a new region, the x pointer to the region address, if the constant is present, directly to the constant address.

String STR = new String(” ABC “); A new block of memory is created in the heap, a pointer is assigned to the stack, and a reference to the newly constructed String is assigned to STR. So whenever it’s a new String(), the address in the stack points to the latest new address in the heap.

Note: String Integer and so on override equals

String s3 = new String("hello"); String s4 = "hello"; System.out.println(s3 == s4); // false System.out.println(s3.equals(s4)); // true String s5 = "hello"; String s6 = "hello"; System.out.println(s5 == s6); // true System.out.println(s5.equals(s6)); // true String s1 = "hello"; String s2 = "world"; String s=s1+s2; String s3 = "helloworld"; System.out.println(s3 == s1 + s2); System.out.println(s == s3); //false System.out.println(s3 == "hello" + "world"); //false // Two constants are added together to get a new constant, and then the constant pool is checked to see if it existsCopy the code

Why do I have to override the hashCode method when overriding equals?

Object’s hashcode methods are native methods, that is, implemented in C or C ++, which usually convert the Object’s memory address to an integer and return it. The substance of the overridden hashCode is related to the hashCode() value of each attribute of the object

 public static int hashCode(Object a[]) {
        if (a == null)
            return 0;

        int result = 1;

        for (Object element : a)
            result = 31 * result + (element == null ? 0 : element.hashCode());

        return result;
    }
Copy the code

The hashCode() method must be overridden when overriding equals only with respect to the hash container. You don’t need to overwrite hashCode() if you are sure that the object will not be added to the hash container. HashCode () is only useful in hash tables, not in other cases. When two objects equal to hashCode () are added to a hashSet without overwriting hashCode (), since the underlying hashSet is the element of the hashMap set as the key in the Map, when the set is added to a second object, the position of the object is determined by the hashCode value of the object. If a new hashCode value is not overridden, it is added to the set, which is not the desired result. (If hashCode is not overwritten, the hash table cannot be correctly positioned based on the hashCode value of the key.)

There is only value passing in Java

public static void main(String[] args) { int num1 = 10; int num2 = 20; swap(num1, num2); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); } public static void swap(int a, int b) {public static void swap(int a=10, int b=20 int temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b); } // a = 20 b = 10 num1 = 10 num2 = 20Copy the code
public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; System.out.println(arr[0]); change(arr); System.out.println(arr[0]); } public static void change(int[] array) {array[0] = 0; } // Output 1 0Copy the code
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Student s1 = new Student (" zhang "); Student s2 = new Student("小李"); Test.swap(s1, s2); System.out.println("s1:" + s1.getName()); System.out.println("s2:" + s2.getName()); } public static void swap(Student x, Student y) { Student temp = x; x = y; y = temp; System.out.println("x:" + x.getName()); System.out.println("y:" + y.getName()); }} // Output x: xiao Li y: Xiao Zhang s1: Xiao Zhang s2: Xiao LiCopy the code

A hashMap source

1.7

The default array size is 16

The default loading factor is 0.75F

Threshold = Capacity x load factor

A constructor

Call this method to assign

  1. Put the process

1.1 Check whether the array is empty and initialize the empty array

First make sure that the array size is >= the capacity value passed in and is a power of 2,

Note: Because the subscript formula of the array is hashCode & (Length-1), in the operation and, in order to make the subscript within the range of 0-Length-1, Lenth-1 is a number that must have all the high values of 0 and all the low values of 1, it is estimated that length must be only 1 bits of 1 and the other bits of 0, that is, the power of 2.

Calculation of threshold

The new array

1.2 Hash (Key) Method To obtain a hash value, internal xOR and right-shift the original hashCode to participate in the operation, improve the hash of hashCode

1.3 Indexfor method to obtain the stored array index

If the hash value of the current node is equal to the hash value just calculated and (the memory address of the key is equal or the content is equal), the new value overwrites the old value and returns the old value

ExpectCount ==expectCount; remove(); expectCount= =expectCount Is a fast-fail mechanism because hashMap does not allow adding or subtracting operations during traversal because it is considered risky.

1.6 If there are no equal entries, run the addEntry(hash, key,value, I) method

Size > size * load factor && table[I]! =null

Array capacity

Create a new array with twice the capacity, transfer the data to the new array, and calculate the threshold

Each node on the transfer list may recalculate the same or twice as much I as the original one, and the whole purpose of expansion is to make the array longer and the list shorter

Note: Why is hashMap thread unsafe, because multithreading can cause problems with Pointers in the transfer method, creating a circular linked list and depleting resources

Create a node

Head insertion and move down, size+1

ConcurrentHashMap

1.7

HashTable is also thread-safe, internally adding the synchronized keyword to every operation

Constructor (default 16 0.75f 16)

DEFAULT_CONCURRENCY_LEVEL: Number of segments

DEFAULT_INITIAL_CAPACITY: indicates the number of hashentries

Figure out that each segment has a hashEntry

A maximum of 16 segments can be created

The true number of segments is < the passed level && is a power of 2 (1,2,4,8,16).

Determining the number of Hashentries Each hashEntry must also be a power of 2 and greater than capacity/level

new Segment[]

Place the segment object at index 0

The CAS mode allows only one thread to modify the array