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
     }
}

静态代码块的使用

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