我在接受采访时被问到这个问题并且无法回答.希望您能够帮助我.
问题是:为什么Java人员不添加对结构的支持?为什么不可能将堆栈上分配的值类型添加到Java?
我想以前的java版本应该存在一些向后兼容性问题?但是,我无法提出任何问题.
虽然Java不允许堆栈分配,但它确实使用了一些名为Escape Analysis的东西.
找到了一个触及这个的链接:http://www.ibm.com/developerworks/java/library/j-jtp09275/index.html
The Java language does not offer any way to explicitly allocate an object on the stack,but this fact doesn’t prevent JVMs from still using stack allocation where appropriate. JVMs can use a technique called escape analysis,by which they can tell that certain objects remain confined to a single thread for their entire lifetime,and that lifetime is bounded by the lifetime of a given stack frame. Such objects can be safely allocated on the stack instead of the heap. Even better,for small objects,the JVM can optimize away the allocation entirely and simply hoist the object’s fields into registers.