jvm运行时堆栈分配问题- 创意小软件开发要塞- JavaEye技术网站

Object object = new Object();

?

单这样一句,能说出个所以然来吗?有的说,object是引用,在栈上分配,new Object()是对象,在堆中分配,是这样吗?

?

All objects in Java are located on the heap. I'm not quite sure what you mean by "access objects from the heap". The only things stored on the stack are the list of functions which called into the current context and their local variables and parameters. All local variables and parameters are either primitive types or references.

If you allocate an object using new (which is the only way to allocate non-primitive types; yes this includes array types), then the object is allocated on the heap, and a reference to that object is stored on either the stack or the heap, depending on if the reference is stored in a local variable/parameter or as a member of another object.

When passed as parameters to functions, all objects are passed by reference - if the function modifies the parameter, the original object is also modified. Identically, one could also say that the object references are passed by value - if you change a parameter to refer to a new object, it will continue to refer to that object for the duration of the function, but the original object which was passed in will still refer to whatever it referred to before. Primitive types are also passed by value.

{zx1}评论

  • 谢谢你的回复,在你启发下我去debug了下程序,的确只有在read()方法的时候当 ...
    -- by

  • 回楼下huayurei:??? 原来的认识是有问题的, 看BufferedRead ...
    -- by

  • wentao365 写道 return obj; 是不是写错了?这里retur ...
    -- by

  • 这个解释是有可能会引起异常,偏偏我怎么设置都不产生异常,搞了我半天都没明白这个ma ...
    -- by

  • return obj; 是不是写错了?
    -- by
郑重声明:资讯 【jvm运行时堆栈分配问题- 创意小软件开发要塞- JavaEye技术网站】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——