1 异常
1.1 Exception
CheckException 检查型如SQL,IOS编译器检查会提示
UncheckException 非检查型,编译器不需检查如算数异常,下标越界,空指针,数据类型转换
1.2 try catch finally
try{...} catch (Exception类型1 e){...} catch (Exception类型2 e){...} catch (Exception e){...}finally{// 最后必会执行的语句块// 需要return时,些代码块慎用retrun, 会先执行完finally后才会执行try catch的retrun。...}
1.3 throw throws
2 包装类
包装类的装箱、拆箱理解
// 自动装箱Integer a = 123;// 手动装箱Integer b = new Integer(123);// 自动拆箱System.out.println(a);// 手动拆箱System.out.println(b.intValue());
2.1 特殊包装类
2.1.1 Int char
Int - Integer char - character
2.1.2 float doubble
// -128 ~ 128的缓存区// float doubble没有缓存区Integer a1 = 100;Integer a2 = 100;System.out.println(a1 == a2); // trueInteger b1 = 200;Integer b2 = 200;System.out.println(b1 == b2); // false
3 字符串
3.1 常用方法
length trim indexOf lastIndexOf substring
3.1.1 字符与byte的互换
String str1 = "字符与byte的互换";byte[] arr= str1.getBytes("GBK");String str2 = new String(arr, "GBK");
3.1.2 字符串在内存空间的原理
3.2 String、StringBuilder和StringBuffer
String 不可变
StringBuilder 用于频繁对字符串更改的场景
StringBuffer 与builder使用一样,用于多线程(安全线程),性能也会损耗,比builder弱一点
3.3 ==与equals()
== 比较字符串对象的内存地址
equals() 比较字符串对象的值
4 集合
数组与集合 数组:固定长度 集合:动态长度,一对一关系,频烦增加的效率,数据重复

集合框架的体系结构

4.1 ArrayList
- 有序的,可以重复的集合。常用方法:add, remove, set …
- ArrayList适合用于对数据的查看与更新。
- 不建议插入数据操作(特别非最后插入数据的方式),因为基础数组的底层原理进行操作,会导致性能一般。
4.2 HashSet
- 无序的,不可重复的集合。所以也只能有一个null元素。
- 底层是HashMap
- 有良好存储、查找性能
- 遍历没有get方法,能使用迭代器iterator实现。iterator.hasNext(), iterator.next()
- add方法没有index参数。因为是HashSet是无序的。
- 查找使用contains()方法
- 进行remove操作需注意:使用foreach遍历set集合进行单个reomve成功后,需要break操作,否则会继续遍历抛出异常java.util.ConcurrentModificationException。因为set的size是动态变化的,删除后set的集合大小发生了变化并且set的遍历是无序的。删除多个可以使用removeAll()进行解决。
4.3 Map
4.3.1 遍历map的2个方法
- 无序的,不可重复key的集合。所以也只能有一个key为null的元素。
- 多个相同key的插入,会覆盖对应key的value。(使用最后插入的value)
Map<String, User> userList = new HashMap<String, User>();// entrySet 获取entrySet集合,foreach遍历Set<Entry<String, User>> entrySet = userList.entrySet();for (Entry<String, User> entry : entrySet) {System.out.print(entry.getKey() + "---");System.out.println(entry.getValue());}// iterator迭达器Iterator<User> it = userList.values().iterator();while (it.hasNext()) {System.out.println(it.next());}
5 线程
- 线程与进程的区别
- 线程创建所用到的类与接口:Thread与Runnable
- 线程的执行是根据cpu来分配的,因为线程的执行会不确定。哪下面代码的输出结果。
// 自定义Threadpublic class MyThread extends Thread {public MyThread() {// TODO Auto-generated constructor stub}public MyThread(String threadName) {// TODO Auto-generated constructor stubsuper(threadName);}public void run() {for (int i = 0; i < 5; i++) {System.out.println(this.getName() + "正在运行循环体第" + i + "次");}}}// Thread的测试public class MyThreadTest {public MyThreadTest() {// TODO Auto-generated constructor stub}public static void main(String[] args) {// TODO Auto-generated method stubMyThread myThread1 = new MyThread("AAAAA");MyThread myThread2 = new MyThread("BBBBB");myThread1.start();myThread2.start();}}// 输出结果AAAAA正在运行循环体第0次AAAAA正在运行循环体第1次BBBBB正在运行循环体第0次BBBBB正在运行循环体第1次BBBBB正在运行循环体第2次BBBBB正在运行循环体第3次AAAAA正在运行循环体第2次BBBBB正在运行循环体第4次AAAAA正在运行循环体第3次AAAAA正在运行循环体第4次
5.1 生命周期
5.2 sleep,join
sleep(long millis) 休眠进程
join(long millis) 带时间的join会按参数millis来占用进程时间片,无论当前进程是否已完成
5.3 线程优先级
getPriority,setPriority
虽然能设置线程的优先级,但由于cpu的执行随机性,不一定高级的优化执行。如 1级的ThreadA.start()可能比10级的ThreadB.start()先执行。
5.3 同步与阻塞
synchronized
wait() 注意出现相互等待造成死琐,一般配合使用notifyAll()唤醒进程
notify() notifyAll()
6 IO流
6.1 File
io流的对象操作完后都需执行各对象的close()操作,释放资源。
io对象包括:
- FileInputStream
- FileOutputStream
- BufferedInputStream
- BufferedOutputStream
6.2 字节流
File file = new File("/Users/zhangguoye/Downloads/javaIO");File file1 = new File(file, "a/b/c");File file2 = new File(file, "text.txt");// 创建目录if(!file1.exists()){file1.mkdirs();}// 读文件if(!file2.exists()){try {file2.createNewFile();} catch (IOException e) {e.printStackTrace();}}
6.2.1 输入流
FileInputStream fileStream1 = new FileInputStream(file2);// 读出一个fileStream1.read();// 读取出一段字节,通常使用 返回-1判断是否读取完毕,进行循环读取 while(..)byte[] b = new byte[(int) file2.length()];fileStream1.read(b);
6.2.2 输出流
File file = new File(path + "text.txt");FileInputStream fis = new FileInputStream(path + "text.txt");FileOutputStream fos = new FileOutputStream(path + "text_" + new Date().getTime() + ".txt");// 文件大小的byte长度byte[] b = new byte[(int) file.length()];int n = 0;// 读取出一段字节,通常使用 返回-1判断是否读取完毕,进行循环读取 while(..)while ((n = fis.read(b)) != -1) {// 边读边写fis.read(b);fos.write(b);}
6.2.3 缓冲流
使用方法跟输入输出流一样。
- BufferedInputStream
- BufferedOutputStream 注意wirite()后使用flush()方法,由于缓冲区有一定的大小,若缓冲区未填充满,需要flush()清空缓冲区的数据才能让buffere进行写入。close()也能使缓冲区的数据写入
6.5 字符流
与字节流的使用方法大致一样,包括以下常用的类。
- InputStreamRead
- OutputStreamWrite
- BufferedRead
- BufferedWreit
- FileRead
- FileWrite
6.6 对象序列化与反序列化
- 类需要实现序列化接口 Serializable
- ObjectInputStream
- ObjectOutputStream

