Java object-oriented programming is used to noun understanding: object identity = = Variable name of an instance, object behavior = = Method of a class, object state = = The data field in the class (strictly speaking, can change the data field);
Relationship between classes: Use_a,has_a,is_a three types
The math class in Java is a bit special, it encapsulates functionality and doesn't involve data fields, so you don't have to hide the data
The object variable name in Java is more like a pointer (like), after declaring an object instance, you must construct the object with new and then use = to link the two, or you can connect two different object variables with =, but you need to be aware that, like pointer assignment, two variables will share the same object instance from this point. The object variable does not contain an object instance, but instead refers to an object instance that explicitly sets the object variable to null to indicate that the object variable does not refer to any object instance, noting that the local (object) variable is not automatically initialized and needs to pass new or null (must lowercase) Assign a value to it
Object variables in Java are all placed in the heap, and when one object contains another object variable, that object contains a pointer to another heap object.
Deep-Copy objects in Java must take advantage of the Clone method
The date class in Java is used to represent a point-in-time Gregoriangalendar class used to represent a calendar, and the date class provides before, after methods for comparing points of time, as well as methods such as Getday, but the author does not advocate the use of
Change method: Methods for modifying Class object data fields
Accessor methods: Only methods that read the data fields of the class object are syntactically indistinguishable from the former
In the same source file, only one public class is allowed to load the main method, When compiled, the corresponding. class file is created for all classes in the source file, and then the class name of the main method is given to the bytecode interpreter to start, but the main method can be included in other classes so that the unit test can be selected by the IDE, and then the target class is tested, and then main in the target class will play a starting role.
For a Variant for loop, it is found that the modification of the integer array in the loop block is not saved, but if it is the object, it will retain the modification marks.
Note that the constructor is not a return value
Here is a causal relationship: because all objects in Java are placed in the heap, they are created with new, which differs from C + +
A variable with the same name as the data field cannot be declared in the constructor in Java, although no error is made.
All methods in Java must be defined inside the class (shell classes), but this does not mean that all methods are inline.
Attention to accessor methods: accessor methods should not directly return the object of the State field (data field), because the receiving variable will share memory with this object, so that the object can be indirectly modified through the receive variable (although private) should do is to return a copy of this object, here using the. Clone method;
Access to class problem: A method of a class can access data fields in all instances of this class of objects. Instead of a data field in a particular object instance. different from C + +
If the static class is stored in this type of data, all object instances of this class will share this data, referenced by the class name. The reference is used to emphasize that this data is a direct class domain that is not a special object instance, and a static constant type can make public variables in a class safe.
Although it is theoretically impossible to modify a static constant, if the SetOut method can be used to break this convention, the principle is that this method is a local method that bypasses the Java implementation
For static methods: Object instances are not required, directly through the class name. Call, there is a cause and effect: so you cannot access the data field in the object instance, there is a special case: unless there is static data in the data field in the object instance, the static method can still access this data. Like static variables in the same class, calling a static method is recommended to call directly using the name of the classes to emphasize that they do not depend on object instances, which is the purpose of static methods.
Summary of 20: Requirements for using static methods: 1. Data fields that do not have access to the instance. 2. Concession, access only the static domain in the data domain
Here's a method: Factory method.
There is only one form of delivery for parameters in Java: Passing by value (copying first and then passing the copied value). So there is a cause and effect: So the parameters of the method if it is a basic type, then it will not be modified, but there is a seemingly special case but it is actually reasonable situation: if the parameter of the method is an instance object, you can implement the modification of its data field, The reason for this is that the object instance itself is a pointer (equivalent) to the existence of a reference, so even if it is copied, the result is a reference, and the two variables share a memory area at the same time, so the modification of this memory area in the method is valid
Note that in Java, the default initialization for a domain (the data field in an object instance, or a higher layer: A variable stored in a heap) can be initialized to 0,boolean default initialization to False, the object defaults to NULL, but the local variable is not initialized by default. And the author does not advocate the use of default initialization because it affects the readability of the program. At this point, it is found that the domain mentioned in the white Paper should have a common feature that is stored on the heap, such as an array, will also be initialized by default, because the array is actually equivalent to a pointer =new, so it should be stored in the heap, and then it is initialized by default. Will all data stored in the heap be initialized by default?
Variables in a data field in a class in Java can be directly initialized, unlike C + +, and, of course, by constructors. Also, Java uses the method when it is initialized directly. Detailed with white Paper P128 page. It also involves initializing block/static initialization blocks, as well as multi-invocation of constructors .... Here is a mess, really use the time to ponder. The white Paper P131, the content is detailed.
This article is from the "Developing_rookie" blog, make sure to keep this source http://8942041.blog.51cto.com/8932041/1627799