jvm - Java heap/stack有关的问题
巴扎黑
巴扎黑 2017-04-17 12:06:35
0
1
236
public Class A { public static String s = "s"; public static int i = 0; public int j = 1; public static String getStaticString() { int k = 2; return s; } public String getString() { int l = 3; return "something"; } }

根据很多资料,static variables是存在heap中的Perm代,primitive类型存在stack中。
问题:
1、s是存在heap中的Perm代,i,j,k,l存在哪呢?
2、static方法存在哪?非static方法存在哪?也就是说,getStaticString存在哪,getString方法存在哪?

巴扎黑
巴扎黑

reply all (1)
大家讲道理
  1. Perm exists in the static variable itself, such as s, i. The string pointed to by s exists in Perm's constant pool. If s points not to a constant, but to a general object, such as an instance of A, then the instance exists in the heap, and since s will always reference it, it will never be recycled.
    j exists in the instance of A, and the instance of A exists in the heap.
    k and l are primitives created on the stack (in the method body), so there is a stack.
  2. All classes and methods are code data and exist in Perm.
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!