Java内存中究竟是什么null

Java内存中究竟是什么null

本文介绍了Java内存中究竟是什么null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我声明一些字符串变量,如

If I declare some string variable like

String str = null;




  • 是否为变量 str ?

  • 如果已分配,将分配多少字节?

  • 如果没有分配,JVM如何知道有一个名为 str 的变量已被声明?

  • 如果已分配并且内存中有 null 值,那么 null 的确切含义是什么?在二进制?

    • Is there memory allocated for variable str?
    • If allocated, how many bytes will be allocated?
    • If not allocated, how does the JVM know that there is some variable called str been declared?
    • If allocated and if there is null value inside the memory, then what exactly is the representation of null in binary?
    • 推荐答案

      当您执行以下操作时:

      String str = null;
      

      唯一分配的是对字符串的引用(这类似于语言中的字符串指针)其中包含指针类型)。此引用是一个32位或64位变量(取决于您的硬件架构和Java版本),它位于堆栈中(可能取决于您的声明所在的确切上下文)。

      The only thing allocated is a reference to a string (this is analogous to a string-pointer in languages which contain pointer-types). This reference is a 32 or 64 bit variable (depending upon your hardware architecture and Java version) which resides on the stack (probably, depending upon the exact context in which your declaration is placed).

      不应为 null 本身分配内存,因为 null 实际上不是有效的对象实例。它只是一个占位符,表示对象引用当前不是指对象。可能它只是文字值 0 ,这样将对象引用分配给 null 等同于设置指针类型C / C ++中的 NULL 。但最后一点是我的推测。

      No memory should be allocated for the null itself, because null is not actually a valid object instance. It is simply a placeholder that indicates that the object reference is not currently referring to an object. Probably it is simply the literal value 0, such that assigning an object reference to null is equivalent to setting a pointer type to NULL in C/C++. But that last bit is conjecture on my part.

      这篇关于Java内存中究竟是什么null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:02