[Java Learning Basics] Creation and Destruction of Java Objects

     Class instantiation can generate objects, instance methods are object methods, and instance variables are object properties. The life cycle of an object consists of three phases: creation, use and destruction.

create object

Creating an object consists of two steps: declaration and instantiation.

  1. statement

    There is no difference between declaring an object and declaring a normal variable. The syntax is as follows:

    type objectName;

    where type is a reference type, i.e. classes, interfaces and arrays. The sample code is as follows:

    String name;

    This statement declares the string type object name. You can declare not to allocate memory space for the object, but only to allocate a reference.

  2. instantiate

    The instantiation process is divided into two stages: allocate memory space for the object and initialize the object, first use the new operator to allocate memory space for the object, and then call the constructor to initialize the object. The sample code is as follows:

    String name;
    name = new String("Hello World");

    The String("Hello World") expression in the code is to call the String constructor. After the initialization is completed, the following figure is shown:

    

empty object

      A reference variable does not allocate memory space through new, this object is an empty object, Java uses the keyword null to represent an empty object. The sample code is as follows:

String name =null;
name ="Hello World";

The default value of a reference variable is null. When trying to call an instance variable or instance method of a null object, a NullPointerException is thrown, as shown in the following code:

String name =null;//输出null字符串System.out.println(name);//调用length()方法int len = name.length();

But when the code runs to line ①, the system throws an exception. This is because when the length() method is called, name is an empty object. Programmers should avoid calling member variables and methods of empty objects. The code is as follows:

//判断对象是否为nullif(name !=null){int len = name.length();}

Tip There are two possibilities to generate an empty object: the first is that the programmer himself forgot to instantiate it, and the second is that the empty object was passed by someone else. Programmers must prevent the first situation and should carefully examine their code to instantiate and initialize all objects they create. The second case needs to be avoided by judging that the object is not null.

Avoid creating unnecessary objects

(1) Note that String has a constant pool. It is actually stored by private final char[], so it is immutable. It only enters the constant pool when this combination of strings is used for the first time: new String ("abc"); In fact, there are two string objects, "abc" exists at compile time, and it has entered the constant pool;

(2) Consider reuse as much as possible for objects with high instantiation costs such as Calendar;

(3) Special care must be taken when using auto-boxing types to avoid creating a large number of objects due to auto-boxing in the loop. If you can use basic types, do not use boxed types;

(4) The cost of creating and destroying small objects is very small. Therefore, you must consider whether it is worthwhile to use the object pool. Improper management of the object pool may also cause memory leaks.

object destruction

      Objects should be destroyed when they are no longer used. C++ language objects are manually released through the delete statement, Java language objects are collected and released by the garbage collector (Garbage Collection), and the programmer does not need to care about the details of release. Automatic memory management is the development trend of modern computer languages, such as: garbage collection in C#, ARC (automatic memory reference counting management) in Objective-C and Swift.

The working principle of the garbage collector is: when the reference of an object does not exist, it is considered that the object is no longer needed. The garbage collector automatically scans the dynamic memory area of ​​the object, collects and releases the object without reference as garbage. .

 

 

 

 

 

 

 

 

 

Guess you like

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