Giter Site home page Giter Site logo

Comments (4)

stackvoid avatar stackvoid commented on August 28, 2024

严谨一点应该这么问:

Java 程序在 JVM 中运行时,全局变量和局部变量在虚拟机的什么位置?

粗略回答:一个是堆一个是栈。

from android-discuss.

zimoguo avatar zimoguo commented on August 28, 2024

java中没有全局变量这一说吧?

from android-discuss.

stackvoid avatar stackvoid commented on August 28, 2024

@zimoguo 其实就是静态变量,Java 程序一旦加载完毕就能获取到的变量。

from android-discuss.

dkmeteor avatar dkmeteor commented on August 28, 2024

@stackvoid

这个问题太难回答了...
静态变量的话

粗暴一点你可以说是 堆
但是你也可以说在 Permanent Generation 或者 Method Area(方法区)
糟糕的是,对于不同虚拟机实现,Permanent Generation实现方式可能不同,或者根本不存在
Method Area也是个很暧昧的东西。

局部变量
我觉得不是很对...

稍微详细一点讲。。

在方法调用时,会首先载入 方法的帧栈,局部变量在方法帧栈的局部变量区中。
如果局部变量是个基本数据类型,那么直接在帧栈中。
如果是非基本类型,那么会在堆中创建对象,帧栈中持有该对象的引用。

糟糕的是,引入JIT以后,编译器可能会对这部分代码进行优化,引入一个叫 栈上分配 的优化策略。
在该策略下,本来应该在堆中创建的对象可能会被拆解成基本类型并直接在帧栈上分配。

from android-discuss.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.