Second, Java object-oriented (6) _ in-depth variables

2018-04-30

 

deep variable

 

First, the classification and initial value of variables

  1. Classification of variables

    Variables are divided into two categories according to where they are defined

      1) Member variables/fields: variables defined directly in the class {} (outside the method)

        • Class member variables (fields decorated with static)
        • Instance member variables (fields not modified with static)

     2) Local (environment) variables: variables other than member variables, variables defined in methods

        • variables inside methods
        • method parameters
        • A variable in a code block, a pair of {}              

  Note: variable access is the principle of proximity

 

  2. Initialization of variables

    • Member variables: the default is to have an initial value (see figure)

        

 

    • Local variables: There is no initial value by default, so it must be initialized before it can be used

  Note: Variables must be initialized (assigned) before they can be used. Initialization is the real memory allocation.

 

Second, the scope and life cycle of variables

  1. variable scope   

    Variables have different scopes depending on where they are defined. Look at the pair of curly braces {} where the variable is located

      Member variables/fields: function in the defined class

      Environment variables: from the beginning of the definition to the } immediately following the end

 

  2. The life cycle of variables

    The scope of the variable determines the lifetime of the variable

    The life cycle of a variable refers to the process from when a variable is created and allocated memory space to when the variable is destroyed and its memory space is cleared.

    -------------------------------------------------------------------------------------------------------------------------   

    Member variables: When the system loads a class or creates an instance of a class, the system automatically allocates memory space for member variables, and automatically assigns initial values ​​to member variables after the memory space is allocated.

    -------------------------------------------------------------------------------------------------------------------------

 

    

 

Third, the initialization of local variables and the running mechanism in the JVM

  After a local variable is defined, it must be explicitly initialized before it can be used. This means that after a local variable is defined, the system does not allocate memory space for the variable. Until the program assigns an initial value to the variable, the system will allocate memory to the local variable and save the initial value to this memory.

 

    与成员变量不同,局部变量不属于任何类或实例,因此它总是保存在其所在方法的栈内存中。如果局部变量是基本类型变量,则直接把变量值存放在栈中,如果是引用类型,则存放地址在栈中。

 

  栈内存中的变量无须系统垃圾回收,变量是随方法或代码块的运行结束而结束的。因此,局部变量的作用域是从初始化该变量开始,知道该方法或该代码块运行完成而结束。因为局部变量只保存基本类型的值或对象的引用,因此局部变量所占的内存通常较小。

 

 

四、变量的定义和选择

  1.考虑变量的生存时间,影响系统(内存)开销

 

  2.扩大了作用域,不利于程序的内聚性。

 

开发中应尽量缩小变量的作用域,这样在内存中停留时间越短,性能也就越高。

尽量不使用static修饰,一般定义工具方法时或者static方法需要访问的变量,该变量属于类的时候,需要使用static修饰

也不要动不动就使用成员变量,因为存在着线程不安全的问题。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325112880&siteId=291194637