
byte, short, int, long, float, double, boolean, charbyte, short, int, long, 默认int 类型byte 类型在内存占 1 个字节, 表示范围 -2^7 ~ 2^7-1short 类型在内存占 2 个字节, 表示范围 -2^15 ~ 2^15-1int 类型在内存占 4 个字节, 表示范围 -2^31 ~ 2^31-1long 类型在内存占 8 个字节, 表示范围 -2^63 ~ 2^63-1int 更大的量, 在直接量后面加上 l 或 Lfloat, double, 默认 doublefloat 类型在内存占 4 个字节, 单精度浮点数, 可以表示 7 位有效数字double 类型在内存占 8 个字节, 双精度浮点数, 可以表示 15 位有效数字double 精度小的量, 在直接量后面加上 f 或者 Fjava.math.BigDecimalboolean, 只有 true 或 false, 赋别的值会报错char, 如: 'a', '人''\n'-10 SPACE-32 '0'-48 'A'-65 'a'-97{'\0', '\t', '\n', '\r', '\"', '\'', '\\'}byte->short,char->int->long->float->doubletarget_type variable_name = (target_type) source_varable_name + - * / % 5/0 -> 算数异常;
5/0.0 -> Infinity;
0/0.0 -> NaN, Not a Number;a=1, b=1, c=1;
a+b+c->3;
a+b+c+""->3;
a+b+""+c->21;
a+""+b+c->111;
""+a+b+c->111;
""+(a+b+c)->3;> < >= <= == !=++ --a++: 先让 a 的数值作为整个表达式的最终结果, 然后让 a 自增++a: 先让 a 自增, 然后让 a 的新值作为整个表达式的最终结果a++ + ++a; -> x+y where x=a, y=a+1+1 -> 2a+2&& || !(false) && (++a == 5); -> (++a == 5) will not be executed
(true) || (++b == 5); -> (++b == 5) will not be executedvariable = Expression1 ? Expression2 : Expression3;= += -= *= /=byte b = 1;
b = b + 3 -> error: incompatible type;
b = b + (byte)3 -> 报错, 编译器优化导致最终还是 int;
b = (byte)(b + 3) -> 4;
b += 3 -> b = (byte)(b + 3) -> 4;"<<" 左移运算符, 将数据的二进制位向左移动, 右边使用 0 补充byte b1 = 13, byte b2 = (byte)(b1 << 1) -> 26;
13 << 1 -> 0000 1101 << 1 -> 0001 1010 -> 26;
-13 << 1 -> 1111 0011 << 1 -> 1110 0110 -> -26;">>" 右移运算符, 将数据的二进制位向右移动, 左边使用符号位补充-13 >> 1 -> 1111 0011 >> 1 -> 1111 1001 -> -7;">>>" 逻辑右移运算符, 将数据的二进制位向右移动, 右边使用 0 补充-13 >>> 1 -> 0111 ... 1111 1001 -> 2147483641 // 默认32位& 表示按位'与'运算符, 按照二进制位进行与运算, 同 1 为 1, 一 0 为 011 & 13 -> 0000 1011 & 0000 1101 -> 0000 1001 -> 9| 表示按位'或'运算符, 按照二进制位进行或运算, 同 0 为 0, 一 1 为 111 | 13 -> 0000 1011 | 0000 1101 -> 0000 1111 -> 15^ 表示按位"异或"运算符, 按照二进制位进行异或运算, 同为 0, 不同为 111 ^ 13 -> 0000 1011 ^ 0000 1101 -> 0000 0110 -> 6-11 ^ -13 -> 1111 0101 ^ 1111 0011 -> 0000 0110 -> 6~ 表示按位"取反"运算符, 按照二进制位进行取反运算, 1 为 0, 0 为 1~ 11 -> ~ 0000 1011 -> 1111 0100 -> -12if () {}
if () {} else {}
if () {} else if () {} else {}
switch (Type) {
// Type 支持的数据类型:
// byte, short, char, int,
// jdk 1.5 开始支持枚举类型,
// jdk 1.7 开始支持 String 类型
case 0:
case 1:
Statement1;
break;
case 2:
statement2;
break;
default:
statement;
// 放在最后可以省略 break, 否则要加上
// break;
} //适合于明确循环次数的场合, 也可以成为死循环
for (initialization; condition; iteration) {
body;
}
//do while 循环格式: 最后要加分号
//do while 循环主要用于至少执行一次循环体的场合
do {
body;
} while (condition);
// while循环适合于明确循环条件, 但不明确循环次数的场合
while (condition) {
body;
}
continue // 语句, 结束本次循环开始下次循环
break // 关键字, 退出当前语句块, 在循环体中用于退出循环
//如果要退出外层循环, 使用标号
outer: for(...) {
for(...) {
break outer;
}
}ArrayIndexOutOfBoundsException 数组下标越界异常ArithmeticException 算术异常
用于存放程序运行过程中所有的局部变量
用于存储使用 new 关键字创建的数组和对象
DataType[] array = new DataType[array.length]array.length - 1DataType[] array = new DataType[]{value0, value1, value2}DataType[] array = {value0, value1, value2} // simplifiedbyte, short, char, int, long 为 0;float, double 为 0.0; boolean 为 false;java.util.Arrays 类可以实现对数组中元素的遍历, 查找, 排序等操作Arrays.toString(array);Arrays.fill(array, val);Arrays.equals(array1, array2);Arrays.sort(array);Arrays.binarySearch(array, val);DataType[][] array = new DataType[row][col];DataType[][] array = {{element0, element1, ...}, ...};
int[][] arr = new int[3][]; // 每一行列数不一样
arr[0] = new int[3];
arr[1] = new int[4];
arr[2] = new int[5];