Introduction
Drip, the next day card.
Five, key words
-static
-Initialization order
-Static variables and static statement blocks take precedence over instance variables and normal statement blocks, and the order in which static variables and static statement blocks are initialized depends on their order in the code.
-In the case of inheritance, the order of initialization is:
Parent class (static variable, static statement block)
Subclass (static variable, static statement block)
Parent class (instance variable, normal statement block)
Parent class (constructor)
Subclass (instance variable, normal statement block)
Sub-class (constructor)
-Static variable private static int y;
-also called a class variable, which means that the variable belongs to the class, all instances of the class share a static variable, which can be directly visited by the class name; static variables in memory
There is only one copy.
-static method public static void Func1 () {}
-Static methods exist when the class is loaded, and it is not dependent on any instances. So a static method must be implemented, which means it cannot be an abstract method
-only static fields and static methods of the owning class can be accessed, and the This and super keywords cannot be in the method.
-Static statement block static {System.out.println ("123");}
-Static statement blocks run once during class initialization.
-Static internal class static classes Staticinnerclass {}
-A non-static inner class relies on an instance of an external class, while a static inner class is not required.
-Static inner classes cannot access non-static variables and methods of external classes.
-Static Guide Package import static com.xxx.classname.*
-simplifies code by eliminating the need to indicate ClassName when using static variables and methods, but with significantly less readability
-final
-Declaring fields
-For basic types, final makes the values unchanged
final int x = 1;
x=0; Here will be an error: Cannot assign value to final variable ' x '
-For reference types, final makes the reference unchanged and cannot reference other objects, but the referenced object itself can be modified.
Final A y = new A ();
Y.A = 1;
-declaration method
-the Declaration method cannot be overridden by a quilt class.
The-private method is implicitly specified as final if the method defined in the subclass is the same as a private method signature in the base class, at which point the child class's
Instead of overriding the base class method, the method defines a new method in the subclass.
-Declaring classes
-declares that the class is not allowed to be inherited.
Vi. Common methods of Object
-Main methods
-public boolean equals (Object obj)
-public native int hashcode ()
-public String toString ()
-protected native Object clone () throws Clonenotsupportedexception
-native Keywords:
-A native method is a Java interface that calls non-Java code. A native method means that the implementation of the method is implemented by a non-Java language, such as
C or C + + implementations.
-When defining a native method, the implementation body is not provided (compared to defining a Java Interface) because its implementation is in the non-Java language outside
Implementation of the interface.
-equals ()
-Comparison with NULL
-X.equals (NULL) result is false for any object x that is not NULL
X.equals (NULL); False
-equals () and null distinguishing
-for basic types, = = Determines whether two values are equal and the base type does not have the Equals () method.
-for reference types, = = Determines whether two variables refer to the same object, and Equals () determines whether the referenced object is equivalent
-hashcode ()
-The primary role is to work properly with a hash-based collection, such as HashSet, HashMap, and Hashtable
-The Hashcode () method should always be overridden when overriding the Equals () method to ensure that the equivalent two object hash values are equal.
-hashcode () returns a hash value, while equals () is used to determine whether two objects are equivalent. Equivalent two object hash values must be the same, but hash values are the same
The two objects of the
are not necessarily equivalent.
-tostring ()
- Converts the data type of the current variable to a string type, and also facilitates printing output
-clone ()
-Official explanation
-Returns a copy of the object to be cloned, Cloned types rely on cloned objects
-Pitfalls
The -clone () method is not a method of cloneable interface, but a protected method of Object. The Cloneable interface only prescribes this method.
-When using Clone (), be aware that you need to throw exceptions, and force type conversions
-cloneable ()
-Implement the (implements) Cloneable interface on the object that requires clone, and then add the Clone method to the class, and only call
in the method.
Super.clone (), can be implemented according to their own needs
-Shallow copy
-Copy object and reference type of original object reference the same object
-Deep copy
-the reference type of the copied object and the original object refers to different objects.
Seven, abnormal
-Check for exceptions: Do not handle compilation cannot pass
Non-check exception: Do not handle compilation can pass if there is thrown directly to the console
Run-time exception: is a non-check exception
Non-run-time exception: is an inspection exception
-throwable can be used to represent any class that can be thrown as an exception, divided into two types: Error and Exception.
-error
-Refers to an error that the program cannot handle, indicating a significant error that occurred while the application was running. For example, outofmemoryerror that occur when the JVM is running and
Socket programming occurs when a program cannot handle errors such as port occupancy.
-exception
-Divided into runtime exceptions and compilation exceptions
-run-time exception
-that is, runtimeexception and other exceptions. This type of exception is not detected by the compiler when the code is written, it is not required to be captured
, but the programmer can also throw the catch as needed.
Common runtimeexception are: nullpointexception (null pointer exception), ClassCastException (type conversion exception),
Indexoutofboundsexception (Array out-of-bounds exception).
-Compile exception
Exceptions outside of-runtimeexception. Such exceptions are compiled with the compiler prompting the need to capture and compile errors if not captured. Common compilation
Exceptions are: IOException (stream transfer exception), SQLException (Database operation exception), and so on.
-java mechanism for handling exceptions:
-Throws an exception and catches an exception (try---Catch or try---catch---finally block), the exception that a method can catch, must be Java code
An exception thrown on a certain premises. Simply put, the exception is always first thrown and then captured.
The difference between-throw and throws:
-throws is a declaration that a method may throw an exception, throw is a statement that throws an exception (which can be custom to inherit exception, or Java itself to
Out of the exception class)
-throws represents the possibility of an exception, which does not necessarily occur; throw throws an exception, and a throw throws a certain exception.
Viii. features of the Java editions
-java5
-an important version that has updates such as generics, enumerations, auto-boxing/unpacking, mutable parameters, annotations, foreach loops, static imports, new threading models, and so on.
-java6
-Chicken version, with JDBC4.0 update, complier API, websevice support enhancement and other updates.
-java7
-Some of the more important updates, such as exception handling, increase the suppressed exception, capture multiple exceptions, Try-with-resource automatically release resources, and apply
The G1 garbage collector, switch can use the string type, the generic auto-judgment type, the Fork/join framework to subdivide the task and use multiprocessor processing, support two
input literals, etc.
-java8
-many new features, such as compilers, class libraries, development tools, and JVMs (Java virtual machines).
-Most importantly, JAVA8 starts to support lambda expressions
- JAVA9
-java Platform-level module system, linking, Shell: Interactive Java REPL, improved Javadoc, etc.
-Detailed features: Java version features