Java notes
Java SE: Java Standard Edition for table-level application and database development;
Java EE: Java Enterprise Edition for enterprise-level development;
Java me: Java handheld edition for embedded and mobile development.
Chapt 1
1) Java is case sensitive;
2) There is an extension when compiling a program in Java, and there is no extension when running the program;
3) the class name in the source program must be the same as the file name.
Chapt 2
1) there is no sizeof () Operator in Java;
2) the Java identifier must start with an English letter and consist of letters and numbers;
3) Switch expressions in Java can use expressions, constants, and enum types;
Chapt 6
1) inheritance is a feature of face-to-face object programming and also a feature of Java;
2) Inheritance occurs between classes. inheritance can be single-layer or multi-layer inheritance;
3) polymorphism means that the object has two States during runtime and compilation, enhancing code flexibility and reusability;
4) Encapsulation means to include variables and methods and access them through unique channels;
5) create a class instance: New Class-Name ();
6) when the used class and the used class are in the same directory, they can be directly used to create class instances;
7) when using other classes in different directories: Import defined-package-name.class-name;
8) use the class provided by the system. Some do not need import, and some need import, depending on the situation;
9) after an object is created using the new keyword, a default initial value is generated;
10) local variables are created in the method body and cannot be accessed in the method body;
11) no default value is assigned to a local variable;
12) differences between local variables and member variables:
A) member variables can be modified by public, private, final, static, etc;
B) local variables can only be modified by final;
C) member variables are created in the heap, and local variables are created in the stack;
D) member variables have system default values;
E) The local variable does not have the system default value and must be assigned manually;
13) the main program entry of Java is a method. The parameter is string [] ARGs, which is a special method;
14) parameter transfer of Methods: when the transfer type is of the basic type and the object reference type, value delivery is used;
15) the parent class of any class is an object;
Chapt 7
1) create a package: Package package name
2) Note the following when creating a package:
A) The package statement must be in the first line;
B) only one package statement can exist, but not multiple;
C. Separate the packages with multiple layers;
3) How to Use the package and introduce the package:
A) import package name. *; // reference all classes under the package
B) import package name. Class Name; // reference the class with the specified class name under the package
C) a program can have multiple import statements;
D) the import statement must be used after the package statement;
4) Static Introduction: Import static package-name.aaa. * or import staticpackage-name.aaa.function;
5) Static introduction includes all static methods and static variables of the class;
6) The default access permission is similar to the public permission. The difference is that the default access permission cannot access the classes in different packages;
7) the value of a final keyword-modified member variable cannot be changed and must be initialized. The final keyword-modified member variables will not be initialized by default;
8) variables modified by the final keyword are either initialized directly during definition or initialized in the constructor;
9) The final modified local variables are very similar to the member variables, so that their values cannot be modified. Once the modified local variables are assigned a value, they cannot be modified, if you do not assign values to them at the beginning, you can assign values to them before use. This is the difference between local variables and member variables;
10) the final keyword modifier method can inherit from the quilt class, but cannot be overwritten;
11) Static static means that there can be only one copy in the memory;
12) static variables belong to the class and do not create copies with the creation of objects;
13) the static modified member variables do not belong to objects, but to classes;
14) static member variables can be accessed using the following methods: Class Name. static member variable name or static member variable name;
15) static member variables depend on the loading of objects but on the loading of classes. When the class is not loaded, the static member variables belong to the class, therefore, the lifecycle of static member variables is longer than that of non-static member variables. static member variables can only be used in static methods;
16) Note that if you have a long life cycle, you can use a short life cycle. Otherwise, you cannot.
17) Static constants are unique, unchanged, and only one copy of data. They are modified using static final;
18) Static constants static and final are not distinguished in sequence. Static modifier variables belong to classes, while final modifies the value of member variables and cannot be modified;
Chapt 8
1) inheritance aims to reuse code and improve efficiency;
2) Java provides single inheritance, and multiple inheritance can be implemented through interfaces;
3) The inherited classes in Java are called superclasses, And the inherited classes are called subclasses;
4) class inheritance is modified by the keyword extends. The extends keyword indicates that the former has the public member variables and methods of the latter;
5) Class Name extends parent class name;
6) The parent class of all classes is object. If the class has no inheritance relationship, the parent class is object, and the package path of the object class is Java. Lang. object;
7) modifiers are used to modify the access restrictions and status of the current member variables. They include public, private, default, and protected;
8) Public indicates that the modified member variable is of the Public type, so this member variable can be accessed in any package, including sub-classes;
9) Private indicates that the modified member variable is of the private type, indicating that no member variable can be accessed by any class except this class, which is highly protective;
10) default indicates that the member variable is of the default type. If no modifier is added to the member variable, the member variable is modified to the default type, classes or subclasses in the same package can be accessed. However, classes or subclasses in different packages do not inherit the member variables and cannot be accessed;
11) protected declares that the member variables are of the protection type. They are the same in the same package and the Public type. The member variables of the protected type in different packages can only be accessed through subclasses, this modifier is different from other modifiers;
12) Class Methods Modified by static cannot be overwritten, but will be inherited to the subclass;
13) method overload. The method name is the same. The method is called differently based on the number and type of the method parameter list and the return value;
14) when the override method is used, if the return type is the basic type, the return type must be the same when the subclass is rewritten. If the return type is a reference, the type must be the same or the parent class should return the derived class referenced by the type, that is, the subclass;
15) the static method cannot be rewritten to supplement the code. Test results: the static method cannot be rewritten, even if the subclass writes the same method as the parent class, it only hides the static method of the parent class and can still be called by calling the static method name of the parent class. However, if it is not static, after rewriting, this is equivalent to the method being changed. In any case, your call is a method after you overwrite it, instead of calling the method to be overwritten in the parent class, the difference is that the static method is hidden because it belongs to the class and is directly overwritten. Therefore, the static method cannot be overwritten;
16) When an inheritance relationship occurs, you can use the super keyword to reference the method of the parent class;
17) differences between overwrite, overload, and rewrite:
A) The override method is identical in parameters and return values, and the exception is thrown, and cannot be private. Otherwise, the subclass defines a new method without overwriting;
B) the overload is to call different methods based on the parameter list;
C) rewrite is a method that exists in the parent class and also in the subclass. It adopts a multi-state form at runtime, the method to be rewritten cannot have higher permissions and more exceptions than the method to be rewritten;
18) Relationship between final and inheritance:
A) when the final keyword modifies a class, it indicates that its class cannot have a subclass;
B) when the final keyword modifier is used, it indicates that the method cannot be rewritten;
C) the object reference of the member variable modified by the final keyword cannot be modified;
D) the methods in the class modified by the final keyword are modified to final by default;
19) Abstract abstract class: the abstract class must contain at least one abstract method, so that its subclass can implement this abstract method. Of course, some methods can also be defined in the abstract class;
20) Abstract abstract classes cannot be instantiated. If the subclass of an abstract class does not implement the abstract method of the parent class, it must be an abstract class;
21) polymorphism: Based on inheritance, the superclass object is dynamically bound to the corresponding subclass method at runtime;
22) static and final methods are bound during compilation, while other methods are bound at runtime to dynamically determine the type;
23) reflection mechanism: similar to a magnifier, the class is loaded by class name to obtain the method and other information of the class;
24) Generic: the container has a default type. You do not need to convert the type when specifying the type for the container;
Chapt 9
1) interface: a set of functions, similar to a provision;
2) define an interface:
A) interface modifier interface Interface Name
{
// Declaration of member variables and methods;
}
3) the interface modifier is the same as the class modifier, and the interface is the keyword defining the interface;
4) The member variables of the interface are of the public static final type by default;
5) the interface cannot be declared as final, because the final type must be implemented;
6) The interface definition follows the class definition principle. The interface can also be inherited. The class cannot be inherited but can be inherited at multiple layers. The interface can be inherited at both layers;
7) an interface is a special abstract class with abstract methods and common methods;
8) How to Implement the interface:
A) Class modifier class name implements Interface Name
9) The header file declaration in the interface and C/C ++ has the same meaning as the C file definition;
10) abstract classes and interfaces are similar. abstract classes require other classes to implement methods in abstract classes. interfaces are special versions of abstract classes, and all methods in interfaces are abstract;
11) Judgment type: instanceof is generally used to determine the object reference type in the code when the polymorphism occurs;
12) How to Use instanceof: The reference instanceof class or interface of the object. The return type is boolean;
13) instanceof cannot compare basic data types. The right side of the instanceof keyword can only be a class or interface;
Chapt 10
1) constructor: it is the constructor of the class. It can have modifiers. The constructor name must be the same as the class name and cannot have returned values, parameters, or none;
2) The constructor can carry modifiers. The modifier permissions are consistent with the member variables and methods;
3) differences between constructors and common methods:
A) The constructor is used to create an object instance of the class;
B) The constructor does not return values, and void does not;
C) The constructor name must be the same as the class name;
4) instantiate an object: New Constructor (parameter list), which matches the constructor according to the parameter list;
5) after using import to introduce the required classes in different packages, the method of using the constructor is the same as that in this class. Of course, the constructor permission must be public;
6) When an inheritance relationship occurs, the constructor method of the subclass calls the constructor method of the parent class by default. After the call ends, the constructor method of the subclass is called;
7) the constructor overload is almost the same as the method overload: it is determined by the parameter table, but the constructor cannot be inherited;
8) when calling the constructor, the virtual machine determines which constructor method to use based on the parameter list;
9) because of the inheritance relationship, the virtual opportunity first creates the parent class object. By default, the super () method is called to initialize the member variables of the parent class, return to the subclass to call the constructor method and perform initialization;
10) if every displayed definition constructor exists, the compiler will generate a non-argument constructor by default. Once the displayed definition constructor is displayed, the compiler will not generate the constructor;
11) writing a non-argument constructor for each class is a good habit;
12) mona1 mode: only one copy of the object exists in a period of time. The constructor is modified to private and a public method is used to return the reference of the object;
13) constructor running status in the program:
A) load the parent class of the object to be created, and the member variables and other inheritance relationships;
B) load the static block and static member variables of the class and initialize them;
C) after loading static blocks and static member variables, create an object, load non-static member variables, and initialize them;
D) execute the method body of the constructor. After completion, the object of this class is created;
E) The execution sequence of the parent class is the same as that of the class. It is a recursive process;
Chapt 11
1) when using try-Catch Block to catch exceptions, note the following:
A) if no exception occurs, the content in the catch statement will not be executed;
B) prevent the program from exiting automatically;
C) when multiple catch statements are used, put exceptions with a relatively small scope in front and exceptions with a relatively large scope in the back;
D) The Block specified by finally stores the statements that will be executed;
E) Try-catch-finally cannot contain any statement except comments;
2) exceptions can be divided into capture exceptions and uncaptured exceptions from a large perspective;
3) all Exception classes inherit the throwable class, while the throwable class inherits from the object class. The throwable class has two direct subclasses: the error class and the exception class, there is a runtimeexception class in the exception class. Apart from the direct and indirect subclasses of the runtimeexception class in the direct and indirect subclasses of the exception class, both are caught exceptions (exceptions must be handled ), other exceptions are not caught;
4) handle caught exceptions. If the exception cannot be caught, but the exception is still caught in the program, the exception will be considered as an error;
5) uncaptured exceptions include the error class, the direct and indirect sub-classes, and the runtimeexception class, and the related direct and indirect sub-classes. errors are usually caused by hardware running errors, runtimeexception is usually an exception caused by running the program;
6) Exceptions not captured can be processed without exception handling. If exception handling is not performed, there will be no problem during compilation, but errors will occur during runtime;
7) throw an exception: in some cases, the exception does not need to be processed immediately, but must also be processed. In this case, an exception can be thrown;
8) when a program segment throws an exception, if you cannot handle the exception, you can throw the exception to the previous layer and know that the exception is thrown to the main method. If the problem persists, the program is interrupted and an exception is displayed;
9) throw an exception using the throws and throw statements:
A) throws are used in method declaration statements to throw an exception;
B) throw is thrown by using an exception in the method body;
10) to create a custom exception class, you must inherit the exception class. The exception class contains many methods to obtain debugging information, such as printstacktrace (), tostring (), getmessage () and so on;
Chapt 12
1) when a class is a non-static member of another class, this class is a non-static internal class;
2) When analyzing an internal class, there are two aspects: one is itself a class, and the other is externally a member of a class;
3) Local internal classes in static methods: note that the lifecycle of static members is longer than that of non-static members;
Chapt 13
1) The main method in each program is a thread, which is generally called the main thread. Multiple subthreads can be started to execute in the main thread;
2) There are two methods to define a thread: one is to inherit the Thread class, and the other is to implement the runnable interface;
3) Like defining a thread, there are two ways to create a thread object;
4) inherit the Thread class definition thread. There is a run method in the Thread class. You need to override this method in the defined thread. In the re-written run method, execute the statement to be executed by this thread. After the thread starts, the program in the run method is called an independent execution thread;
5) in the inherited Thread class definition thread method, creating a thread object is the same as creating a common object. This method is only applicable to methods that inherit the Thread class definition thread;
6) define a thread by implementing the runnable interface: There is an abstract run method in the runnable interface. This run method must be implemented when the runnable interface is implemented, this run method serves as an execution thread;
7) both methods need to define a run method. Whether it is by rewriting the parent class method or implementing the interface method, the run method is a thread entry and is required by the thread;
8) in a thread defined through the runnable interface, directly creating an object is not a thread object. To create a thread object, you must use the Thread class:
A) Class Object implementing the Interface Class-name = new class-Name ();
B) thread t = new thread (name );
9) create a class object that implements the runnable interface, and use this object as a parameter of the thread class to create a thread object;
10) the run method of the class can still be called for running, but this does not mean that a new thread is created. To start a thread correctly, you need to call the start method of the thread object;
11) after the start method is called, the thread is started. The thread is executed in parallel with the main method, and the program becomes a multi-threaded program;
12) the thread can only be started once, that is, the start method can only be called once. When the start method is called multiple times, an exception occurs;
13) run multiple threads at the same time, define multiple classes that implement the runnable interface, create multiple thread objects, and call the start method with the corresponding thread object, multiple Threads are started at the same time;
14) when multiple threads are running at the same time, the running result is not unique. It is not clear which thread to execute first;
15) thread lifecycle: new status, preparation status, running status, waiting/blocking status, and death status:
A) New State: After a thread object is created, it is in the new State. After the start method is called, it enters the preparation state;
B) preparation status: After the start method is called, the newly created thread enters the preparation status. threads in the preparation status may be selected by the system to run at any time to execute the thread;
C) running status: Once the preparation thread is selected by the system and the CPU time is obtained, it enters the running status. In the running status, the statement in the thread-class run method is executed, the thread may be scheduled back to the preparation status by the scheduler at any time;
D) waiting/blocking status: the thread scheduling method schedules a running thread to the waiting/blocking status. After a thread in the waiting/blocking status is released, it does not return to the running status immediately, but the preparation status, waiting for System Scheduling;
E) dead state: After the run method in the thread is executed, or after the program terminates abnormally, the thread enters the dead state. The thread in the dead state cannot start the thread using the start method, but it can be used as a common class;
16) thread scheduling: includes sleep methods, priority setting, concession methods, etc.
A) sleep method: When the thread is in the running state, call the sleep method to change the thread from the running state to the waiting/blocking state, thus stopping the program, the sleep method takes longer to sleep the thread than to pass the parameter, because the thread enters the preparation state and waits for the system to schedule the thread;
B) The sleep method is overloaded. It is a static method that does not depend on objects. To put a thread into sleep state, the thread is not allowed to call the sleep method, but to execute the sleep method, then, the sleep method may cause exceptions to be captured. Therefore, the sleep method must be used to handle exceptions;
C) thread priority: in many systems, thread scheduling is performed by priority. Different threads have different priorities, A thread with a higher priority will occupy more CPU and execution probability;
D) in Java, priority is represented from 1 to 10. A larger number indicates a higher priority. If no priority is set, the thread priority is 5, which is also the default value of the thread, however, the default value of the sub-thread is the same as that of the parent thread;
E) Call method: Public final void setpriority (int I); To set the priority;
F) There are two methods of thread concession in Java-yield () method:
I. yield concession method: it is determined by the system to let the thread out of the current CPU, and which thread the CPU is to be given to. Using yield will make the thread go from running to preparation;
Ii. Public static void yield (); static methods are also irrelevant to objects and called by class names;
III. The yield () method may fail, because the system may choose to re-run the thread that has just entered the preparation status;
G) another thread concession method in Java-join () method:
I. Join concession method: Give the CPU resources of the current thread to the specified thread;
Ii. Public final void join ();
Iii. Public final void join (longmills );
Iv. Public final void join (longmills, int Nanos );
V. join without parameters indicates that other threads are executed after the thread completes execution, and the parameter indicates the execution thread that is concession to the parameter time;
It took me two days to read a spam book, a bunch of errors, and now I am probably familiar with Java, which is similar to C ++. Make a note for future reference.