This is the 7th day of my participation in Gwen Challenge

A basis,

How to create Java Objects (5 ways)

  1. newThe keyword
  2. Reflection:Class.newInstatnce()
    • (This method must have a public no-argument constructor, otherwise an error will be reported)
  3. Reflection:Constructor.newInstance()
    • Method calls that have arguments (no longer have to be no-arguments) and private constructors (no longer have to be public)
  4. clone
    • callcloneMethod, the JVM creates a new object that copies the contents of the previous object
    • callcloneMethod to create an objectNo constructors are called

Object class

  • Is the parent of all classes in Java. Methods include:
    • ToString () returns information about the current object itself, as a string object
    • Equals () compares whether two objects are the same object, and returns true if they are
    • HashCode () returns the hash value of the object
    • GetClass () gets information about the Class to which the current object belongs and returns the Class object

The keyword

static

  1. Static modifies member variables and methods: Static variables and methods belong to a class. They do not belong to an object and are shared by all objects of the class. Member variables declared static are static member variables that are stored in the method area of the Java memory area
  2. Modify static code blocks: (Initialize execution order: Static code blocks > Normal code blocks > constructors), static code blocks are executed only once.
  3. Modify inner classes: Static modifies only inner classes

final

  1. finalModification of theclassCannot be inherited
  2. finalModification of themethodsCannot be covered
  3. finalModification of thevariableFor constant
    • If a variable is of a primitive data type, its value cannot be changed once initialized.
    • If a variable is a reference type, it cannot be made to point to another object after it is initialized.

this

  • thisThe keyword is used to refer to the current instance of the class

super

  • superThe keyword is used to access variables and methods of the parent class from a subclass

Pay attention to this and super

  • thisWhen other constructors of this class are called, they need to be placed on the first line
  • superWhen accessing the constructor of the parent class, it needs to be in the first line
  • This, superCannot be used instaticIn the method

Interfaces and abstract classes

Three, collections,

List

  • ArrayList: Object array
  • LinkedList: a two-way LinkedList
  • Vector: an Object array

Set

  • HashSet :(unordered) based on the implementation of HashMap, the underlying use of HashMap to store element data
  • TreeSet :(unordered) red-black tree (self-balanced sorted binary tree)

Map

  • HashMap:
    • Before JDK1.8, a HashMap consists of arrays (the body of a HashMap) and linked lists (the main object of a HashMap).
    • After JDK1.8, see the link for red-black trees
  • LinkedHashMap: LinkedHashMap descends from HashMap,
  • HashTable: Array + linked list, array is the body of HashTable, linked list is to resolve hash conflicts exist
  • TreeMap: red-black tree (self-balanced sorted binary tree)

Map Common traversal modes

  1. Map.entrymap.entrySet())
    • The most common way
    Map<String, String> map = new HashMap<String, String>();
    map.put("a"."111");
    map.put("b"."222");
    map.put("c"."333");
    for (Map.Entry<String, String> entry : map.entrySet()) {
        String mapKey = entry.getKey();
        String mapValue = entry.getValue();
        System.out.println(mapKey + ":" + mapValue);
    }
Copy the code
  1. To iterate overkeyorvaluesmap.keySet() / map.values())
    • Applies to keys or values only
    Map<String, String> map = new HashMap<String, String>();
    map.put("a"."111");
    map.put("b"."222");
    map.put("c"."333");
    // Print the set of keys
    for (String key : map.keySet()) {
        System.out.println(key);
    }
    // Prints a collection of values
    for (String value : map.values()) {
        System.out.println(value);
    }
Copy the code
  1. The iteratorIteratormap.entrySet().iterator())
    Map<String, String> map = new HashMap<String, String>();
    map.put("a"."111");
    map.put("b"."222");
    map.put("c"."333");
    Iterator<Entry<String, String>> entries = map.entrySet().iterator();
    while (entries.hasNext()) {
        Entry<String, String> entry = entries.next();
        String key = entry.getKey();
        String value = entry.getValue();
        System.out.println(key + ":" + value);
    }
Copy the code
  1. Traversal by key finding value
    • This approach is inefficient because the value from the key itself is a time-consuming operation.
    Map<String, String> map = new HashMap<String, String>();
    map.put("a"."111");
    map.put("b"."222");
    map.put("c"."333");
    for(String key : map.keySet()){
        String value = map.get(key);
        System.out.println(key+":"+value);
    }
Copy the code

Four, IO

IO model

BIO

NIO

AIO

Five, abnormal

Six, reflection

Refer to the link

  • JavaGuide Common Java keywords
  • JavaGuide Java IO model
  • How many of the five ways to create objects in Java do you know?
  • Java traverses a Map collection in four ways