`

why null is allocated on java stack

 
阅读更多

提问:

恩。。。俺的意思是,
String str = null;
str = "String";
最后的"String"对象是建立在栈里的,而不是堆里的,这样一来,第一句话,仍然还是在堆中开辟空间么?因为最后的对象其实是在栈中,如果一开始声明null的时候,在堆中开辟空间,岂不是有点奇怪?

 

回答:

1. null不是一个对象,JLS 3.10.7 中提及的是The Null Literal,况且它是ASCII中值为0的一个字符
2. null instanceof Object 是false 也证明了它不是Object
3. null在JVM全局内存中只存在一个,因为它无需存在多个,造成不必要浪费,因此挺符合String interned的初衷。All literal strings and string-valued constant expressions are interned. 我觉得null勉强能算是literal string,因为它是基于字符的。

综上,既不是Object又够格interned,所以认为null是在栈中开辟。

 

http://norvig.com/java-iaq.html#null

http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf

http://topic.csdn.net/u/20111204/23/816f6ab6-e99c-48ad-84a0-03de4fd8aa4c.html?seed=797611320&r=78770770#r_78770770

分享到:
评论

相关推荐

    Stack Requirements.pdf

    The stack for the osKernelStart function is referred as "Startup" and RTX v5 uses 32 bytes (with Arm Compiler). However the user should also consider additional stack that might be allocated by the '...

    修改DVMT Pre-Allocated数值方法1

    本文主要介绍了如何修改小新 Pro13 笔记本电脑BIOS中的DVMT(Dynamic Video Memory Technology,动态显存技术)Pre-Allocated数值,以满足macOS系统对至少64M DVMT值的需求。DVMT Pre-Allocated数值决定了分配给集成...

    javacv-platform-1.3.3-src

    JavaCV also comes with helper classes and methods on top of OpenCV and FFmpeg to facilitate their integration to the Java platform. Here is a small demo program demonstrating the most frequently ...

    RR.rar_Time sharing_quantum algorithm_round robin_rr ready queue

    If the remaining request is less than a time slice, only the remaining request time is allocated. Round-robin scheduling is both simple and easy to implement. It is also starvation-free.

    SSD7 选择题。Multiple-Choice

    Your score on this question is: 10.00 Feedback: (b) -------------------------------------------------------------------------------- 2. The arity of a table is the number of _____ in the...

    Objective C Memory Management Essentials(PACKT,2015)

    and why memory leaks occur in an application, moving on to autorelease pools and object creation/storage to get an idea of how memory is allocated. You will also see what ARC (Automatic Reference ...

    ssd3选择题答案大全

    - (d) a list of variables allocated on a program's stack - **正确答案**:(a) a sequence of method calls - **解析**:堆栈跟踪是一系列方法调用的记录,它通常在发生异常时显示出来,帮助开发者追踪问题所在...

    Turbo C++ 3.0[DISK]

    that has the install program on it and type INSTALL. You will be given instructions in a box at the bottom of the screen for each prompt. For example, if you will be installing from drive A:, type...

    Turbo C++ 3.00[DISK]

    that has the install program on it and type INSTALL. You will be given instructions in a box at the bottom of the screen for each prompt. For example, if you will be installing from drive A:, type...

    Introduction to Machine Learning with Applications in Information Security-2018

    At least one week is allocated to student presentations of their project results. A suggested syllabus is given in Table 1. This syllabus should leave time for tests, project presentations, and ...

    java实现银行家算法

    int allocated[]; // 当前分配 int need[]; // 剩余需求 } // Banker类实现银行家算法 class Banker { Process processes[]; // 进程数组 int available[]; // 可用资源 int max[][], alloc[][]; // 所有进程...

    Engine_controller_Software.rar_ecu_传感器 标定_动力系统_发动机_电喷

    小型发动机电子燃油喷射系统控制程序,根据发动机的不同工况,电喷系统控制单元(ECU)将电喷系统各执行器及传感器实时数据采集并上传显示到标定软件界面,通过软件在线标定(即喷油控制及点火控制),从而实现...

    中关村在线

    ALLOCATION_RATE_COUNTER_009_HELP=This counter displays the rate of bytes per second allocated on the GC Heap. This counter is updated at the end of every GC; not at each allocation. This counter is ...

    银行家避免死锁算法模拟实现Java版

    《银行家算法避免死锁:Java模拟实现》 在多线程编程中,死锁是一个常见且严重的问题,它发生在两个或多个并发进程之间,每个进程都等待其他进程释放资源,从而导致所有进程都无法继续执行。银行家算法是解决死锁...

    微软内部资料-SQL性能优化5

    For example, if the clustered index is on (lastname, firstname) and a nonclustered index is on firstname, the firstname value will not be duplicated in the nonclustered index leaf rows. Note The ...

    java 银行家算法

    private int[] allocated; // 已分配资源 // 其他属性和方法... } public class Resource { private int total; private int available; // 其他属性和方法... } ``` ### 6. 性能与优化 虽然银行家算法可以...

    银行家算法Java

    《银行家算法在Java中的实现》 银行家算法是一种经典的资源分配和死锁预防策略,由艾兹格·迪杰斯特拉在1965年提出。它主要用于确保系统的安全性,即系统不会陷入无法继续执行的状态。在这个Java实现中,我们将深入...

    vld-2.5.1-setup.exe

    The leak report includes the full call stack showing how any leaked memory blocks were allocated. Double-click on a line in the call stack to jump to that file and line in the editor window. It's a ...

    JAVA编程规范大全

    根据提供的文件信息,我们可以归纳出以下关于JAVA编程规范的关键知识点: ### 1. 文件与类组织结构 #### 文件头部版权信息 在JAVA编程规范中强调了文件头部应该包含版权信息,例如: ```java /** * Copyright ?...

    running rac on oracle vm server for sparc 2.0

    - **Sharing Processor Resources**: Careful planning is required to ensure that the virtual processors are allocated efficiently among the domains to prevent contention and maximize performance. ...

Global site tag (gtag.js) - Google Analytics