Java variables are stored in memory

Java variables are stored in memory

Explore the following member variables and local variables stored in the memory of the case.

package com.my.pac04;

/**
 * @author Summerday
 * @date 2019/12/1 13:05
 */
public class ValStoreTest {
    public static void main(String[] args) {
        //分别创建两个Value类的实例,val0和val1
        Value val0 = new Value();
        Value val1 = new Value();
        //修改val0的num值
        val0.num = 5;
        System.out.println("val0's num = " + val0.num);//val0'num =5
        System.out.println("val1's num = " + val1.num);//val0'num =0
        
        //直接使用类调用类变量
        Value.staticNum = 10;
        //使用实例对象调用类成员变量,不建议使用
        //val0.staticNum = 10;
        System.out.println("val0's staticNum =" + val0.staticNum);//10
        System.out.println("val1's staticNum =" + val1.staticNum);//10
    }
}
class Value {
    public int num;//实例变量
    public static int staticNum;//类变量
}

Member variables

Members of the above-mentioned variables into instance variables and class variables. Both forms of storage in memory, how is it?

class Value {
public int num;//实例变量
public static int staticNum;//类变量
}

Instance variables

Value val0 = new Value();
Value val1 = new Value();
//修改val0的num值
val0.num = 5;
System.out.println("val0's num = " + val0.num);//val0'num =5
System.out.println("val1's num = " + val1.num);//val0'num =0
  • In the heap opened up memory, storage instance variables, and the default initialization.
  • In the stack area to declare a reference variable val0 and val1, so that they are stored two heap address of an object, which means make reference variable points to the actual object.
  • Two objects are independent of each other.

Class variables

//直接使用类调用类变量
Value.staticNum = 10;
//使用实例对象调用类成员变量,不建议使用
//val0.staticNum = 10;
System.out.println("val0's staticNum =" + val0.staticNum);//10
System.out.println("val1's staticNum =" + val1.staticNum);//10
  • In the method area (not the stack area) storing modified static variables, i.e. staticNum.
  • Val0 and two reference variables val1 is stored in the stack area, and they point to the same memory area of a regional approach.
  • In the same class, an object instance of class shared variables. I mean, if a Java program to run in a different JVM process, it will not share data. as follows:
package com.my.pac04;
/* ClassVal.java */
public class ClassVal {
    public static int value = 6;
}
package com.my.pac04;
/* ClassValTest01.java */
public class ClassValTest01 {
    public static void main(String[] args) {
        ClassVal val01 = new ClassVal();
        val01.value = 10;//修改static修饰的值为10
        System.out.println(val01.value);
    }
}
package com.my.pac04;
 /* ClassValTest02.java*/
public class ClassValTest02 {
    public static void main(String[] args) {
        ClassVal val02 = new ClassVal();
        System.out.println(val02.value);//不是10,而是6
    }
}

About JVM and class loading mechanism, will continue in-depth after learning here is not to express their superficiality of talk, after which will be supplemented ha.


Local variables

  • When you define local variables are initialized to him only after the assignment, the system will get given memory.
  • Local variables do not belong to the class or instance, the stack is always kept in memory, so the method ends with the end code block, LIFO.
  • Storing basic data types specific value, refer to the actual type of the referenced object storage address value .

Note : Although most of the time, you can use between member variables instead of local variables to solve the problem, but this approach has several drawbacks:

  • Increase the survival time variable, increase the memory overhead.
  • Expand the scope of variables, not conducive to improving the cohesion of the program.

The smaller the scope of local variables, its shorter residence time in memory, the better the performance of the program.

to sum up

  • If the variables defined for the specific information describes a class or object, with the member variable.
  • If the variables defined for state information storage class or instance running, with the member variable.
  • If the defined variable for storing a plurality of messages between the shared methods, with the member variable.

Guess you like

Origin www.cnblogs.com/summerday152/p/11966036.html