This main record (handling) is the understanding of some common concepts in Java, the following
- The difference between final, finally, finalize
- Java's interpretation of the value of a pass or reference
- overriding override and overload overload understanding
- Comprehension of combinations and inheritance
- The understanding of abstract class and interface interface
- The difference between static nested classes (the static Nested Class) and inner classes (Inner Class)
- = =, equals (), Hashcode (), and CompareTo ()
- Constant pool, String (immutable Class), StringBuffer, StringBuilder
- Understanding of static, public static void main (string[] args)
- Initialization order of Java programs
- Whether the abstract method can be static at the same time (static), whether it can be a local method (native), and whether it can be synchronized decorated at the same time
- Whether static methods can be overridden
the difference between final, finally, finalize
1. Final: the modifier (keyword) is used to declare properties, methods, and classes, respectively, that the property is immutable, that the method is not overridden, and that the class is not inheritable.
- Final class: Cannot derive a subclass, that is, it cannot be inherited as a parent class. Therefore, a class cannot be declared both abstract and final.
- Final variable: Given the initial value at the time of declaration, subsequent cannot change its initial value (base type) or reference (but the properties of the referenced object can be changed).
- Final method: can only be used and cannot be overloaded.
2. Finally: part of the structure of an exception-handling statement that is always executed.
Provides a finally block to perform any cleanup operations during exception handling. If an exception is thrown, the matching catch clause executes, and then the control enters the finally block, unless the JVM is closed (system.exit (1)), which is typically used as a resource that frees external resources (resources that are not reclaimed by the garbage collector).
3. A method of the Finalize:object class that, when executed by the garbage collector, calls this method of the reclaimed object, which can override this method to provide garbage collection of other resource collections, such as closing the file.
- Java allows the use of the Finalize () method to do the necessary cleanup before the garbage collector clears objects from memory. This method is called by the garbage collector to this object when it determines that the object is not referenced.
- It is defined in the object class, so all classes inherit it. Subclasses override the Finalize () method to organize system resources or perform other cleanup work.
- Note that it is not possible to ensure exactly when the garbage collector calls the method, nor does it guarantee the order of methods that invoke different objects.
Extension: What classes in the JDK are not inherited?
The class with the final decoration. Types that generally compare basic types or prevent extension classes from inadvertently destroying the original method should be finally, in the JDK, string and StringBuffer are basic types, so strings, StringBuffer, and so on are not inherited.
Java's interpretation of the value of a pass or reference
The Java method passes a parameter that has a base type and a reference type of 2.
- Base type: The method passes a copy of the value of the base type parameter, and any change to the copy of the parameter in the method does not affect the value of the original variable.
- Reference type: The method passes a copy value of the reference, and the copy and the original argument point to the same object, and any changes to the content of the object referenced by the copy within the method will have an effect, and if you point the Copy to another object reference, then the operation does not cause any shadow to the original reference object.
overriding override and overload overload understanding
1. Overwrite/override (override)
The child class re-implements the parent class (or interface) method.
- The flag of the overlay method must match the mark of the overridden method exactly to reach the effect of coverage;
- The return value of the overridden method must be consistent with the return of the overridden method;
- The exception that is thrown by the overridden method must be the same as the exception that is thrown by the overridden method, or its subclass;
- The overridden method cannot be private, otherwise only a new method is defined in its subclass and is not overwritten.
2. Heavy-duty overload
The same class, the same function name different number of parameters/types of methods.
- You can only pass different parameter styles when using overloads. For example, different parameter types, different number of parameters, different order of parameters;
- cannot be overloaded by Access permission, return type, thrown exception;
- The exception type and number of methods do not affect overloading;
- For inheritance, if a method is priavte in the parent class, it cannot be overloaded in subclasses, and if defined, it simply defines a new method without overloading the effect.
[Refer to the difference between overloading and overwriting (also called rewriting) in 1:java]
[The difference between overloading and rewriting in reference 2:java]
comprehension of combinations and inheritance
- In an inheritance structure, the inner details of the parent class are visible to the child class. So we can often say that code reuse through inheritance is a white-box code reuse. (If the implementation of the base class changes, the implementation of the derived class will change as well.) This leads to the unpredictability of the subclass behavior;)
- A combination is the creation of new, more complex functions by assembling (combining) existing objects. Because the internal details are not visible between the objects, we also say that the code reuse in this way is black-box code reuse. (because a type is generally defined in a composition, it is not known at compile time which method of the implementation class will be called at all)
- inheritance, when writing code to specify which class to inherit, so, in the compilation period to determine the relationship. (implementations inherited from the base class cannot be dynamically changed at run time, thus reducing the flexibility of the application.) )
- Combination, you can use interface-oriented programming when writing code. Therefore, the combination of classes is generally determined at run time.
| Combinatorial Relationships |
Inheritance Relationship |
| Pros: Do not break the package, the whole class and the local class loosely coupled, relatively independent of each other |
Cons: Break encapsulation, the child class is tightly coupled with the parent class, the subclass relies on the implementation of the parent class, the subclass lacks the independence |
| Advantages: Good Scalability |
Cons: Supports scaling, but often at the expense of increasing the complexity of the system structure |
| Pros: Support dynamic combination. At run time, the whole object can select different types of local objects |
Disadvantage: Dynamic inheritance is not supported. Subclasses cannot select different parent classes at run time |
| Advantage: The whole class can wrap the local class, encapsulate the interface of the local class, and provide the new interface |
Disadvantage: Subclasses cannot change the interface of the parent class |
| Disadvantage: The whole class cannot automatically get the same interface as the local class |
Advantage: Subclasses can automatically inherit the interface of the parent class |
| Disadvantage: When you create an object of a whole class, you need to create an object of all local classes |
Advantage: You do not need to create an object of the parent class when you create an object of the subclass |
the understanding of abstract class and interface interface
Abstract classes in the broad sense (including abstract classes or interfaces in Java) are often used to characterize the abstractions we derive from the analysis and design of the problem domain, as an abstraction of a series of concrete concepts that look different, but are essentially the same. Abstract classes used to characterize abstract concepts cannot be instantiated because abstract concepts have no corresponding specific concepts in the problem domain.
Interfaces can inherit interfaces, and multiple inheritance is supported. Abstract classes can implement (implements) interfaces, and abstract classes can inherit concrete classes or they can inherit abstract classes.
The OCP principle (open-closed Principle) is embodied in Java in large part by the design of interfaces and abstract classes, that is, abstract design.
- Abstract classes represent an inheritance relationship in the Java language, which is single-inheritance (normal class and abstract class) and multiple implementations (interfaces) in Java.
- In an abstract class, you can have your own data member, the value of the data member can be redefined or assigned, or it can have a non-ABSTARCT member method, that is, there can be a specific method, whereas in an interface, there can only be static data members that cannot be modified (that is, static final, and must have an initial value. However, data members are not generally defined in the interface, and all member methods are public abstract by default.
- Classes that implement abstract classes and interfaces must implement all of these abstract methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface. If the subclass does not implement an abstract method of the parent class, the subclass must also be defined as an abstract class.
- Interfaces cannot contain static code blocks and static methods, whereas abstract classes can have static code blocks and static methods
- The methods of the interface are public, the method of the abstract class can be public,protected,private or the default package, the abstract method must be public or protected (because if it is private, it cannot be inherited by the quilt class, Subclasses will not be able to implement this method), which by default is public.
- Abstract classes can define constructors, but interfaces cannot.
- An abstract class represents an abstraction of a thing (property and behavior), an abstraction of a class, and an interface is an abstraction of the behavior, a behavior specification, or a behavior contract.
- Abstract classes and interfaces reflect a different design concept. In fact, abstract classes represent "is-a" relationships, and interfaces represent "like-a" relationships.
Interface
- The interface is used to describe all the services provided by the system, so the member constants and methods in the interface must be of the public type, ensuring that external users can access them;
- The interface only describes what the system can do, but does not specify how to do it, so the methods in the interface are abstract (abstracted) methods;
- The interface does not involve details related to any specific instance, so the interface has no constructor method, cannot be instantiated, there are no instance variables, only static variables;
- The variables in the interface are common to all implementation classes, and since they are common, they must be immutable, because the things that change cannot be shared. So the variable is the immutable (final) type, which is the constant.
- The variable cannot be defined in the interface. The attributes in the interface must be constants, which can only be read and not changed, in order to provide a uniform property for the object that implements the interface.
An interface can be seen as a higher level of abstraction over a behavior contract or a protocol. For the modification to close, open to the extension, the interface is an embodiment of the principle of closure. The method of an interface is public abstract by default, and only constants (final adornments) can be defined in an interface. Therefore, the properties of the interface default to the public static final constant, and the initial value must be assigned. Note: Final and abstract cannot appear at the same time.
| Parameters |
Abstract class |
Interface |
| The default method implementation |
Can have the default method implementation |
The interface is completely abstract. There's no way to implement it at all. |
| Realize |
Subclasses use the extends keyword to inherit abstract classes. If the subclass is not an abstract class, it needs to provide an implementation of all the methods declared abstract in the abstraction class. |
Subclasses use the keyword implements to implement an interface. It needs to provide an implementation of all the declared methods in the interface |
| Constructors |
Abstract classes can have constructors |
Interfaces cannot have constructors |
| Differences from normal Java classes |
In addition to being able to instantiate an abstract class, it is no different from a normal Java class |
Interfaces are more abstract types |
| Access modifiers |
Abstract methods can have public, protected, and default modifiers |
The interface method default modifier is public. You may not use other modifiers. |
| Main method |
An abstract class can have a main method and can run it |
The interface does not have the main method, so it cannot be run. |
| Multiple inheritance |
An abstract method can inherit a class and implement multiple interfaces |
Interfaces can inherit only one or more other interfaces |
| Speed |
It's faster than the interface. |
The interface is a little bit slower because it takes time to look for methods implemented in the class. |
| Add a new method |
If you add a new method to an abstract class, you can give it a default implementation. So you don't need to change your current code. |
If you add a method to an interface, you must change the class that implements the interface. |
[Reference 1: Detailed parsing of the differences between abstract classes and interfaces in Java]
[Reference 2:java abstract class and interface differences]
the difference between static nested classes (the static Nested Class) and inner classes (Inner Class)
The following excerpt from the Java programming idea
The static Nested class is an inner class declared static (static), and an ordinary inner class object implicitly holds a reference and only wants to create its enclosing class object. However, when an inner class is static (that is, a nested class), it means:
- To create an object of a nested class, you do not need to rely on the creation of objects whose perimeter classes are created, whereas the usual inner classes need to be instantiated after the external class has materialized
- Non-static perimeter class objects cannot be accessed from objects in the nested class.
- The fields and methods of ordinary inner classes can only be placed on the outer level of the class, so normal inner classes cannot have static data and static fields, nor can they contain nested classes. But nested classes can contain all of these things.
= =, equals (), Hashcode (), and CompareTo ()
= =is an operator that is used to compare two data for equality and to compare whether the memory address represented by a variable is the same
equals: is a method provided by the object class, and equals is equal depending on how the method in the class implements the "override". The Equals method in the string class is the equality of the comparison values, the object class is the root class of the class hierarchy in Java, all classes in Java are subclasses of object by default, the array type is also a subclass of object, and the default equals implementation of the object class is using = = To determine whether the comparison points to the same address, or whether the values of the base type are equal.
1 1 = = 1.0;// established 23 s1= "abc", s2= "abc"; // (S1==S2)
The nature of equals ()
- Reflexive (reflexive): for any non-null reference value X,x.equals (x) must be true.
- Symmetry (symmetric): Y.equals (x) is also true for any non-null reference value x and Y, and only if X.equals (y) is true.
- transitivity (transitive): for any non-null reference value x, Y, and Z, if X.equals (y) is true and Y.equals (z) is true, then X.equals (z) must be true.
- Consistency (consistent): for any non-null reference value x and Y, if the object information used for the equals comparison has not been modified, x.equals (y) will either return true consistently or return false consistently if it is called multiple times.
- Returns false for any non-null reference value x,x.equals (null).
It is important to note that when the Equals () method is override, Hashcode () is also override. According to the General Hashcode () method implementation, equal objects, their hash code must be equal.
Hashcode () method
- During the execution of a Java application, if the information provided by an object to equals is not modified, the object calls the Hashcode () method multiple times, and the method must consistently return the same integer.
- If two objects are equal according to the Equals (object) method, then calling their respective hashcode () methods must produce the same integer result.
- Not requiring two objects that are not equal according to the Equals (Java.lang.Object) method, calling both of their respective hashcode () methods must produce different integer results. However, programmers should be aware that having different integer results for different objects may improve the performance of hash table.
Hashcode is used for hash tables, for example, in the comparison of HashMap, when judging whether the element is duplicated, first use hashcode to determine whether the equality, and then use equals to judge
CompareTo () method
The method of comparing the size that is provided when sorting.
A.compareto (b) >0 indicated A>b,a.compareto (b) =0 indicated A=b,a.compareto (b) >0 indicated a < b
[Reference: Java Improvement Article--equals () and Hashcode () method detailed]
constant Pool, String (immutable Class), StringBuffer, StringBuilder
Constant Pool section
1. class file constant pool : some of the important components of Java's class file are called constant pools, and the constant pool holds two major classes of constants: Literal (Literal) and symbolic (symbolic Reference). Literal comparisons are similar to the concept of constants at the Java language level, such as text strings, constant values declared final, and symbolic references that are concepts of compilation principles, including the following three types of constants:
- Fully qualified name of class and interface (Fully qualified name)
- The name and descriptor of the field (descriptor)
- The name and descriptor of the method
(Refer to Zhou Zhiming "in-depth understanding of JVA Virtual Machine 2nd Edition", chapter 6th)
2. Run a constant pool: class file Constants Pool This section will be stored in the run-time pool where the class loads the incoming method area, and the run-time pool also contains a direct reference to the translation. The virtual machine is dynamically connected when the class file is loaded. When a virtual run occurs, the corresponding symbolic reference is obtained from Chang, and then the symbol reference is converted to a direct reference when the class is created or at runtime parsing and translating into a specific memory address. The run-time constant pool also contains the new constants created during the run (the common string intern method), which is dynamic.
A common string constant pool and integral type Chang (-128-127) can be used to share objects and avoid frequent creation and destruction. \
[Reference 1:java constant pool comprehension and summary]
[reference to several constant pools in 2:java]
Of course, the focus is on "in-depth understanding of JVA Virtual Machine 2nd Edition" 2nd, 6th and 7th chapters (pending further study).
String
Immutable types (immutable), which are thread-safe, are implemented internally using private final char value[], and unless they are constants, a completely new string is regenerated each time it is created, which is less efficient.
Why the string is designed to be immutable: design considerations, efficiency optimizations, security, string constant pooling, hashcode cache acceleration Processing, multithreading security, and more
StringBuffer
Variable, and is thread-safe, efficiency is lower than StringBuilder, need to maintain thread security, lock, synchronization.
StringBuilder
variable, but the thread is not secure.
- Both StringBuffer and StringBuilder inherit Abstractstringbuilder, and StringBuffer most of the methods are synchronized, which is thread-safe, and StringBuilder is not. So, we look at the API can know that StringBuilder can operate stringbuffer, but StringBuffer can not operate StringBuilder, which is the cause of the thread;
- Because the StringBuffer to maintain the synchronization lock, consumes some resources, so the efficiency will be relatively low.
- -The main performance difference between string types and StringBuffer: string is an immutable object, so each time a string is changed, a new string object is generated and the pointer is pointed to the new string object. Therefore, it is best not to use string to change the content of the strings, because each generation of the object will have an impact on the system performance, especially when there is no reference object in memory, the JVM's GC will start to work, and will be degraded.
- -When using the StringBuffer class, the StringBuffer object itself is manipulated each time, instead of generating new objects and changing the object references. So in most cases it is recommended to use StringBuffer, especially if the string object is often changed.
- Usage Policy:
(1) Basic principles: If you want to manipulate a small amount of data, using string, single-threaded operation of large amounts of data, with StringBuilder, multi-threaded operation of a large number of data, with StringBuffer.
(2) Do not use the String class "+" for frequent splicing, because that performance is very poor, should use the StringBuffer or StringBuilder class, which is a more important principle in Java optimization.
(3) In order to achieve better performance, you should specify their capacity whenever possible when constructing StringBuffer or StringBuilder. Of course, if you manipulate a string length (length) of not more than 16 characters, it is not necessary, when the capacity is not specified
(capacity), an object with a capacity of 16 is constructed by default. Not specifying capacity can significantly degrade performance.
(4) StringBuilder is generally used inside the method to complete a similar "+" function, because the thread is unsafe, so after use can be discarded. StringBuffer are mainly used in global variables.
(5) In the same case, using StringBuilder compared to using StringBuffer can only obtain the performance improvement of around 10%~15%, but the risk of multi-threading insecurity. In real-world modular programming, a programmer in charge of a module does not necessarily have a clear sense of whether the module will run in a multithreaded environment, so unless the system bottleneck is determined to be on the StringBuffer, and it is determined that your module will not run in multithreaded mode, can use StringBuilder, or use StringBuffer.
[Reference 1:string, StringBuffer, and StringBuilder]
[Refer to 2:java:string, StringBuffer and StringBuilder]
understanding of static, public static void main (string[] args)
The understanding of static
Static variables: statically variable or class variable
static method, or class method
Static code block: Automatic execution
Static class: Can only be statically internal classes
public static void Main (string[] args) understanding
Public keyword: Indicates that this is a method that can be called externally, primarily by a virtual machine.
Static: Indicates that the class belongs to.
void: Indicates that the method has no return value
The main () method is the entry method for the Java reference program whose parameters are an array of string objects, which the Java compiler requires that must be used to store command-line arguments.
initialization Order of Java programs
Java program Initialization order (parent static variable, parent class static code block, subclass static variable, subclass static code block, parent non-static variable, parent non-static code block, parent class constructor, subclass non-static variable, subclass non-static code block, subclass constructor)
Whether the abstract method can be static at the same time (static), whether it can be a local method (native), and whether it can be synchronized decorated at the same time
Can not. Abstract methods require subclasses to rewrite, and static methods cannot be overridden, so they are contradictory. Local methods are methods implemented by local code, such as C code, and abstract methods are not implemented and are contradictory. Synchronized is related to the implementation details of the method, and the abstract method does not involve implementation details, and therefore is contradictory to each other.
whether static methods can be overridden
belongs to a class, cannot be inherited, cannot be overridden.
Java Basics Point Précis-writers