String,StringBuffer与StringBuilder的区别(参考)
- String改动字符串需要重新创建字符串对象(不可变字符串,底层被final修饰)【适用情况:操作少量数据】
- StringBuilder线程不安全、但是速度快,所以多数情况下建议使用 StringBuilder 类。【适用情况:多线程操作字符串缓冲区下操作大量数据】
- 在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类(StringBuffer底层被synchronized修饰)【适用情况:单线程操作字符串缓冲区下操作大量数据】
- 初始化上的区别,String可以空赋值,后者不行,报错
![](https://i-blog.csdnimg.cn/blog_migrate/ee778cbc84b1affbfbf51cd491fe4442.png)
java异常处理机制详解(参考)
![](https://i-blog.csdnimg.cn/blog_migrate/c70308da7b4124f1de254897cee49d5f.png)
- Error:程序无法处理的系统错误,编译器不做检查
- Exception:程序可以处理的异常,捕获后可能恢复
- RuntimeException:不可预知的,程序应当自行避免
- 非RuntimeEXception:可预知的,从编译器校验的异常
- Error属于JVM需要负担的责仼;
- RuntimeException是程序应该负担的责任;
- Checked Exception可检查异常是Java编译器应该负担的责任。
package lmm.error;
import java.io.FileNotFoundException;
/**
* @author lmm E-mail:violet_mmhh@163.com
* @time 时间:2019年9月5日
* @function 功能:测试错误和异常
*/
public class ErrorAndException {
private void throwError() {// 运行错误异常
throw new StackOverflowError();
}
private void throwRunTimeException() {// 运行时异常
throw new RuntimeException();
}
private void throwCheckedException() {// 编译时可检查异常
try {
throw new FileNotFoundException();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
ErrorAndException eae = new ErrorAndException();
eae.throwError();
eae.throwRunTimeException();
eae.throwCheckedException();
}
}
常见得Error以及Exception
RuntimeException
- NullPointerException-空指针引用异常
- ClassCastExceptionˉ类型强制转换异常
- illegalArgumentException-传递非法参数异常
- IndexOutOfBounds Exception-下标越界异常
- NumberFormatException-数字格式异常
非RuntimeEXception
- ClassNotFound Exception-找不到指定class的异常
- IOException-IO操作异常
Error
- NoClassDefFound Error-找不到 class'定义的异常
- StackOverflow Error-深递归导致栈被耗尽而拋出岀的异常
- OutOfMemory Error-内存溢出异常
如何捕获
- 抛出异常:创建异常对象,交由运行时系统处理
- 捕获异常:寻找合适的异常处理器处理异常,否则终止运行
捕获原则
- 具体明确:抛出的异常应能通过异常类名和 message准确说明异常的类型和产生异常的原因;
- 提早抛出:应尽可能早的发现并抛出异常,便于精确定位问题;
- 延迟捕获:异常的捕获和处理应尽可能延迟,让掌握更多信息的作用域来处理异常。
java的IO机制(参考)
package lmm.map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* @author lmm E-mail:violet_mmhh@163.com
* @time 时间:2019年8月28日
* @function 功能:
*/
public class MapTest {
public static void main(String[] args) {
Map<Integer, Integer> m = new LinkedHashMap<Integer, Integer>();
m.put(2, 1);
m.put(1, 2);
m.put(3, 3);
m.put(4, 5);
m.put(5, 4);
System.out.println("第一种遍历方式:使用keySet、values");
for (Integer i : m.keySet()) {
System.out.println(i + " " + m.get(i));
}
for (Integer value : m.values()) {
System.out.println(value);
}
System.out.println("第二种遍历方式:使用Entry");
for (Map.Entry<Integer, Integer> mentry : m.entrySet()) {
System.out.println(mentry.getKey() + " " + mentry.getValue());
}
System.out.println("第三种遍历方式:使用迭代器");
Iterator<Map.Entry<Integer, Integer>> iterator = m.entrySet()
.iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, Integer> im = iterator.next();
System.out.println(im.getKey() + " " + im.getValue());
}
/*-------------------------------------对LinkedHashMap排序(key)--------------------------------------*/
List<Map.Entry<Integer, Integer>> ranklist = new ArrayList<Map.Entry<Integer, Integer>>(
m.entrySet());
Collections.sort(ranklist,
new Comparator<Map.Entry<Integer, Integer>>() {
public int compare(Entry<Integer, Integer> arg0,
Entry<Integer, Integer> arg1) {
return arg0.getKey() - arg1.getKey();
}
});
for (Map.Entry<Integer, Integer> sortentry : ranklist) {
System.out.println(sortentry.getKey() + " " + sortentry.getValue());
}
/*-------------------------------------对LinkedHashMap排序(value)--------------------------------------*/
Collections.sort(ranklist,
new Comparator<Map.Entry<Integer, Integer>>() {
public int compare(Entry<Integer, Integer> arg0,
Entry<Integer, Integer> arg1) {
return arg0.getValue() - arg1.getValue();
}
});
for (Map.Entry<Integer, Integer> sortentry : ranklist) {
System.out.println(sortentry.getKey() + " " + sortentry.getValue());
}
for (Map.Entry<Integer, Integer> sortentry : ranklist) {
System.out.println(sortentry.getKey() + " " + sortentry.getValue());
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/90ca5c09c45810b6f6c3c81ab7481e78.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4abdd0967de4d487be069c77032b4d1b.png)