區域變數全域變數

全域變數

在方法()之外宣告的變數為全域變數,生命周期跟著物件同生共死。
以下為方法之外宣告的變數,也是成員屬性。

1
2
3
4
5
6
7
8
9
10
public class Variable {
  int global = 100;
  public static void main(String[] args) {

  }
  
  public void func1() {

  }
}

全域變數預設值

全域變數沒有設值,也會有預設值。

類型 預設值
int 0
short 0
byte 0
long 0
float 0.0f
double 0.0
char \u0000
boolean false
String null
1
2
3
4
5
6
7
8
9
10
11
public class Variable {
  int global;
  public static void main(String[] args) {
    Variable variable = new Variable();
    variable.func1();
  }

  public void func1() {
    System.out.println("global = " + global);
  }
}
0

區域變數

在方法()之內宣告的變數為區域變數,生命周期跟著方法()執行完畢,區域變數會被記憶體回收。
所謂的記憶體回收是指,告訴系統某一區塊的記憶體已經沒人使用,可以拿去存放其它變數。

區域變數沒有預設值,使用前一定要指派值給區域變數。
以下編譯失敗,因為要試圖輸出沒有值的區域變數。

1
2
3
4
  public void func1() {
    int local;
    System.out.println(local);
  }

results matching ""

    No results matching ""