Stored value and reference

Declare a variable

We use var to declare a variable,Congguizili like a tube placed in the experimental stage;

We give a specified variable type,Just like in the test tube labeled the;

Then put reagents must be marked on the label - otherwise could cause an explosion or poisoning。

Same,If we try to give a reservoir into a wrong data type,Then the compiler will complain - yes there is always a way to be able to fool the compiler - anyway, I will not teach you this method,That would cause it to crash。

So,The tube has a capacity of,Type declaration we also have size restrictions,Will overrun error - general enough。

That is why there is a term called "overflow”。

 

If you are not interested in chemistry

Well,We change a chestnut:

We all know that there are some animals eat the grass,And there are some animals eat these animals graze?

Well I've got a wolf and a sheep,Wolf to eat the sheep,And the sheep to graze。

You give the wolf to sheep graze or eat wolf is wrong ..................

All right,We still right next topic。

 

The value of the object is not

Now,We know what is the reservoir,Let us talk about "open" to the reservoir where another thing - Object。

After contact with the object-oriented programming,We must face a new concept - what is the object,of course,This is something we have discussed in the previous lesson,This lesson we focus on how to put an elephant into a refrigerator problem。

When we declare:

Is not the instances into a memory, too? - Not really。

In fact really can not store an object storage,As we say,Nor can an elephant into household refrigerators。

Quietly tell you,In fact, the object on the "stack" on the right ......,It is garbage。

All right,Here we have to call out another special value except nil value of this special:

Quote

In fact, that is a reference pointer。It is just a pointer to packaging - specific package became what we know - probably only Apple's siege lion can tell you。In short,We call this value is called reference。

Unlike C language pointer,Although reference is a pointer to an object side of the pile,But it indeed can not be directly edited。

TV and remote control

I talked about two things you may also feel that it is precisely because the old ancient ......,So I think probably everyone should be contacted。

When you generate an object,Then we say you bought a television set;Then the corresponding put your hand,Nature is the remote control it。So we say,Regardless of your generated object instance how complex,How large,Its remote control is always like that - at most, a few more buttons (method) only,The real example is actually placed on the heap。

Speaking heap

Let's talk about the correspondence between the reservoir in a reference to the object instance on the heap。

We declare a general store,And then assign it to an object,Then the store would keep a reference to,And then instantiate an object was thrown in a heap。Then,This time our remote control and TV is one to one correspondence of。

like this,We generated from two instances of the same class,Two examples was placed pile,And our hands are saved variables refer to them - like a house number。

What if I want to quote a copy to someone else?

Yes! Another variable that will point to the same object on the heap!

In fact, now objA and objC is a reference to the same object of it!

Now I do:

what! They have become the same object! ……and many more,That objB in the original object where to go? This not a way to never have access to it yet? ?

Yes,This time the original objB that object referenced by it alone exists in the heap - it does not matter,It has long been GC (garbage collector) gave away (do not ask me to where)。

Be chestnuts now!

In fact,Is the question on the lesson after-school friends ~

Operating results is 24 instead of 10.

 

After-school title

We declare a class named HeapQuiz,This class contains only one attribute is the ID,We use it to distinguish between instances,Now we generated five instances,Then they upset references,Now,Can you name the five reference ID corresponding instance of it?

 

Original article written by Gerber drop-off:R0uter's Blog » Stored value and reference

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1109.html

About the Author

R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *