原始和参考变量的内存分配 [英] Memory allocation for primitive and reference variables

查看:69
本文介绍了原始和参考变量的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类型名称;

对于引用类型,这会通知编译器您将使用name来引用类型为type的数据.对于原始变量,此声明还将为该变量保留适当的内存量.

以上是我从oracle docs Java教程中读取的确切内容.

Above are the exact lines i read from oracle docs java tutorials.

我的问题是,什么时候进行内存分配.在编译时还是运行时?原始变量和引用变量都一样吗?

My question is, when does memory allocation take place. During compile time or run-time?? Is that same for both primitive and reference variables?

推荐答案

运行时.

由于 Java 是由 VM 执行的,因此在编译分配内存毫无意义.

As Java is executed by a VM, it does not make sense allocating memory at compile time.

'局部变量'(例如函数参数)或函数中的变量 仅在"分配"上堆栈(原始值或参考).在上始终创建对象.

'Local variables' like function arguments or variables inside a function are only 'allocated' on the stack (primitive value or reference). Objects are always created on the heap.

但是:与内存管理有关的所有事情(分配,释放,垃圾回收)都依赖于 JVM 实现,并且仅发生在 运行时(当然,除了 primitive String常量).

But: everything concerning memory management (allocation, deallocation, garbage-collection) is JVM implementation dependent and happens only at runtime (except for primitive and String constants of course).

这篇关于原始和参考变量的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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