第3章. Java基本程序结构 -- 详细阅读
1. 强行退出: System.exit(0);
2. 数据类型
2.1. 整形(允许负数)
int 4字节 32位
short 2字节 16位
long 8字节 64位
byte 1字节 8位
2.2. 浮点型
float 4字节 32位
double 8字节 64位
2.3. char类型
转义写法: /b 退格,/t制表,/n换行,/r回车,/"双引号,/'单引号,//反斜杠
2.4. boolean类型
3. 类常量static final - 可以在一个类的多个方法中使用,常量名大写,定义在方法体外。
4. 枚举类型
enum Size{SMALL, MEDIUM, LARGE, EXTRA_LARGE};
Size s = Size.MEDIUM;
5. 比较字符串相等(equals),一定不要使用==,不区分大小写使用equalsIgnoreCase,==只是比较两个字符串是否被放置在同一个位置。
6. java.util.Scanner
6.1 读取输入行 Scanner in = new Scanner(System.in);
String name = in.nextLine();
int age = in.nextInt();
6.2 读取文件中的内容 Scanner scanner = new Scanner(new File(filename), "BIG5"); //编码方式
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
}
7. String to Integer: Integer.parseInt(String);
Integer to String: Integer.toString();
int to String: String.valueOf(int);
8. while(condition) statement; // if condition is false, do not execute statment
do statement while (condition); //至少执行一次
9. swith (input) {} // case标签必须是整数或者枚举常量,不能检测字符串。
10. 大数字 BigInteger / BigDecimal
BigInteger a = BigInteger.valueOf(100);
BigInteger c = a.add(b);
11. 数组 - 允许数组长度为0
int[] a = new int[100];
int[] anonymous = {17, 20, 32, 48};
数组拷贝: System.arraycopy(from, fromIndex, to, toIndex, count);
Arrays.sort(a); //数组排序
本文详细介绍了Java的基本程序结构,包括数据类型如整型、浮点型、字符型及布尔型,类常量的使用方法,枚举类型的定义,字符串比较的方式,以及如何使用Scanner进行输入读取。此外还讲解了条件语句、循环语句、数组操作等核心概念。
1232

被折叠的 条评论
为什么被折叠?



