[Java Learning Basics] Creation and destruction of Java objects

Source: Internet
Author: User
Tags instance method

class instantiation generates an object, an instance method is an object method, and an instance variable is an object property. The life cycle of an object consists of three stages: creation, use, and destruction.

Creating objects

Creating objects consists of two steps: Declaration and instantiation.

    1. Statement

      There is no difference between declaring an object and declaring a normal variable, with the following syntax:

      Type objectName;

      Where type is a reference type, that is, a class, an interface, and an array. The sample code is as follows:

      String name;

      The statement declares a string type object name. You can declare that you do not allocate memory space to an object, but simply assign a reference.

    2. Instantiation of

      The instantiation process is divided into two stages: allocating memory space and initializing objects for an object, first allocating memory space for the object using the new operator, and then invoking the constructor method 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 the constructor that calls the string. After initialization is complete as shown:

    

Empty Object

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

String name =null= "Hello World";

The reference variable default value is null. When attempting to invoke an instance variable or an instance method of an empty object, a null pointer exception NullPointerException is thrown, as shown in the following code:

String name =null;
// output null string System.out.println (name);
// call Length () method int len = Name.length (); ①

However, the system throws an exception when the code runs to the ① row. This is because the name is an empty object when the length () method is called. Programmers should avoid calling the Null object's member variables and methods, the code is as follows:

// determines whether the object is null
if (name!=null) {int len = Name.length ();}

Tips There are two possibilities for generating an empty object: The first is that the programmer forgets the instantiation himself, and the second is that the empty object is passed on by someone else. Programmers must prevent the first case from happening, and should examine their code carefully, instantiating and initializing all the objects that they have created. The second case needs to be avoided by judging the object's non- null .

Avoid creating unnecessary objects

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

(2) for the calendar of such a costly instantiation of the object to consider as far as possible reuse;

(3) Use automatic boxing type must be particularly careful to avoid in the loop because of automatic boxing and create a large number of objects, you can use the basic type do not use boxing type;

(4) The cost of creating and destroying small objects is very small, so when using object pooling it is important to consider whether it is worthwhile to use the object pool to manage improperly and may cause memory leaks.

Object Destruction   

Objects should be destroyed when they are no longer in use. The C + + language object is manually released through the DELETE statement, and the Java language object is collected and then freed by the garbage collector (garbage Collection), and the programmer does not care about the details of the release. Automatic memory management is the trend of modern computer language, such as: C # language garbage collection, objective-c and Swift language arc (memory automatic reference count management).

The garbage collector (garbage Collection) works by saying that when a reference to an object does not exist and that the object is no longer needed, the garbage collector automatically scans the object's dynamic memory area, collecting the unreferenced objects as garbage and releasing them.

[Java Learning Basics] Creation and destruction of Java objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.