异常处理
① Error(错误)
Error(错误)
一般指比较严重的问题,不做针对性处理,无法挽救;
OutOfMemoryError 内存溢出
② Exception (异常)
Exception(异常)
checked Exception(受检异常)
强制在编译期对可能出现的问题做一个预处理
unchecked Exception(非受检异常)
运行之后才能触发
try catch (代码异常捕获)
try(尝试执行的代码)catch(捕获到的异常对象)(对异常的处理方式)
finally(不论是否有异常,都将执行到的代码)
多异常处理,可以1个try,多个catch;catch里面范围大的放后面
Java中常见的异常 :
java.lang.NullPointerException SecturityException
IOException java.lang.classnotfoundexception
java.lang.arrayindexoutofboundsexception
java.lang.illegalargumentexception FileNotFoundException
NoSuchMethodException java.lang.OutOfMemoryError
java.lang.ClassNotFoundException
java.lang.IllegalArgumentException
工具类
① Array(排序)
array 的排序默认是升序排序
int [] num = {1,2,3,4,5,6}; //排序 Arrays.sort(num); //打印 System.out.println(Arrays.toString(num)); //复制 Arrays.copyOf(num,4); //填充 Arrays.fill(num,666); //比较 Arrays.equals(xx,xx);
② StringBuffer/StringBuilder
StringBuffer/StringBuilder 的应用只在于字符串大量的拼接或者修改操作时使用
StringBuffer 速度慢 安全
StringBuilder 速度快 不安全
public class Demo { public static void main(String[] args) { StringBuffer stb=new StringBuffer("hello"); String str=stb.toString(); System.out.println(str); //拼接 stb.append('A'); stb.append(12); stb.append("xx"); System.out.println(stb); //删除 stb.delete(1, 4); //(删头不删尾) System.out.println(stb); //插入 stb.insert(3, 999); System.out.println(stb); //替换 stb.replace(1, 3,"dd"); //翻转 stb.reverse(); //截取 stb.substring(2, 4); } }
③ Random(随机)
public class Demo2 { public static void main(String[] args) { Random ran=new Random(); for(int i=0;i<20;i++){ System.out.println(ran.nextBoolean()); System.out.println(ran.nextDouble()); System.out.println(ran.nextInt()); //0-9,不包括10 System.out.println(ran.nextInt(10)); System.out.println("-----------------------"); } //Math.Random()函数 //调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取值范围是[0.0,1.0)的左闭右开区间, //返回值是一个伪随机选择的数,在该范围内(近似)均匀分布。 for(int i=0,i<10,i++){ int num=(int)(Math.random()*3);[0,3) System.out.println(num); } } }
④ Math
public class Demo { public static void main(String[]args) { //绝对值 System.out.println(Math.abs(-4)); // 4 //最大值 System.out.println(Math.max(5,94)); // 94 //平方根 System.out.println(Math.sqrt(9)); // 3 //立方根 System.out.println(Math.cbrt(27)); // 3 //向上取整 System.out.println(Math.ceil(-6.3)); // -6 //向下取整 System.out.println(Math.floor(-6.8)); //-7 //四舍五入 System.out.println(Math.round(-6.4)); //-6 //π System.out.println(Math.PI); //e System.out.println(Math.E); System.out.println(Math.pow(3, 2)); //3的平方 }
⑤ Date
public class Demo { public static void main(String[]args) throws ParseException { Date date = new Date(); System.out.println(date); date.setTime(0); System.out.println(date); // 修改日期的显示格式2015-10-25 17:20:21 Date date1 = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String str = df.format(date1); System.out.println(str); // 从一个字符串中解析出一个Date对象 String time = "1999-12-21 20:62:10"; SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d = sf.parse(time); String s1 = df.format(d); System.out.println(s1); } }