This is the 7th day of my participation in Gwen Challenge
A basis,
How to create Java Objects (5 ways)
new
The keyword- Reflection:
Class.newInstatnce()
- (This method must have a public no-argument constructor, otherwise an error will be reported)
- Reflection:
Constructor.newInstance()
- Method calls that have arguments (no longer have to be no-arguments) and private constructors (no longer have to be public)
- clone
- call
clone
Method, the JVM creates a new object that copies the contents of the previous object - call
clone
Method to create an objectNo constructors are called
- call
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
- 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
- Modify static code blocks: (Initialize execution order: Static code blocks > Normal code blocks > constructors), static code blocks are executed only once.
- Modify inner classes: Static modifies only inner classes
final
final
Modification of theclassCannot be inheritedfinal
Modification of themethodsCannot be coveredfinal
Modification 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
this
The keyword is used to refer to the current instance of the class
super
super
The keyword is used to access variables and methods of the parent class from a subclass
Pay attention to this and super
this
When other constructors of this class are called, they need to be placed on the first linesuper
When accessing the constructor of the parent class, it needs to be in the first lineThis, super
Cannot be used instatic
In 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
Map.entry
(map.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
- To iterate over
key
orvalues
(map.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
- The iterator
Iterator
(map.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
- 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