With the development of the computer revolution, "unsafe" programming has gradually become one of the main reasons for the high cost of programming.
Initialization and cleanup are two issues that involve security.
1. Ensure initialization with constructors
Default constructor (parameterless constructor): constructor that does not accept any arguments
2. Method overload
1. Distinguishing between overloaded methods
Each overloaded method must have a unique list of parameter types.
The difference in the order of parameters is also sufficient to distinguish two methods. However, in general, do not do this because it makes the code difficult to maintain.
2. Overloads involving basic types
The basic type can be automatically elevated from a "smaller" type to a "larger" type, which can cause some confusion once the overload is involved.
If the incoming data type (the actual parameter type) is less than the formal parameter type declared in the method, the actual data is promoted. The char type is slightly different,
If you cannot find a method that accepts exactly the char parameter, the char is raised directly to the int type.
If the actual parameters passed in are large, a narrowing conversion is performed through type conversions. If you do not do this, the compiler will complain.
3. Distinguishing overloaded methods with return values
Sometimes invoking a method ignores its return value, so it is not feasible to distinguish overloaded methods based on the return value. 、
3. Default Builder
If there is no constructor in the class, the compiler automatically creates a default constructor for you. However, if a constructor has been defined (regardless of whether there are parameters), the compiler will not automatically create a default constructor for you.
4.this keywords
The This keyword can only be used within a method, representing a reference to the object that invokes the method.
1. Call constructor in constructor
In the constructor, if you add a parameter list to this, you have a different meaning. This produces an explicit call to a constructor that conforms to this parameter list.
Although you can call a constructor with this, you cannot call two. In addition, the constructor call must be placed at the very beginning, otherwise the compiler will complain.
The meaning of 2.static
The static method is the method without this. You can invoke the static method through the class itself without creating any objects. Has the semantics of global functions.
Non-static methods cannot be invoked inside the static method, but static methods can be invoked in Non-static methods.
5. Cleanup: End processing and garbage collection
Java has a garbage collector responsible for reclaiming memory resources occupied by unwanted objects. But there are special situations: assuming that your object (not using new) obtains a "special" area of memory due to garbage
The collector only knows to release the memory allocated through new, so it does not know how to release this "special" Memory of the object. To cope with this situation, Java allows you to define a method named Finalize () in the class.
It works "assuming" that once the garbage collector is ready to release the storage space occupied by the object, it first calls its finalize () method, and the next garbage collection action occurs
, the memory consumed by the object is actually reclaimed. So if you're going to use Finalize (), you can do some important cleanup work at the time of garbage collection.
Note: Finalize () differs from destructors in C + +: in C + +, objects are bound to be destroyed (if there are no bugs in the program), but the objects in Java are not always garbage collected.
In other words: 1. Objects may not be garbage collected 2. Garbage collection does not equal "destructor"
As long as the program is not near the end of the storage space, the space occupied by the object is always not released. If the program execution ends and the garbage collector has not released any of the
object, the resources are returned to the operating system as the program exits. This strategy is appropriate because garbage collection itself has overhead, and if you don't use it, you don't have to
Pay for this part of the cost.
What is the purpose of 1.finalize ()
Garbage collection is only related to memory. The only reason to use the garbage collector is to recycle memory that the program no longer uses. So for any behavior associated with garbage collection (especially the Finalize () method),
They must also be associated with memory and its recycle.
Finalize () function: Frees the storage space that is allocated to an object in a way that is not created by some form of object.
The reason for Finalize () is that it is possible to allocate memory in a similar way to the C language rather than the usual practice in Java. This occurs mainly when the local method is used
Method is a way to invoke non-Java code in Java. Local methods currently support only C and C + +, but C and C + + can invoke code written in other languages, so you can actually invoke any code.