Java中向上传输的内存分配 [英] memory allocation for upcasting in java

查看:92
本文介绍了Java中向上传输的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这些类别:

public class Animal{

}

public class Dog extends Animal{

}

public AnimalTest(){
    public static void main(String[] args){
       Dog d = new Dog();
       Animal a = d;
   }
}

我的问题是,因为我在Animal a = d;上执行了上传,它是否消耗了计算机上的新内存分配,还是使用了分配给Dog d = new Dog();

my question is since I performed an upcasting on Animal a = d;does it consume a new memory allocation on the machine or does it use the memory allocated to the Dog d = new Dog();

推荐答案

动物a = d;

Animal a = d;

a只是一个引用,并且该引用的内存是在方法堆栈(或jvm堆栈,而不是堆)中分配的.

a is just a reference and the reference's memory is allocated in method stack(or jvm stack, not heap).

也就是说,当调用方法main时,JVM将分配一个包含引用空间的堆栈.

That is when invoke the method main, JVM will allocate a stack which contains the reference's space.

这篇关于Java中向上传输的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