简体   繁体   中英

what is the importance of reference variable type?

Dog dogObj = new Dog();

Yes I know that we have to give the suitable type to the reference variable so it can reserve a place for the object's location address or whatever we put in it.

The reference variable stores a 8 byte address(number) of the real object located in the heap of register. So it's job is just to hold the address of that location. Why does it need a type then? What changes on this 8 byte after giving it a type. It still could point with it 8 byte data to that location even if it has no type.

What is the details behind this?

If you want a variable which can just hold any reference value, that's easy to do:

Object dogObj = new Dog();

However, if you then try to call:

dogObj.bark();

... you shouldn't be surprised that the compiler doesn't know what you're talking about. The compiler uses the declared type of the variable to know how it can be used... it's as simple as that.

Read this: http://www.javaranch.com/campfire/StoryCups.jsp

Then read its follow-up: http://www.javaranch.com/campfire/StoryPassBy.jsp

But basically, you need to give it a type so that the compiler knows that type it is. Note that you could also do this, which is basically what you're talking about:

Object dogObj = new Dog();

But then you could only use the methods defined in Object on that variable, since the compiler has no way of guaranteeing whether some Object is a more specific type or not.

Edit: Let's say you have two classes, Dog and Cat. Dog has a bark() method, and Cat has a meow() method.

You can do this, because as you say, a reference is a reference:

Object dog = new Dog();

But then, you won't be able to do this:

dog.bark();

Even though the Dog class has a bark() method, and the dog variable is an instance of Dog, the compiler doesn't know that . In fact, the compiler can't know what. After all, you could also do this:

Object dog = new Dog();
dog = new Cat();
dog.bark(); //good thing this is a compiler error!

And you might think, why can't the compiler figure it out? The reason for that is code like this can happen:

Object dog = new Dog();
if(Math.random() < .5){
   dog = new Cat();
}
//what type is dog now??

So the compiler can only guarantee that an Object is the type of the variable. That's why you need to give every variable a type.

Note that you can also cast your variable to explicitly tell the compiler what type something is:

Object dog = new Dog();
dog = new Cat();
((Cat)dog).meow(); //this will now work, since the compiler knows dog is a Cat!

But watch out, because if you're wrong, you'll get a runtime error:

Object dog = new Dog();
dog = new Cat();
((Dog)dog).bark(); //this won't work, since dog is actually a Cat!

I highly suggest putting together your own little test program that runs code similar to these examples, that way you can get a better idea of what's going on.

A declaration is not only memory allocation. OOP not using types would not be OOP, you are not manipulating raw bytes but objects with a precise role and a precise logic. That's what classes are for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM