[Java] 變數的範圍與類型
變數的初始化:
| 區域變數 (Local variables) | 必須手動初始化 | 
| 實例變數(Instance variables、Non-Static Fields)靜態屬性變數 (Class Variables、Static Fields)通稱 屬性變數(Field Variable) | 若未指定 Java 會給予預設值 | 
變數預設值:
| byte | 0(byte 型態) | 
| short | 0(short 型態) | 
| int | 0 | 
| long | 0L | 
| float | 0.0F | 
| double | 0.0D | 
| char | ‘\u0000’(NULL) | 
| boolean | false | 
同場加映:
因為 variable shadowing 在 Java 中是不存在的,所以以下的範例二會無法編譯!
class X
{  
    public static void main(String args[])
    {
        {
            int a = 2;
        }
        {
            int a = 3;
        }       
    }
}
▲編譯成功
class X
{  
    public static void main(String args[])
    {
        int a = 2;
        {
            int a = 3;
        }       
    }
}
▲編譯失敗
但有趣的是,你可以在如下的範例中找到 variable shadowing 的”影子”
class A
{
    int x = 0;
    void m()
    {
        int x = 10; // Shadows this.x
    }
}
參考資料(Reference):
- Java Variable Types – http://www.tutorialspoint.com/java/java_variable_types.htm
- 變數的種類 – http://www.oreilly.com.tw/column_sleepless.php?id=j015
- 基礎程式設計(13)-變數範圍 – http://www.moke.tw/wordpress/computer/programming/337
- Variables (The Java™ Tutorials) – http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
- java – Block scope variables – Stack Overflow – http://stackoverflow.com/questions/20499554/block-scope-variables
 
- Java – static variable with example – http://beginnersbook.com/2013/05/static-variable/
- 小狐狸事務所: Java 複習筆記 : 變數與記憶體 – http://yhhuang1966.blogspot.tw/2014/03/java_25.html
- ThiS WORLD, MY WORLD: 引數?! 參數??!! 什麼鬼啊!! – http://thisworldmyworld.blogspot.tw/2009/12/blog-post_08.html
![[Java] 關於 Garbage Collection 的二三事(一)](;https://img.alexleo.click/teambob/java-coffee.png) 
																			![[Linux] 如何安裝 Eclipse 在 Ubuntu 14.04](;https://img.alexleo.click/teambob/eclipse.png) 
																			 
																											 
																											![[Linux] 如何使用 LUKS 建立加密的磁碟映像檔](https://img.alexleo.click/Team-BoB_luks_disk_image/pixy.org_97715-small.jpg) 
																											![[筆記] Matplotlib 使用上的一些建議](https://img.alexleo.click/Team-BoB_matplotlib_notes/title.jpg) 
																											![[Git 筆記] merge、squash、rebase 三種方式的比較](https://img.alexleo.click/Team-BoB_git_merge_squash_rebase/cover.jpg) 
																											![[JavaScript] 手把手一起入門(二) – 變數 & 基本操作](;https://img.alexleo.click/teambob/WHATWG_JavaScript_logo.png) 
																											![[C/C++] Linklist 的架構與使用](;https://img.alexleo.click/teambob/2405613966_c68110ca76_o.jpg)