java foundation -- review the old and know the new series 1

catalogue

preface

Cache pool (constant pool)

1. Prove the existence of cache pool

2. Cache value range

Implicit type conversion

keyword

final

static

1. Static variable

2. Static inner class

this,super

Access modifier

java8 features

abnormal

Java collection (container)

1,List, Set, Queue, Map

preface

This series focuses on the basic knowledge of Java and common technologies at work, and precipitates the experience accumulated during the working time. After this round of review, I believe we will gain something!!! (this series will not be exhaustive, but will be concise)

Cache pool (constant pool)

1. Prove the existence of cache pool

Integer a = Integer.valueOf(23);
Integer b = Integer.valueOf(23);
System.out.println(a == b);   // true
String s1 = "Yao";
String s2 = "Yao";
System.out.println(s1 == s2); // true

2. Cache value range

  • Boolean ---values true and false
  • Byte ---all values
  • Short ,Long,Integer---values between -128 and 127
  • Character --- \u0000 to \u007

Note: the upper limit of Integer type can be adjusted through the jvm parameter (- XX:AutoBoxCacheMax)!

Implicit type conversion

byte b = 1;
b += 1;
b++;

keyword

final

final int[] is = new int[] {1,2,3,4};
System.out.println(Arrays.toString(is)); //[1, 2, 3, 4]
is[0] = 2; 
System.out.println(Arrays.toString(is)); //[2, 2, 3, 4]
//is = new int[3];  Compilation error

static

1. Static variable

Static variables are also called class variables. All instances of a class share a static variable

public class Demo {
	
	static int age=2;

	public static void main(String[] args) {
		Demo demo1 = new Demo();
		Demo demo2 = new Demo();
		demo1.age = 23;
		System.out.println(demo1.age);
		System.out.println(demo2.age);
	}
}

2. Static inner class

public class OuterClass {
 
    class InnerClass {} //Inner class

    static class StaticInnerClass {} //Static inner class

    public static void main(String[] args) {
        OuterClass outerClass = new OuterClass();
        InnerClass innerClass = outerClass.new InnerClass(); 
        StaticInnerClass staticInnerClass = new StaticInnerClass(); 
        OuterClass outerClass = new OuterClass(){{System.out.println("Anonymous Inner Class ");}};
    }
}

this,super

public class Demo {
	String name;
	Byte age;
	 public Demo(){
		 name = "Invincible hands ";
		 age = 23;
	 }
	 public Demo(Byte age){
		 this();
		 this.age = age;
	 }
}
class Demo2 extends Demo{
	Demo2(){
		super();
	}
	
}

Access modifier

java8 features

Click to view related articles

abnormal

Error is an error that cannot be handled by the program and cannot be caught by catch, such as out of memory error (out of memory error) and class definition error (NoClassDefFoundError).

Java collection (container)

1,List, Set, Queue, Map

List: the stored elements are ordered and repeatable.

  • Arraylist, Vector: the bottom layer is an array
  • LinkedList: two-way linked list structure (before JDK1.6, it was a circular linked list, and JDK1.7 canceled the cycle)

Set: the stored elements are non repeatable and unordered.

  • HashSet: the bottom layer uses HashMap to save elements.
  • LinkedHashSet: the bottom layer is LinkedHashMap, which is orderly.
  • TreeSet (ordered, unique): red black tree (self balanced sorted binary tree)

Queue: queue (first in first out)

  • PriorityQueue: array implements binary heap
  • ArrayQueue: array + bidirectional pointer

Map: key value pairs. Keys are unordered and not repeated.

  • HashMap: JDK1. Before 8, HashMap was composed of array + linked list, jdk1 After 8, when the length of the linked list is greater than the threshold (the default is 8) (it will be judged before converting the linked list to a red black tree. If the length of the current array is less than 64, you will choose to expand the array first instead of converting to a red black tree), convert the linked list to a red black tree to reduce the search time
  • LinkedHashMap: LinkedHashMap inherits from HashMap, so its bottom layer is still based on zipper hash structure, that is, it is composed of array, linked list or red black tree. In addition, LinkedHashMap adds a two-way linked list on the basis of the above structure, so that the above structure can maintain the insertion order of key value pairs. At the same time, through the corresponding operation of the linked list, the access order related logic is realized. Details can be viewed:
  • Hashtable: it is composed of array + linked list. Array is the main body of hashtable, and linked list mainly exists to solve hash conflict
  • TreeMap: tree

Keywords: Java Interview Programmer

Added by robindean on Thu, 16 Dec 2021 15:15:05 +0200