[Java]基礎觀念(3)-數值運算
Java 變數
一種需經宣告初始化而使用,宣告形態不能再改變
修飾字 | 型態 | 變數名 | =初值 | ; |
---|---|---|---|---|
final | int | v | =6 | ; |
一經設定後就不能在改變值 | int | v | 可以先不預設初值 | ; |
變數 | 區域變數 | {}一程式區塊自宣告開始存在,同一層右大括弧消失 |
成員變數 |
class LocalVar{ public static void main(String[] args){ int i = 4; { int j =3; { int k=2; System.out.println(i); System.out.println(j); System.out.println(k); } System.out.println(i); System.out.println(j); System.out.println(k); //無法執行 } System.out.println(i); System.out.println(j);//無法執行 System.out.println(k); //無法執行 } } |
意思 | 符號 |
---|---|
程式區塊 | { } |
陣列 | [ ] |
運算 | ( ) |
方法表示 | |
轉型 |
整數運算 | 運算順序 |
---|---|
i++ |
|
++i |
|
i-- |
|
--i |
|
class TestOperator1{
public static void main(String[] args){
int i=5;
int j=5;
System.out.println(i+++j); //10
System.out.println(i); //6
System.out.println(j);
//5 System.out.println("===============================1");
i=5;
j=5;
System.out.println(i++ +j); //10
System.out.println(i); //6
System.out.println(j); //5
System.out.println("===============================2"); i=5;
j=5;
System.out.println(i+ ++j); //11
System.out.println(i);//5
System.out.println(j); //6
System.out.println("===============================3");
i=5;
j=5;
System.out.println(++i); //6
System.out.println(j++); //5
System.out.println("===============================4");
i=5;j=5;
System.out.println(i+++ ++j); //11
System.out.println(i);//6
System.out.println(j);//6
System.out.println("===============================5");
i=5;j=5;
System.out.println(i+++j++); //10
System.out.println(i); //6
System.out.println(j); //6
System.out.println("===============================6");
i=1;
i=i++;
System.out.println(i); //1
System.out.println("===============================7");
i=1;
j=i-- + ++i;
System.out.println(i);//1
System.out.println(j);//2
System.out.println("===============================8");
i=1;
j=i-- + ++i+i-- + ++i;
System.out.println(i); //1
System.out.println(j); //4
System.out.println("===============================9");
i=1;
j=1;
int k=i-- + ++j;
System.out.println(i); //0
System.out.println(j); //2
System.out.println(k); //3
System.out.println("===============================10");
}
}
留言
張貼留言