常用类
包装类基本知识
基本数据类型 | 包装类 |
---|---|
byte | Byte |
boolean | Boolean |
short | Short |
char | Character |
int | Integer |
long | Long |
folat | Folat |
double | Double |
自动装箱和拆箱
自动装箱:
基本类型的数据处于需要对象的环境时,会自动转换为“对象”
在JDK1.5以前,这样的代码Integer i = 5 是错误的,必须要通过Integer i = new Integer(5)这样的语句来实现基本数据类型转换成包装类的过程;而在JDK1.5以后,Java提供了自动装箱的功能,因此只需要Integer i =5这样的语句就能实现基本数据类型转换成包装类,这是因为JVM为我们执行了Integer i = Integer.valueOf(5)这样的操作,这就是Java的自动装箱
Integer i = 100;//自动装箱
//相当于编译器自动为您执行以下的语法编译:
Integer i = Integer.valueOf(100);//调用的是valueOf(100),而不是new Integer(100)
自动拆箱:
每当需要一个值时,对象会自动转换成基本数据类型,没必要再去显示调用intValue()、doubleValue()等转型方法
Integer i = 100;
int j = i;//自动拆箱
//相当于编译器自动为您执行以下的语法编译
int j = i.intValue();
缓存;
- 缓存[-128,127]之间的数字。实际就是系统初始的时候,创建了[-128,127]之间的一个缓存数组
- 当我们调用valueOf()的时候,首先检查是否在[-18,127]之间,如果在这个范围则直接从缓存数组中拿出已经存在的对象
- 如果不在这个范围,则创建新的Integer对象
String类
StringBuilder
public static void main(String[] args) {
//线程不安全,效率高(一般使用);StringBuffer线程安全,效率低
StringBuilder sb = new StringBuilder("abcdefg");
System.out.println(Integer.toHexString(sb.hashCode()));
System.out.println(sb);
System.out.println("-----------------------");
sb.setCharAt(1,'B');
System.out.println(Integer.toHexString(sb.hashCode()));
System.out.println(sb);
}
@Test
public void test(){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 26; i++){
sb.append((char)(65+i));
}
System.out.println(sb);
System.out.println("-------------------");
sb.reverse();//倒叙
System.out.println(sb);
sb.setCharAt(3,'Q');
//链式调用 核心就是该方法返回return this
sb.insert(4,'W').insert(5,'E');
System.out.println("----------");
//也可以链式调用
sb.delete(24,25);
System.out.println(sb);
}
时间处理相关类
public static void main(String[] args) throws ParseException {
Date date = new Date();
System.out.println(date.getTime());
System.out.println(System.currentTimeMillis());
//把时间对象按照指定格式转换成字符串
SimpleDateFormat df = new SimpleDateFormat("yyyy年-MM月-dd日");
System.out.println(df.format(date));
System.out.println("-------------------");
//把字符串按照“格式字符串指定的格式”转成相应的时间对象
SimpleDateFormat df2 = new SimpleDateFormat("yyyy年-MM月-dd日");
Date d = df2.parse("2018年-12月-25日");
System.out.println(d);
}
Calendar日历类
public static void main(String[] args) {
GregorianCalendar calendar = new GregorianCalendar(2018,9,21);
System.out.println(calendar.get(Calendar.YEAR));
}
File类的基本用法
public static void main(String[] args) throws IOException {
File file = new File("a.txt");
//判断文件是否存在
System.out.println(file.exists());
System.out.println(file);
//改名操作
file.renameTo(new File("b.txt"));
//目录 打印工程目录
System.out.println(System.getProperty("user.dir"));
//创建文件
File file1 = new File("gg.txt");
file1.createNewFile();
//创建目录
File file2 = new File("test");
file2.mkdir();
//使用递归遍历目录树
printFile(new File("F:\\Java\\视频教程\\2018年尚学堂_高淇_Java300集"),0);
}
public static void printFile(File file, int leve){
//输出层数
for(int i = 0; i < leve; i++){
System.out.print("-");
}
//输出文件名字
System.out.println(file.getName());
//判断文件是否为目录
if (file.isDirectory()){
//将目录下的文件存放到file数组中
File[] files = file.listFiles();
//遍历
for(File temp : files){
//递归调用
printFile(temp,leve+1);
}
}
}
枚举(enum)
枚举体就是放置一些常量
enum Season{
SPRING,SUMMER,AUTUMN,WINDER
}