`

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 '...

    FAT16文件系统格式-英文版.pdf

    The 16-bit part describes the way units are allocated on the drive. The FAT16 file system uses a 16-bit number to identify each allocation unit (called cluster), and this gives it a total of 65.536 ...

    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.

    修改DVMT Pre-Allocated数值方法1

    命令行方法以管理员身份运行CMD或PowerShell,进入InsydeH2OUVE所在目录,运行WDFInst.exe安装工具驱动提取命名为SaSetup的变

    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 ...

    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 ...

    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 ...

    微软内部资料-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 ...

    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 ...

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

    A stack is an area of memory in which program procedure or function call addresses and parameters are temporarily stored. In Process To run in the same address space. In-process servers are loaded ...

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    From there, you’ll create a custom LLDB command which gives you the stack trace of when an object was allocated or deallocated in memory — even after the stack trace is long gone from the debugger....

    wireshark-DHCP-DNS-抓包分析全英文版

    wireshark抓包及分析,...laptop is allocated to a public IP address and in dorm, where it is wired connection, the laptop is allocated to a private IP address, while the router’s IP is 192.168.1.1.

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

    If no rows satisfy the WHERE condition the first time the range is scanned, no rows should be returned on any subsequent scans. Key range locks are similar to row locks on index keys (whether ...

    Multi-Core Cache Hierarchies

    A key determinant of overall system performance and power dissipation is the cache hierarchy since access to off-chip memory consumes many more cycles and energy than on-chip accesses. In addition, ...

    c++ granularity.pdf

    This article is the first of several that will describe principles that govern the macro structure of large object-oriented applications. I emphasize the word large, because these principles aremost ...

Global site tag (gtag.js) - Google Analytics