JAVA基礎之表達式,語句,和塊

JAVA基礎之表達式,語句,和塊

表達式(expression)

  • 表達式是遵循java語法的基礎上,由變量(variable),運算符(operators),方法調用(method invocations)並返回單個值的組合 eg:
int cadence = 0;    //int cadence = 0
anArray[0] = 100;   // anArray[0] = 100
System.out.println("Element 1 at index 0: " + anArray[0]);
int result = 1 + 2; // result is now 3
if (value1 == value2)
  System.out.println("value1 == value2");
  • 複合表達式(compound expression)
  1 * 2 * 3
  • 運算符的優先級

提倡清楚明確的括號

  x + y / 100;  //ambiguous
  x + (y / 100) // unambiguous, recommended

Statements(語句)

  • 賦值語句(Assignment expressions)
  • 自增自減語句(Inrement statements ,any use of ++ or –)
  • 方法執行語句(Method invocation)
  • 對象創建語句(object creation expression)
  • 聲明語句(declaration statements)
  • 控制語句(control flow statemets)
// assignment statement
aValue = 8933.234;
// increment statement
aValue++;
// method invocation statement
System.out.println("Hello World!");
// object creation statement
Bicycle myBike = new Bicycle();

blocks (塊)

即大括號包含的語句

class BlockDemo {
     public static void main(String[] args) {
          boolean condition = true;
          if (condition) { // begin block 1
               System.out.println("Condition is true.");
          } // end block one
          else { // begin block 2
               System.out.println("Condition is false.");
          } // end block 2
     }
}

靜態代碼塊的使用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章