Java declaration object

Source: Internet
Author: User
Java declaration object-general Linux technology-Linux programming and kernel information. The following is a detailed description. As mentioned earlier, when you create a class, you create a new data type. You can use this type to declare objects of this type. However, it takes two steps to obtain a class object. First, you must declare a variable of this type. This variable does not define an object. In fact, it is just a simple variable that can reference objects. Step 2: This statement creates an actual physical copy of an object and assigns a reference to the object to the variable. This is achieved by using the new operator. The new operator dynamically allocates (that is, memory space allocated at runtime) for the object and returns a reference to it. This reference is more or less the memory address allocated to the object by new. The reference is stored in the variable. In this way, all class objects must be dynamically allocated in Java. Let's take a closer look at this process.

In the previous example, the following statement is used to declare a Box-type object:

Box mybox = new Box ();
In this example, the two steps mentioned above are combined. You can rewrite the statement to the following form to make each step clearer:

Box mybox; // declare reference to object
Mybox = new Box (); // allocate a Box object

The first line declares mybox and uses it as a reference to Box objects. After this sentence is executed, the value of mybox is null, indicating that it does not reference the object. Any attempt to reference mybox will cause a compilation error. The second row creates an actual object and assigns its reference to mybox. Now, you can use mybox as the Box object. However, mybox only saves the memory address of the actual Box object. The effect of the two statements is 6-1.

-1 declare a Box object
Note: readers familiar with the C/C ++ language may have noticed that object references look similar to pointers. This suspicion is actually true. An object reference is similar to a memory pointer. The main difference (that is, the key to Java security) is that you cannot operate on it as the actual pointer does. In this way, for object reference, you cannot allocate any memory address as the pointer, or operate on it like an integer.

6.2.1 in-depth study of the new operator

As I have explained just now, the new operator dynamically assigns an address to an object. Its common format is as follows:

Class-var = new classname ();

Among them, class-var is the variable of the created class type. Classname is the name of the instantiated class. The parentheses following the class specify the class constructor. The constructor defines what will happen when a class object is created. Constructor is an important part of all classes and has many important attributes. Most classes explicitly define constructors in their own interior. If a class does not explicitly define its own constructor, Java automatically provides a default constructor. This is the case for defining the class Box. Now we will use the default constructor. Soon you will see how to define your own constructor.

At this time, you may want to know why the new operator is not used for simple variables such as integers or characters. The answer is that Java's simple type is not implemented as an object. For efficiency, they are implemented as "regular" variables. You will see that the object has many features and attributes, so that Java processes the object differently from the simple type. Java can implement simple types more efficiently because of the different overhead of processing objects and processing simple types. Later, you will see that for those cases where the full object type is required, the simple object version is also available.

It is important to understand that the new operator allocates memory for objects during runtime. The advantage of this is that your program can create the memory required by it during running. However, the memory is limited, so new may not be able to allocate memory to an object due to insufficient memory. If this happens, a runtime exception occurs (you will learn how to handle this exception and other exceptions in Chapter 10th ). For example programs in this book, you don't have to worry about insufficient memory, but you must consider this possibility in actual programming.

Let's review the differences between classes and objects. Class to create a new data type that can be used to create objects. That is, the class creates a logical framework that defines the relationship between its members. When you declare a class object, you are creating an instance of this class. Therefore, a class is a logical structure, and the object has physical authenticity (that is, the object occupies memory space ). It is important to clarify the difference.
Related Article

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.