Java Study Notes (Day 3)

The two most critical words in object-oriented are class and object. In essence, a class can be regarded as an abstraction of an object, which defines the properties and methods of an object.

A class is a template used to define the state and behavior that a group of objects have in common, while an object is an abstract representation of an individual or thing in the real world and encapsulates their properties and behavior. A class declares these common characteristics. Before an object (that is, an instance of a class) can be used, the class of the object must be defined, and its properties and behaviors can be accessed only after knowing the type of the object.

In the Java language, the behavior of an object is defined as a method of a class, and the properties of an object are defined as class member variables. So, a class includes the properties and behavior of objects. Declared by the class keyword. Multiple member variables and member methods can be defined in the class body.

Java provides a mechanism for managing class files, namely packages.

Each interface or class in Java comes from a different package. Whether it is a class and interface in JavaAPI or a custom class and interface, it needs to belong to a certain package. This package is equivalent to a folder, which contains some classes and interfaces. document.

When specifying a package name in a class, the package statement must be placed on the first line of the program. It must be the first line of non-commented code in the file. When the package keyword is used to assign a package name to a class, the package name will become the class name In this case, the full name of the class is the package name plus the class name.

Java's naming conventions require packages to use all lowercase letters.

Use java's import statement to import single or all classes of the specified class package. The package name can use the wildcard "*" to represent all classes.

When import imports all the classes in a package, the classes in the corresponding subpackage will not be imported. If you want to use the subclasses in this package, you need to guide the subpackages separately again.

In addition to importing packages, the import keyword can also import static members: import static static members; import static static import statements can only import static members of a class.

Member variables are variables defined in the class body, i.e. global variables, which are used to define the state of the object.

Member methods are descriptions of object behavior. Each method is represented by a compound statement describing the corresponding behavior.

Member variables and member methods are collectively called class members.

The parameter of a member method can be an object or a variable of the basic data type, and the member method has a return value or no return value.

The return value type must be specified when declaring a method. If a method has no return value, the void keyword must be used as the return value type. If the declared method specifies the return value type of the method, you must use the return statement in the method body to return the value of the corresponding type. After using this keyword, the execution of the method will be terminated. In a method whose return value type is declared void, you can also use a return statement without a return value, which ends the execution of the method.

To create an instance object of a class, you need to use the new statement: class name object name = new class constructor();

After the object is created, you can access the properties of the object, that is, member variables: object name.property

The behavior of the object is also the member method of the class, that is, it is usually said to call or execute a method of the object. ObjectName.MemberMethodName();

When calling a member method, if the method defines parameters, you must provide parameter values ​​of the same type within parentheses, in the same order as the method parameters were defined.

The java language provides a garbage collection mechanism. Objects that are no longer used will be automatically destroyed. You can also explicitly assign a null value to an object in the program, so that the object is no longer used.

A class variable is also called a static member variable, and its syntax adds the static keyword to the member variable, making the member variable static. A static member variable is not assigned to each instance variable, but a variable belonging to a class. It is unique in memory and can be accessed directly using the format of "class name.member variable". Its location in memory is fixed and is a storage unit shared by all instance objects of the class. And only takes up one memory space.

Variables declared as static must be member variables, not local variables. Variables declared in the body of a method or in any code block cannot use the static keyword.

Member variables and member methods must be created and accessed through the object. A class method is defined by the static keyword, which is also a static member method.

Static members are members of a class and should be accessed using the class name as a prefix instead of the object name. Static member variables and static member methods cannot be accessed by non-static methods, whereas non-static methods can access static members.

Method overloading is to define multiple methods in the same class with the same name, but with different parameter types, number and order.

If a method contains a local variable with the same name as a member variable, the access to the variable in the method is accessed as a local variable, that is, the local variable in the method body overrides the member variable. If you want to access member variables in this scope, you must use the this keyword. The this keyword cannot be used in static contexts and static code blocks.

The Java language provides constructors and static code blocks for initializing objects, which complete the initialization work at the same time as the object is created.

The constructor is a method with the same name as the class. The creation of the object is done through the constructor. Its function is mainly to complete the initialization of the object. Whenever the class instantiates an object, the class will automatically call the constructor. Declare a constructor with no return value type. The name of the constructor should be the same as the name of this class.

