Java 基本语法
创建: 2020/11/15
完成: 2020/11/16
更新: 2020/11/21 增加foreach
| method结构 | |||||||||||||||||
| 声明 |
retType mthName(pType0 p0, pType1 p1, ...) { ... } // 声明抛出的例外 retType mthName(pType0 p0, pType1 p1, ...) throws exceptionList { ... } 没有返回值的声明void |
||||||||||||||||
| 呼出 |
mthName(args);
|
||||||||||||||||
| static方法和变量 | |||||||||||||||||
| 执行static方法 |
className.mthName(args);
|
||||||||||||||||
| 获取static变量 |
className.varName
|
||||||||||||||||
| instance方法和变量 | |||||||||||||||||
| 执行instance方法 |
objRef.mthName(args);
|
||||||||||||||||
| 获取instance变量 |
objName.varName
|
||||||||||||||||
| new | |||||||||||||||||
| 生成类实例 |
clsName objRef = new clsName(args);
|
||||||||||||||||
| command line参数 | |||||||||||||||||
| String args[] |
public static void main(String args[]) {}
|
||||||||||||||||
| 控制语法 | |||||||||||||||||
| if |
if (expression) { ... } else if (expression) { ... } else { ... }
|
||||||||||||||||
| switch |
switch(exp) { case exp1: ... break; case exp2: ... break; default: ... break; }
|
||||||||||||||||
| for |
for(initialization; test; increment) { ... }
|
||||||||||||||||
| foreach |
for(type varName : collection) { ... }
|
||||||||||||||||
| while |
while(exp) { ... }
|
||||||||||||||||
| do-while |
do { ... } while(exp);
|
||||||||||||||||
| break | 退出循环
break; break label;
|
||||||||||||||||
| continue |
直接进下一循环 continue; continue label;
|
||||||||||||||||
| 运算符 | |||||||||||||||||
| 关系运算符 |
String只有!=和== |
||||||||||||||||
| 逻辑运算 |
|
||||||||||||||||
| 比特运算 |
只用于整数型(char, byte, short, int, long)
|
||||||||||||||||
| 递增递减 |
++, --
|
||||||||||||||||
| 三项运算符 |
exp1 ? exp2 : exp3
|
||||||||||||||||

浙公网安备 33010602011771号