Constant
final int i=10; // as const in C.
整數類型的轉換
為了防止整數溢位,可以在其數值的後方加上一個L, 強制轉換成long類型。
int i=java.lang.Integer.MAX_VALUE; //讓java提供整數的最大值
System.out.println("Value: "+(i+100L)); // 會使i的結果為long 類型
Input data from user
import java.util.Scanner;
...
Scanner scn = new Scanner(System.in);
int i;
double d;
i=scn.nextInt();
d=scn.nextDouble();
運算元、運算子
運算元(operand) : 變數或數值
運算子(operator) : 運算符號
宣告陣列和分配記憶體空間
int arr[]; //宣告陣列但未分配記憶體位址
arr = new int[4]
宣告陣列並初始數值
int a[] = {40, 39, 38};
二維陣列
int score[][];
score = new int[4][3];
// 或合在一起寫成
int score[][] = new int[4][3];