1 异常

JAVA基础三(常用工具) - 图1

1.1 Exception

CheckException 检查型如SQL,IOS编译器检查会提示
UncheckException 非检查型,编译器不需检查如算数异常,下标越界,空指针,数据类型转换

1.2 try catch finally

  1. try{
  2. ...
  3. } catch (Exception类型1 e){
  4. ...
  5. } catch (Exception类型2 e){
  6. ...
  7. } catch (Exception e){
  8. ...
  9. }finally{
  10. // 最后必会执行的语句块
  11. // 需要return时,些代码块慎用retrun, 会先执行完finally后才会执行try catch的retrun。
  12. ...
  13. }

1.3 throw throws

2 包装类

包装类的装箱、拆箱理解

  1. // 自动装箱
  2. Integer a = 123;
  3. // 手动装箱
  4. Integer b = new Integer(123);
  5. // 自动拆箱
  6. System.out.println(a);
  7. // 手动拆箱
  8. System.out.println(b.intValue());

2.1 特殊包装类

2.1.1 Int char

Int - Integer char - character

2.1.2 float doubble

  1. // -128 ~ 128的缓存区
  2. // float doubble没有缓存区
  3. Integer a1 = 100;
  4. Integer a2 = 100;
  5. System.out.println(a1 == a2); // true
  6. Integer b1 = 200;
  7. Integer b2 = 200;
  8. System.out.println(b1 == b2); // false

3 字符串

3.1 常用方法

length trim indexOf lastIndexOf substring

3.1.1 字符与byte的互换

  1. String str1 = "字符与byte的互换";
  2. byte[] arr= str1.getBytes("GBK");
  3. String str2 = new String(arr, "GBK");

3.1.2 字符串在内存空间的原理

image.png

3.2 String、StringBuilder和StringBuffer

String 不可变
StringBuilder 用于频繁对字符串更改的场景
StringBuffer 与builder使用一样,用于多线程(安全线程),性能也会损耗,比builder弱一点

3.3 ==与equals()

== 比较字符串对象的内存地址
equals() 比较字符串对象的值

4 集合

数组与集合 数组:固定长度 集合:动态长度,一对一关系,频烦增加的效率,数据重复

JAVA基础三(常用工具) - 图3 JAVA基础三(常用工具) - 图4

集合框架的体系结构

image.png

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)
  1. Map<String, User> userList = new HashMap<String, User>();
  2. // entrySet 获取entrySet集合,foreach遍历
  3. Set<Entry<String, User>> entrySet = userList.entrySet();
  4. for (Entry<String, User> entry : entrySet) {
  5. System.out.print(entry.getKey() + "---");
  6. System.out.println(entry.getValue());
  7. }
  8. // iterator迭达器
  9. Iterator<User> it = userList.values().iterator();
  10. while (it.hasNext()) {
  11. System.out.println(it.next());
  12. }

5 线程

  • 线程与进程的区别
  • 线程创建所用到的类与接口:Thread与Runnable
  • 线程的执行是根据cpu来分配的,因为线程的执行会不确定。哪下面代码的输出结果。
  1. // 自定义Thread
  2. public class MyThread extends Thread {
  3. public MyThread() {
  4. // TODO Auto-generated constructor stub
  5. }
  6. public MyThread(String threadName) {
  7. // TODO Auto-generated constructor stub
  8. super(threadName);
  9. }
  10. public void run() {
  11. for (int i = 0; i < 5; i++) {
  12. System.out.println(this.getName() + "正在运行循环体第" + i + "次");
  13. }
  14. }
  15. }
  16. // Thread的测试
  17. public class MyThreadTest {
  18. public MyThreadTest() {
  19. // TODO Auto-generated constructor stub
  20. }
  21. public static void main(String[] args) {
  22. // TODO Auto-generated method stub
  23. MyThread myThread1 = new MyThread("AAAAA");
  24. MyThread myThread2 = new MyThread("BBBBB");
  25. myThread1.start();
  26. myThread2.start();
  27. }
  28. }
  29. // 输出结果
  30. AAAAA正在运行循环体第0
  31. AAAAA正在运行循环体第1
  32. BBBBB正在运行循环体第0
  33. BBBBB正在运行循环体第1
  34. BBBBB正在运行循环体第2
  35. BBBBB正在运行循环体第3
  36. AAAAA正在运行循环体第2
  37. BBBBB正在运行循环体第4
  38. AAAAA正在运行循环体第3
  39. 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 字节流

  1. File file = new File("/Users/zhangguoye/Downloads/javaIO");
  2. File file1 = new File(file, "a/b/c");
  3. File file2 = new File(file, "text.txt");
  4. // 创建目录
  5. if(!file1.exists()){
  6. file1.mkdirs();
  7. }
  8. // 读文件
  9. if(!file2.exists()){
  10. try {
  11. file2.createNewFile();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. }

6.2.1 输入流

  1. FileInputStream fileStream1 = new FileInputStream(file2);
  2. // 读出一个
  3. fileStream1.read();
  4. // 读取出一段字节,通常使用 返回-1判断是否读取完毕,进行循环读取 while(..)
  5. byte[] b = new byte[(int) file2.length()];
  6. fileStream1.read(b);

6.2.2 输出流

  1. File file = new File(path + "text.txt");
  2. FileInputStream fis = new FileInputStream(path + "text.txt");
  3. FileOutputStream fos = new FileOutputStream(path + "text_" + new Date().getTime() + ".txt");
  4. // 文件大小的byte长度
  5. byte[] b = new byte[(int) file.length()];
  6. int n = 0;
  7. // 读取出一段字节,通常使用 返回-1判断是否读取完毕,进行循环读取 while(..)
  8. while ((n = fis.read(b)) != -1) {
  9. // 边读边写
  10. fis.read(b);
  11. fos.write(b);
  12. }

6.2.3 缓冲流

使用方法跟输入输出流一样。

  • BufferedInputStream
  • BufferedOutputStream 注意wirite()后使用flush()方法,由于缓冲区有一定的大小,若缓冲区未填充满,需要flush()清空缓冲区的数据才能让buffere进行写入。close()也能使缓冲区的数据写入

6.5 字符流

与字节流的使用方法大致一样,包括以下常用的类。

  • InputStreamRead
  • OutputStreamWrite
  • BufferedRead
  • BufferedWreit
  • FileRead
  • FileWrite

6.6 对象序列化与反序列化

  • 类需要实现序列化接口 Serializable
  • ObjectInputStream
  • ObjectOutputStream