"null"在哪里?在记忆中 [英] Where is "null" in memory

查看:289
本文介绍了"null"在哪里?在记忆中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,您无法在声明中声明数组的大小

In java, you cannot state an array's size in its declaration

int[5] scores;  //bad

我被告知这是因为JVM在初始化对象之前不会在内存中分配空间. 如果您有实例数组变量(自动初始化为默认值null),那么该变量是否指向堆中指示null的位置?

I'm told this is because the JVM does not allocate space in memory until an object is initialized. If you have an instance array variable (auto initialized with a default value of null), does that variable point to a place in the heap indicating null?

推荐答案

否,因为在JVM中不需要这样做. 如果您使用母语(例如C和C ++),则NULL是一个零值的指针,它指向内存基址.显然,这不是一个有效的地址,但是无论如何,您都可以取消引用"它-尤其是在没有受保护的内存的系统中,例如旧的MS-DOS或用于嵌入式处理器的小型内存.并不是说这将是一个有效的地址-通常该位置包含中断向量,因此您不应该触摸它们.当然,在任何会引起保护错误的现代操作系统中.

No, because in the JVM there's no need for that. If you're in a native language (C and C++, for instance), NULL is a pointer with a zero value, and that points to the memory base address. Obviously that's not a valid address, but you "can" dereference it anyway - especially in a system without protected memory, like old MS-DOS or small ones for embedded processors. Not that it would be a valid address - usually that location contains interrupt vectors and you shouldn't touch them. And of course in any modern OS that will raise a protection fault.

但是在JVM中,对对象的引用更像是句柄(即表中的索引),而null是不可能的"值(在表域之外的索引),因此可以不会被取消引用,并且不会在该表中占用空间.

But in the JVM a reference to an object is more like a handle (i.e. an index in a table) and null is an 'impossible' value (an index that is outside the domain of the table), so it can't be dereferenced and doesn't occupy space in such table.

这篇关于"null"在哪里?在记忆中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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