Object
Objec is the parent of all classes
1. The == operator, which can be used in primitive and reference data type variables.
If you are comparing primitive data type variables, compare whether the two variables hold the same data. (Not necessarily the same type)
If you are comparing reference datatype variables, compare whether the address value of two objects is the same and whether two references refer to an object entity.
When “==” is used for comparison, the data types on both sides of the symbol must be compatible (except for basic data types that can be converted automatically), otherwise the compilation will fail
2. Equals () method
Methods, not operators, apply only to reference data types
Definition of equals () in the Object class
public boolean equals(Object obj){
return (this == obj);
}
Copy the code
It is common for custom classes to override equals ()
3. The difference between == and equals
①== can compare both base and reference types. == == == == == == == == == == == == == == == == == == == We can see that the equals method of the String class has been overwritten, and the String class is used more frequently in daily development, resulting in the mistaken view that equals is a comparison value. ③ Check whether the equals method of the custom class overrides Object. (4) In general, overriding equals compares the corresponding attributes of a class to whether they are all equal.
4. The toString() method is defined in the Object class. Its return value is String, and it returns the class name and its reference address.
The toString() method can be overridden in user-defined types as needed
Packaging Class (Wraper)
static
The static keyword is used
1. The static: static
2. Static can be used to modify properties, methods, code blocks, inner classes
3. Use static to modify properties: Static variables (or class variables)
3.1 Attributes, depending on whether static modification is used, can be divided into static attributes vs non-static attributes (instance variables)
Instance variables: We create multiple objects of the class, each of which has an independent set of non-static properties of the class. Changing a non-static property in one object does not result in changing the value of the same property in other objects.
Static variables: We create multiple objects of the class that share the same static variable. When a static variable is modified through one object, it causes other objects to call the static variable, which is modified.
3.2 Static modifiers:
① Static variables are loaded as the class is loaded. It can be called as class.static variable
② Static variables are loaded before objects are created.
(3) Since the class is only loaded once, there is only one copy of the static variable in memory: the static field of the method area.
Class variable instance variable
Class yes no
Object yes yes
3.3 Example for Static Properties: System.out; Math.PI;
4. Use static modifiers: static methods
① As the class is loaded, can be through “class. Static method”
② Static method non-static method
Class yes no
Object yes yes
③ In static methods, only static methods or properties can be called
In non-static methods, you can call either static or non-static methods or propertiesCopy the code
- Static note:
5.1 In static methods, the this keyword and super keyword cannot be used
5.2 The use of static properties and methods is understood from a lifecycle perspective.
- During development, how do I determine if a property should be declared static?
Properties can be shared by multiple objects and do not vary from object to object.
Constants in classes are also often declared static
During development, how do YOU determine if a method should be declared static?
Methods for manipulating static properties, usually set to static
Methods in utility classes that are traditionally declared static. Examples include Math, Arrays, and Collections