Chapter 2 All things are all objects. All objects must be established by you. Where is the register: We cannot control in the program 2) Stack: Store the basic type of data and object's Reference, but the object itself is not stored in the Stack, but stored in Heap 3) HEAP: Store NEW Data 4) Static Storage: Static member stored in the object 5) Constant Storage: Storage constant 6) NON-RAM: Hard disk or the like permanent storage space 2. Special case: Basic type Basic Type Data is stored in Stack, which is stored. When the object is generated, only the reference to the object is stored in the STACK, used to point to an object, and the object itself is stored in the HEAP. 3. Arrse in Java When you generate an array of a storage object, the actually generated is actually an array of reference. After the quota is built, each ofes each Reference will be automatically set to null, indicating that "do not point to any object". two. Create a new data type: Class 1. Data members and functions 1.1 Basic members' default 1) When a member of the Class is a basic type, even if you don't provide the initial value, Java still guarantees that it has a default. 2) Only when the variable identity is "member within the class, Java guarantees the initial value for this variable. II. Function (mehtods), Return Values 1. Return value is transmitted When the object is the object, the object's Reference. IV. Note With Embedded Document Java to provide two annotations: / * xxxx * /, // xxxx
Chapter 3 Controls Process Process One. Using the Java Operator 1. Relational Operators 1.) When comparing the two objects to compare the relationship operators, comparison is Object Reference, such as Integer N1 = New Integer (3); Integer N2 = New Integer (3) System.out.Println (N1 == N2); the result is False because the two Object Reference (N1 and N2) values are different 2) The default behavior of QUALS () is also compared to Refactor. However, Class in Java overwrites the equals method, such as Integer n1 = new integer (3); integer n2 = new integer (3); system.out.println (n1.quals (n2)); // value true TRUE 2. Logic operators 1) can only apply both, or, not, on the Boolean value. If there is a non-boolean value on both sides of the logical operator, it will be wrong, such as int test1 = 1; system.out.println ((TEST && 1 <2); // Edited error, Test is a non-boolean value 3 Displacement Operator If the displacement object operated is CHAR, BYTE, SHORT, the value will be promoted to int, the calculation result will be int. II. Process Control 1. Iterative (Iteration) 1.1 comma operation CCM operator can only be used in INITIALIZATION and STEP in the control expression of for loops, such as: for (int i = 0, j = i 1; i <5; i , j = i * 2) 1.2 Break and Continue Break indicate an exit loop; Continue means exiting this loop and come back to cycle start position. 1.3 Label Label only works before it is an iterative statement, and inserting any statements between Label and iterative statements will not work 2. The selector in Switch Switch must be int or CHAR type, such as float i = 2; switch (i) // will be wrong, because i is not an int or char 3. Calculation details 1) from FLOAT or DOUBLE Turning to an integer value, it is always carried out in a completely discarded decimal manner. 4. Math.random () output range is [0, 1].
Chapter 4 Initialization and Cleaning One. To ensure the initialization of the constructor, if a Class has a constructor, Java will automatically call its constructor before the user has the ability to operate, so it will ensure that the initialization action must be carried out. two. Method Overloading 1. Distinguishing the overloaded function Since the two functions can only be distinguished from the privileges of the function name and the function, the overload function has the same function name, so each overload function must have a unique quoter column. 2. DEFAULT Construction Function 1) The default constructor is a constructor without any quotes. If the Class you develop does not have any constructor, the compiler will automatically generate a default constructor for you. 2) If you define any constructor yourself (whether there is no quota), the compiler will not generate a default constructor for you. 3) If a class is defined, such as Class Bush {Bush (INT I) {}} wants to use new bush (); to generate an instance of Class, an error is generated. Since the constructor has been defined when defining the Class, the compiler will not generate a default constructor for the Class. When we use new bush () to generate an instance, try to call the default constructor, but there is no default constructor in the class, so it will be wrong. Such as: Class Sundae {Sundae (INT i) {}} public class {public static void main (string [] args) {// sundae x = new sundae (); will compile errors = New Sundae (1);}} *: When you define a Class, if you define your own constructor, it is best to define a default constructor 3. Keyword this 1) This is only used within the function, which can get the Object Reference that evokes this function. 2) In the constructor, you can call other constructor in the same class through this, such as Public Class Flower {Flower (InT Petals) {} Flower (INT PETALS, STIT) {// Petals ; Calling another configuration function must be at the most positional position is (PETALS); // this (ss); error occurs, because only one constructor can be called in a constructor}} **: 1) In the constructor call another constructor, the calling action must be placed in the most start 2) Calling constructor 3 in any function outside the constructor can only call only one constructor 4 in one constructor. 4. The meaning of Static cannot call the Non-Static function in the Static function (reverse feasible).
three. Cleanup: Finalization and Garbage Collection 1) Your object may not be reclaimed only when the program is not in memory, the garbage collector will start the memory space that is no longer used. . The space occupied by an object may never be released, because your program may never approach the memory of the memory, and the garbage collector is completely not started to release the memory occupied by your object, those The space will only return it to the operating system when the program terminates 3) only use Finalize () only when using native method (Native Methods). four. Member Initialization 1) The variables in the function will not be initialized automatically, such as Void f () {INT i; i ;} will have compilation errors, because i is not initialized. 2) Class's data members will be automatically initialized, and the specific conditions are as follows (see P220 example): Basic model: Boolean: False, Char: Null (/ U00), Byte: 0, Short: 0, Int: 0, long: 0, Float: 0, Double: 0 Object (Reference): NULL 1. Initialization Sequence 1) All variables will definitely complete the initialization (see P233 example) 2 when any function (even constructor) is called, first initialize the Static member variable in the class, and then execute All initializations at the Static Data Definition, Finally, STATIC Block, all of which are only performed when the object is generated for the first time. 3) Automatically initialize other member variables in the class. 4) Execute all initialization actions that appear in the data definition. Such as: INT i = 1; the execution order is to automatically initialize i to 0, and then execute the initialization operation at the data definition, initialize the 1. 5) Execute a Non-Static Block 6) call constructor. Example: Class Cup {CUP (Int Marker) {System.Out.println ("CUP (" Marker ")");} Void F (Int Marker) {system.out.println ("f (" marker ")");}}
Class Cups {Static Cup C1 = New CUP (11); Static Cup C2; CUP CUP C2; CUP C3 = New CUP (33); CUP C4; {C3 = New CUP (3); C4 = New CUP (4);} Static {C1 = New CUP (1); C2 = New CUP (2);} CUPS () {System.out.Println ("CUPS ()");}}}} public class explicitstatic}} public class explicitstatic}}} public class explicitstatic}} System.out.println ("INSIDE Main ()"); cups.c1.f (99);} static cups x = new CUPS (); static cups y = new CUPS ();} Result is: CUP (11) CUP (1) CUP (2) CUP (33) CUP (3) CUP (4) CUPS () CUP (33) CUP (3) CUP (4) CUPS () INSIDE Main () f (99) 2. Initialization of Array 1) You cannot specify the size when defining an array. Such as int [4] IARR = {0, 1, 2, 3}; 2) For basic types, the array is stored in the data itself; for the Class array, the array is just an array of Reference Reference. Any array must be initialized, and an array that does not perform initialization will generate runtime errors, such as: int [] IARR; System.out.Pritnln (IARR [0]); // Generate errors, because IARR has not initialized array initialization In any place, the array can be initialized by the following method: a) int [] IARR = {1, 1, 1, 1}; // The length of the array is the number of {} elements B) INT i = 10; INT [] IARR = new int [i]; // The length of the array can be variables (this is not in C / C ) System.out.Println (IARR [0]); // otr [0] is an int, Automatic initialization value is 0 integer [] IARR2 = new integer [i]; system.out.println (IARR2 [0]); // otr [0] is a Reference, automatically initial NULL
I) For basic unrecognizes, New generates an array for storing data; otherwise, only the array of Reference is stored. II) New can be used to initialize basic arrays, but cannot produce basic type data of Non-Array. c) int [] IARR = new int [] {1, 1, 1, 1}; integer [] IARR2 = new integer [] {new integer (1), new integer (2)}; 3. MultiDimensional Arrays multidimensional array can be different, such as: integer [] [] [] A5; A5 = new integer [3]; for (int i = 0; i Chapter 5 Hide implementation details. Java Access Specifiers Java has four access rights of public, protect, friendly, private, and this four access rights are getting smaller. 1. Friendly 1) If the data member or method within a class does not have any permissions, its default access is Friendly. All other classes in the same package can access Friendly members, but Classes other than Package is shaped with private. For Classes under the same folder, Java will automatically first see these Classes for the Default Package that belongs to the directory, can call each other Friendly members. As the following two classes are in the two files of the same folder, although there is no introduction of Package, they are affiliated with the same default package. Class Sundae {// The following two methods default to Friendly Sundae () {} void f () {system.out.println ("sundae.f ()");} public class} c (String ");} public class @} (String"); ] args) {sundae x = new sundae (); xf ();}} 2. Public: You can call 3 in any class 3. PRIVATE: Private members can only be called inside the Class of members belonging, such as: Class sundae {private sundae () {} // can only call Sundae (INT i) {} Static Sundae Makasundae () {Return New sundae ();}} public class Icecream {public static void main (string [] args) {// sundae class in the constructor Sundae () is Private, so it cannot be used to initialize // sundae x = new sundae () Sundae y = new sundae (1); // sundae (int) is Friendly, you can call Sundae Z = Sundae.makasundae ();}} 4. Protected: While having Friendly access, it can be accessed by Subclass. That is, it can be accessed by Classes in the same package, and can be accessed by the Protected member's Class Subclass