Ordinary methods with no return value are defined in the form of public void methodEx(), but constructors do not need to be modified with the void keyword. If no constructor is explicitly defined in the class, the compiler automatically creates a default constructor that takes no parameters.

In the construction method, initialization work such as variable assignment and connection to the database can be completed.

Constructors can also be overloaded, and multiple different constructors that define parameter lists can create and initialize different objects.

If none of the constructors defined in the class are no-argument constructors, the compiler does not set a default no-argument constructor for the class.

The this keyword can also be used for calls between constructors.

A class can create a static code block that is not contained in any method body, the code block is a compound statement defined using the static keyword. static{code block}; The program code block is executed and executed only once when the class is loaded, and is mostly used for the initialization of class attributes.

When defining a method, you can specify a parameter list for the method, and the method interacts with the caller of the method according to the parameter list. The parameter type of the parameter list can be basic data types such as int, short, double, etc., or it can be an object type, but the value transmission methods of these two parameter types are different. If the passed parameter is a basic data type, the method will directly obtain the value of the specified type; if the passed parameter is an object type, then the method only obtains a reference to an object address, through which you can access and operate object properties and behaviors , but the entity of the object is not passed to the method, it is still in a certain area of ​​memory, in this case, when the method changes the properties of the object, it will directly change the data of the object entity, and the data of the basic type is passed by value Yes, changing it will not affect the variable passed by the method caller.

In the Java language, literals of type string must be enclosed in a pair of " " (double quotes). Strings are made up of many characters concatenated. A string is any textual information that the system can display, even a single character. In Java, all strings enclosed by the "" sign cannot be used as other data types. A declared string variable must be initialized before it can be used. Objects that are not initialized cannot be used.

The concatenation of strings, or the concatenation of strings with other primitive types, can use the "+" operator, which concatenates multiple expressions and produces a String object.

In Java, a concatenated string cannot be separated into two lines, but "+" can be used to solve the problem.

As long as one operand of the "+" operator is a string, the compiler converts the other operands to string form.

The comparison operator "==" compares whether the addresses of two strings are the same. To compare whether the contents of two strings are the same, you can use the equals() method and the equalsIgnoreCase() method. If the two strings have the same characters and length, and the same case, use the equals() method to compare and return true, and use the equals() method to compare the strings with case sensitivity. The equalsIgnoreCase() method compares whether two strings are the same regardless of case, and the returned structure is still of boolean type.

The compareTo() method of the String class compares two strings lexicographically, based on the Unicode-encoded values ​​of the individual characters in the strings. Returns a negative integer, positive integer or 0. The compareTo() method returns 0 only if the equals(Object) method returns true.

Use the length() method of the String class to get the length of a string object.

The String class provides two methods for finding strings, namely the indexOf() and lastIndexOf() methods. Both of these methods allow searching within a string for a character or string of specified criteria. The indexOf() method returns the position of the first occurrence of the searched character or string, and the lastIndexOf() method returns the position of the last occurrence of the searched character or string. Returns -1 if no string is retrieved.

String subscripts are from 0 to length()-1. If the parameter in the lastIndexOf() method is the empty string "", the result returned is the same as that returned by calling the length() method of that string.

Use the charAt() method to get the character at the specified index position in the string. The trim() method returns a copy of the string, that is, a new string. This new string removes leading and trailing blanks from the original string, where blanks include spaces and tabs.

The substring() method of the String class can intercept a substring of a specified length from the current string. Use the subscript of the string to intercept, and the subscript of the string starts from 0. A space occupies an index position in the string.

Use the split() method to split the string according to the specified method; or split the content by substring, and store the split result in the string array. There is no unified symbol for dividing characters. If you want to define multiple delimiters, you can use the symbol "|", for example, ", |=" indicates that the delimiters are "," and "=" respectively.

The replace() method can be used to replace the specified character or string with a new character or string. The result returned by the replace() method is a new string. If the string oldChar does not appear in the string sequence in the object expression, the original string is returned. The method is to replace all.

The toLowerCase() method of the String class changes all characters in the string from uppercase to lowercase. The toUpperCase() method changes the lowercase letters in the string to uppercase letters. The string length is the same as the original string length. Numbers or non-characters will not be affected.

The startWith() method and the endsWith() method are used to determine whether the string starts or ends with the specified content, respectively. The return values ​​are all boolean types.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325770169&siteId=291194637