1. Exception Handling
Jvm provides a garbage collection mechanism. The garbage collection mechanism is part of java memory management and is only responsible for garbage collection.Heap memoryAs for the physical resources opened in the program, such as input/output streams, the garbage collection mechanism is powerless and should be disabled correctly. Usually combined with finally Blocks
When a return or throw statement is encountered when a java program executes try/catch, return or throw causes the method to end immediately. After the system executes the retrun Statement (Note: if the finally statement does not have a return statement, the returned value is here and saved, no matter whether the data is modified in finally or not, if finally has a return, based on the returned results in finally), the system checks whether a finally block exists. If yes, the system immediately executes the finally block, the system will jump back again. The end method is based on the return or throw exception. If return exists in finally, no return is required.
Public class Test {public static int t () {int a = 5; try {throw new Exception ();} catch (Exception e) {return a ++ ;} finally {++ a; System. out. println ("finally:" + a) ;}} public static void main (String [] args) {System. out. println (t (); // finally: 7 // 5} // if return a is added at the end of finally; then t () returns 7
The java language specifies that when a subclass overrides the parent class method, it cannot throw an exception with a larger range than the parent class method.
2. final keywords
Modify data: the compile time is constant. For the basic type, the value of the variable cannot be changed, but the reference of the object variable cannot be changed. If it is a form parameter, the parameter cannot be changed in the function, it is very practical for object variables, because the object variables are passed as references, so your modifications to the object variables in the method will also affect the object variables in the call statement, when you do not need to change the object variable used as a parameter in the method, using final to declare it will prevent you from accidentally modifying it and affecting the calling method.
Modification Method: All private methods in the class are implicitly declared as final and cannot be modified. Therefore, final cannot be overwritten, that is, it cannot be overloaded. Note that during inheritance, methods declared as private cannot be inherited into sub-classes.
Modifier: cannot be inherited
The local variables accessed in any internal class should be modified using final, because for common local variables, its scope stays within the method. When the method execution ends, this local variable also disappears, but the internal class may generate an implicit "closure", which will cause the local variable to continue to exist out of the method where it is located, for example:
public class Test {public static void main(String[] args) {final int a = 1;new Thread(new Runnable() {public void run() {for (int i = 0; i < 100; i++)System.out.println(a);}});}//------1}
Under normal circumstances, after the program executes the code at one point, the main method's life cycle ends, and the scope of a ends, but the new thread has not finished executing, the life cycle of an anonymous internal class instance is not over, and a will be accessed all the time.
3. the java program allows a method to return the java object that calls the method through retrun this; but does not allow direct return of super; or even the use of super as a reference variable.
4. When the program needs to use internal classes, try to use static internal classes instead of non-static ones. When a static internal class is used, the external class is equivalent to a package of the static internal class, so it is convenient to use it. However, the static internal class cannot access non-static members of the external class. Outer. inner in = new Outer. inner (); a common internal Class Object implicitly stores a reference pointing to the peripheral class object that created it. It cannot have static data or static fields, but can have static final.
5. java has some native methods. For these methods, java programs do not provide implementation bodies for this method. Although the java language itself is cross-platform, these methods depend on specific platforms, especially the methods provided by jdk include a large number of native methods.
6. Two Methods for Array initialization: static array initialization and dynamic array Initialization
Int [] a = {5, 4, 3}; int [] B = new int [] {4, 5, 3 }; // The array size cannot be specified. int [] c = new int [5];