Java object, reference, instance

Source: Internet
Author: User

When it comes to references, as the name implies, We are referencing a Dongdong. This Dongdong is the memory address. It is complicated to pull the memory address. Or help you analyze it.
Java code
Class Picture {
Int width;
Int length;
......
}
Picture (int x, int y)
{
Width = x;
Lenth = y;
}
Picture pic = new Picture (3, 4 );
1: Why use a class?
Here we have created a Picture class, which is a model and a model for creating Picture images. Why is there a class in java? If there is no class, when we represent an image, we need to declare a wide width1, long length1, and so on. We need to indicate the time of the second image and a wide wid22, I declare N images. I don't know how many width and length I declare. It's so tired.
With the class, it is convenient, Picture pic1 = new Picture (); Picture pic2 .. picture pic3 ....; it is inconvenient to call attributes such as pic1.width and pic2.width. here is why the class is used.
2: Let's talk about the instance, object, and object references;
When you have defined a Picture class, the long and wide attributes are not allocated a grid to store these two attributes. The defined Picture class is just a model, not a real image, java uses new to create real images. new Picture (3, 4) creates an image with a width of 3 and a length of 4. Therefore, we can use new Picture (3, 4) to call the attributes of this image, new Picture (3, 4 ). width, new Picture (3, 4 ). length.
You may have doubts. Why is new Picture (3, 4) followed by Picture (3, 4 ?, In fact, it is the attribute of initializing the width and length of Picture. If you want to define a real image, you must design the width and length. So new follows Picture (3, 4), which is the constructor. Some textbooks are not clear about examples. In fact, we can see from the above that new Picture (3, 4) is an example. It is a real Picture!
For further analysis, every time I call a property, new Picture (3, 4). length is always required. Is it too long?
Find something to replace new Picture (3, 4). Let's use pic1 to replace new Picture (3, 4). Then we only need pic1.length to call attributes. How can we declare the variable pic1? Picture pic1, so declare it! So we set their equal signs Picture pic1 = new Picture (3, 4); then we can say that pic1 is next to the new Picture (3, 4) object that is equal to the following. Why is it next to it? There can be many photos of the same size, so we can use a new Picture (3, 4) NEW image, which is also 3 in width and 4 in length, the new image is not equal to the previous pic1. In Picture pic1 = new Picture (3, 4);, pic1 is an object or an instance. The next new Picture (3, 4) is also an object or an instance, the two of them are equivalent.
So what is the object reference? As mentioned above, we use pic1 to replace new Picture (3, 4), so we can say that pic references the instance image or object next to the equal sign, so pic1 is also called reference.
Summary: pic1 is an object, an instance, or a reference to this object. new Picture (3, 4) is a real image. It can only be an object or an instance, not a reference!
-Pic1 actually stores a byte memory address, which is the first address of the new Picture (3, 4) heap memory address, that is, the first address-the term, pic1 points to the first address of the new Picture (3, 4) heap memory address.

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.