JavaSE [Java] · difficult basis points collection

Java Foundation · difficult point


2019-08-03 19:51:39 by rushing

 

1. Some Java keywords

instanceof: used to test whether an object is an instance of a specified type.

native: a method is used to declare a computer-related language (C / C ++ / FORTRAN language) implemented.

(1)break、continue

break: ①switch judgment statement, the switch statement to jump out of the body. ② a loop, the loop breaks out.

Note: When the loop comprises a switch statement, if the break in the switch statement, the end of the switch statement used; to be used as the end of the loop, then the switch setting break vitro.

continue: only the end of this cycle and the next cycle begins. (Remaining unexecuted statements are skipped in this loop, now for the next cycle condition determination)

(2)final、finally

final: the ultimate statement indicating that a final class can not be subclassed, or a final method can not be covered, or can not change the value of a final member. (String class is a final class)

finally: for when dealing with different, a statement sure to be executed to the block. fianlly block without being executed 

note

① final class member variables can be set in the final as needed, but be aware that all members of the methods are final class are implicitly designated as the final method .

② stated as final when you do not want the process to be covered in the subclass. final parent method can not overridden by subclasses, but can be overloaded. Note that, because rewrite the premise that a subclass can inherit this method from the parent class, if the parent class final modification of the method of its access control rights for private, will lead to a subclass can not directly inherit this method, Thus subclass can be defined in the same method name and parameters, then no conflict rewrites the final, but in the subclass redefines the new method with the same name, which itself belongs to the subclass. (Note: private class methods implicitly designated as the final method.)

③ final member variable represents a constant, can only be assigned once, after the assignment value does not change . When a final modification of the basic data types, represents the value of the basic data types of change can not occur at once after initialization; If the final modified a reference type, then it can not let its initialization after point to other objects, but the reference points to the contents of the object changes can occur. Essentially the same thing, since the reference value is an address, final required value, i.e. the value of the address does not change.

④ final modification of a member variable (property) must be displayed initialized. There are two ways to initialize a variable is initialized when declared; The second method is not the initial value at the time you declare a variable, but to assign to this variable in all of this variable constructor where the class the initial value. When the type parameter is declared final, indicating that the parameter is read-only type. Ie you can use this parameter to read, but can not change the value of the parameter.

On the final keyword in Java  

(3)super、this

super: indicates the type of object reference current parent or a parent type of construction method.
this: is a reference to the current object, or the current object's constructor.

note

① If you call other constructors by this keyword, this () statement must be the first clause of the constructor , otherwise the compiler will complain. That is, in the configuration of the this method can only be called once ([parameters]), the second clause is no longer used the this .

If the constructor does not pass this call other constructor explicitly, the compiler does not automatically add this to its default () method call, but it will automatically add method calls to a default Super () !

③ If the constructor does not call the parent class constructor explicitly, then the compiler will automatically default to it with a super () method call. If the parent class has no default constructor with no arguments, the compiler will report an error, Super () statement must be the first clause of the constructor . The first statement of each subclass constructor method is invoked implicitly Super (), if the sub-class constructor of the this is a ([parameter n]), so that the this ([parameter n]) Method the first sentence would have explicitly or implicitly calling super (). If the first or the this ([parameter n]) or one of the this ([parameter m]), so that the this ([parameter m]) method have an explicit or implicit call super (), and so on ...

④ this () and super () are represented by objects, it can not be used in a static environment. Comprising: static variables, static methods, static block.

class Creature {
     public Creature () {
         // Here will automatically call the default constructor of the superclass final Object () 
        System.out.println ( "a biological initialization" ); 
    } 
} 

class Animal the extends Creature { 
     public Animal () {
         / / here will automatically call the parent class default constructor Creature Super () 
        System.out.println ( "initialize an animal" ); 
    } 

    public animal ( int head) {
         the this ();
         the this .head = head; 
        the System.out. the println ( "animal which has a" + head + "head" ); 
    } 

    publicAnimal ( int head, int nose) {
         // the this (); the this can only be called a 
        the this (head);
         the this .nose = nose; 
        System.out.println ( "animal which has a" + head + "and head" + nose + "noses" ); 
    } 
}

 

 

 

 

(4)throw、throws

throw: appear in the body of the function, it represents an exception is thrown.
throws: appears in the function header, in the current method used to declare all the necessary throw.

Normally, when an exception procedure statement appears, the system will passively automatically throw an exception.

. 1  public  static  void main (String [] args) { 
 2      int A =. 5, B = 0 ; 
 . 3      System.out.println (. 5 / B); 
 . 4  }
 . 5  / ** ********** **
 6  system will automatically ArithmeticException throws an exception, an unexpected application termination
 7  ************** * /

 

① throw an exception is thrown represents active, usually occurs within the code block, when the program by a programmer some logical errors active certain type of exception is thrown.

 1 public static void main(String[] args) { 
 2     String s = "abc"; 
 3     if(s.equals("abc")) { 
 4       throw new NumberFormatException(); 
 5     } else { 
 6       System.out.println(s); 
 7     } 
 8 }
 9 /************
10 控制台显示:
11 Exception in thread "main" java.lang.NumberFormatException at......
12 *************/

 

② throws a statement will throw an exception.

public void function() throws Exception{......}

 

When a method may throw an exception with some kind of statement throws an exception might be thrown, then to the upper layer calls its method procedures.

. 1  public  class testThrows {
 2   
. 3      public  static  void function () throws a NumberFormatException {
 . 4          String S = "ABC" ;
 . 5          System.out.println (Double.parseDouble (S));
 . 6      }
 . 7   
. 8      public  static  void main (String [ ] args) {
 . 9          the try {
 10              function ();
 . 11          } the catch (a NumberFormatException E) {
 12 is              System.err.println ( "non-data types can not be cast." );
 13             // e.printStackTrace (); 
14          }
 15      }
 16  }
 . 17  / ** **********
 18 is  the console display:
 19  non-data types can not be cast.
20  ************ * /

 

Both throw and throws the exception handling are negative (negative here is not that bad in this way), they might just throw or throw an exception, but not by the function to handle exceptions, handle exceptions by the real upper caller function to deal with.

Generally, program portions may appear abnormal, use try {...} catch {...} to catch it, and the abnormality process in the catch {...}. Manner may be a simple output statement, the stack may be output e.printStackTrace ();, or other.

If the input is to catch exceptions IO output stream, be sure to try {...} catch {...} finally {...} After the addition of the input and output streams closed.

If the throw thrown by some abnormality in the body of the function, the function name to be used is preferably throws declared to throw exceptions, and to call it a function of the upper layer is processed by catch {...}.

(5)synchronized、volatile

synchronized: represents a piece of code need to synchronize execution.
volatile: indicate that two or more variables must change synchronously.

 

2. break custom tag jump

 1 public class Test {
 2     public static void main(String[] args) {
 3         int i, j;
 4         first: for (i = 4; i > 1; i--) {
 5             second: for (j = 4; j > 1; j--) {
 6                 if (j == 2) {
 7                     break first;
 8                 }
 9                 System.out.println(i + "-" + j);
10             }
11         }
12     }
13 }
14  
15  / * **************
 16  outputs:
 . 17  4-4 of 
 18 is  4-3
 . 19  **************** * /

 

 

3. try, catch, finally mix

try other end of the statement must have a statement (catch block or a finally block) followed. the catch-the try, the try-the catch-a finally, the try-a finally . Wherein simultaneously catch and finally can not be omitted.

 

Guess you like

Origin www.cnblogs.com/yadiel-cc/p/11297545.html