一、String类:
String对象代表不可变的Unicode字符序,String对象的内部成员变量的值被赋值后无法改变;
1.String类的底层结构:
String类的底层数据结构是char类型的数组;
String的相应方法的实现实际上就是对数组的一个操作;
2.String的常用方法:

2.StringBuffer和StringBuilder类:
StringBuffer和StringBuilder非常类似,均代表可变的字符序列。 这两个类都是抽象类AbstractStringBuilder的子类,方法几乎一模一样。
(1)二者的区别:
StringBuffer:线程安全,做线程同步检查,效率较低;适合多线程。
StringBuilder:线程不安全,不做线程同步检查,因此效率较高;适合单线程。
(2)常用方法:
1)append()方法:可以为该StringBuilder 对象添加字符序列,仍然返回自身对象。
2)index(String str):返回指定子字符串第一次出现的字符串内的索引。
3)charAt(int index):返回char在指定索引在这个序列值。


3.可变字符和不可变字符
(1)String:不可变字符序列;StringBuffer和StringBuilder可变字符序列;
(2)在频繁修改字符串时StringBuffer的效率更高;


二、Date时间类:
类Date表示特定的瞬间,精确到毫秒;
在 JDK 1.1 之前,类Date有两个其他的函数。它允许把日期解释为年、月、日、小时、分钟和秒值。它也允许格式化和解析日期字符串。
从 JDK 1.1 开始,应该使用Calendar类实现日期和时间字段之间转换,使用DateFormat类来格式化和解析日期字符串。Date中的相应方法已废弃。

1.Date类的常用方法:
1)getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此Date对象表示的毫秒数;
2)setTime():设置此Date对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后time毫秒的时间点。
3)after(Date when):测试此日期是否在指定日期之后;
4)before(Date when):测试此日期是否在指定日期之前。
5)equals(Object obj):比较两个日期的相等性。


2.DateFormat类和SimpleDataFormat类:
(1)DateFormat 类:
是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期时间。
(2)常用方法:
1)format(Date date,StringBuffer toAppend,ToFilePosition fieldPosition)将一个 Date 格式化为日期/时间字符串。
2)getNumberFormat():获取此日期/时间格式器用于格式化和解析时间的数字格式器。
3)getDateInstance():获取日期格式器,该格式器具有默认语言环境的默认格式化风格。
4)parse(String source):从给定字符串的开始解析文本,以生成一个日期。
(2)SimpleDateFormatSimpleDateFormat类:
是一个以与语言环境有关的方式来格式化和解析日期的具体类。它继承了抽象类DateFormat并继承了DateFormat的方法,允许进行格式化(日期 -> 文本)、(文本 -> 日期)和规范化。
(3)二者的关系:
DateFormat是日期时间格式化子类的抽象类;SimpleDateFormat是DateFormat的子类;SimlepDateFormat是一个与语言环境有关的方式和解析日期的具体类。


三、Calendar类:
1.Calendar类:
Calendar 类是一个抽象类,为我们提供了关于日期计算的相关功能,比如:年、月、日、时、分、秒的展示和计算。
2.GregorianCalendar类:
GregorianCalendar是Calendar的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。
(1)常用方法:
1)add(int field, int amount):根据日历规则,将指定的(有符号的)时间量添加到给定的日历字段中。
2)set(int year, int month, int date, int hourOfDay, int minute, int second):设置字段YEAR、MONTH、DAY_OF_MONTH、HOUR、MINUTE和SECOND的值。
3)get(int field)返回给定日历字段的值。
4)getAvtualMaximum(int field):给定此Calendar的时间值,返回指定日历字段可能拥有的最大值。


(2)可视化日历的编写:


四、Math类:
1.Math类常用的方法:
(1)abs:求绝对值;
(2)sqrt:平方根;
(3)pow(double a, double b):求a的b次幂;
(4)max(double a,double b):取大值;
(5)min(double a,double b):取小值;
(6)random() 返回 0.0 到 1.0 的随机数;
2.静态导入:Import static java.lang.Math.*;


五、File类:
1.File类的作用:
文件和目录路径名的抽象表示形式;代表文件和目录;在开发中,读取文件,生成文件,删除文件,修改文件的属性时经常会用到。
2.File类操作目录和文件的常用方法:
(1)通过文件对象访问文件属性:



(2)通过文件对象创建空文件或目录



3.使用递归遍历目录树:


六、枚举:
(1)枚举类型的定义包括枚举声明和枚举体。
enum枚举名 {
枚举体(常量列表)
}
(2)枚举的实质还是类;每个被枚举的成员实质就是一个枚举类型的实例,都是public static final修饰;通过枚举类型名直接调用。
(3)当需要定义一组常量时,可以使用枚举类型;


网友评论