大数据--Java异常和字符串
1 package com.atguigu.javase.exception; 2 3 import com.sun.org.apache.xpath.internal.operations.Div; 4 5 import java.io.IOException; 6 import java.sql.SQLException; 7 8 /** 9 * 异常 : 程序在运行时出现的非正常状况. 异常会导致程序崩溃 10 * 异常的分类 : 11 * 1) 按照程度来分 12 * 1) Error 严重错误. 13 * 2) Exception 一般问题. 14 * 2) 按照处理方式来分 15 * 1) 受检异常(checked) 在程序中必须接受检查和处理的异常. 如果不处理编译错误,也称为编译时异常 16 * Exception及其子类(RuntimeException及其子类除外) 17 * 2) 非受检异常(unchecked) 在程序中不是必须接受检查和处理的异常, 如果不处理不生编译错误, 但是运行时仍会出问题, 也称为运行时异常 18 * Error及其子类. 19 * RuntimeException及其子类. 20 * 21 * 异常必须处理 : 适用于所有异常, 包括Error 22 * 1) 捕获 23 * try { 24 * 可能抛出异常的语句; 25 * } catch (可能的异常类型1 引用) { 26 * 对异常处理. 27 * } catch (可能的异常类型2 引用) { 28 * 对异常处理. 29 * } ..... 30 * catch (Exception e) { 31 * 确保万无一失 32 * } finally { // final 修饰符, 修饰类, 方法, 变量 33 * 释放不在GC区中资源, 而是由OS管理的资源. 比如文件,网络等硬件资源. 34 * } 35 * 36 * try catch 37 * try catch finally 38 * try finally 39 * 40 * 2) 抛出 : 使用throws体现异常的抛出. 41 * 42 * 在方法声明中的throws 异常类型列表, 作用是警告调用者, 此方法有风险. 43 * 在方法体中使用throw语句, 在方法中真的抛出异常对象. 44 * 45 * 可以只有throws而没有throw 46 * 只要有throw必须要有throws 47 * 48 * throws 是警告 49 * throw 是玩真的. 50 * 51 * 方法覆盖条件中对于异常的描述 : 52 * 要求如下 : 53 * 1) 方法签名一致, 子类方法的返回值的对象类型可以小于等于父类方法返回的对象类型. 54 * 2) 子类方法的访问控制修饰符的范围要大于等于父类的. 55 * 3) 被覆盖方法不可以被private, static, final. 56 * 4) 子类方法抛出的受检异常范围要小于等于父类方法抛出的受检异常. 57 * 58 * 3) 先捕获, 再抛出. 59 * 先 60 * try { 61 * 可能抛出异常的语句; 62 * } catch(任意异常类型 引用) { 63 * throw new 自定义异常(引用); 64 * } 65 * 66 * 处理方式的选择 : 67 * 1) 入口方法尽量捕获. 68 * 2) 功能方法尽量抛出. 69 * 70 */ 71 72 // 自定义异常 : 1) 继承Exception 表明它是一个异常, 2) 提供2个构造器一个String,一个是Throwable 创建自定义异常对象方便 73 class DividedByZeroException extends Exception { // 这是受检异常, 必须要处理 74 75 public DividedByZeroException(String message) { 76 super(message); // 间接调用到父类构造, 完成私有的detailMessage的初始化. 77 } 78 public DividedByZeroException(Throwable cause) { // 对象关联专用 79 super(cause); 80 } 81 } 82 83 public class ExceptionTest { 84 85 public static int divide(int x, int y) throws DividedByZeroException { 86 try { 87 return x / y; 88 } catch (ArithmeticException e) { 89 throw new DividedByZeroException(e); // 包装 : 自定义异常对象内部包含一个其他异常对象. 90 } 91 } 92 93 public static void main(String[] args) { 94 try { 95 try { 96 System.out.println(divide(10, 2)); 97 System.out.println(divide(10, 0)); 98 } catch (DividedByZeroException e) { 99 System.out.println(e.getMessage()); 100 } 101 System.out.println(divide(20, 5)); 102 } catch (DividedByZeroException e) { 103 e.printStackTrace(); 104 } finally { 105 System.out.println("finally"); 106 } 107 } 108 } 109 110 class ExceptionTest3 { 111 // throws的作用是警告调用者, 调用此方法有风险!!! 112 public static int divide(int x, int y) throws DividedByZeroException { // throws和throw最好要一致, 显得诚实 113 if (y == 0) { 114 // 真的抛出一个异常对象, 当前方法就会提前弹栈返回结束, 并给调用者产生破坏. 115 throw new DividedByZeroException("除数不可以为0错误!!!!"); 116 } 117 return x / y; 118 } 119 120 //public static void main(String[] args) throws DividedByZeroException { main方法虽然可以抛出异常, 但是不要抛. 121 public static void main(String[] args) { 122 try { 123 System.out.println(divide(10, 2)); 124 System.out.println(divide(10, 0)); 125 } catch (DividedByZeroException e) { 126 System.out.println(e.getMessage()); 127 } 128 129 System.out.println("end"); 130 } 131 } 132 133 class ExceptionTest2 { 134 135 public static int divide(int x, int y) { 136 if (y == 0) { 137 RuntimeException re = new RuntimeException("除数不可以为0"); // throw和return效果一样, 都是让方法返回的 138 throw re; 139 // return 是和平返回, throw 带有破坏性的返回. 140 } 141 return x / y; 142 } 143 144 public static void main(String[] args) { 145 try { 146 System.out.println(divide(10, 2)); 147 System.out.println(divide(10, 0)); 148 } catch (RuntimeException e) { 149 System.out.println(e.getMessage()); 150 } 151 System.out.println("end..."); 152 } 153 } 154 155 156 class ExceptionTest1 { 157 158 public static void main(String[] args) { 159 System.out.println("main begin"); 160 161 boolean b = true; 162 if (b) { 163 //return; 164 } 165 166 try { 167 int n = Integer.parseInt(args[0]); 168 System.out.println(n); 169 return; 170 } catch (ArrayIndexOutOfBoundsException e) { 171 System.out.println(e); 172 } catch (NumberFormatException e) { 173 System.out.println(e.getMessage()); 174 } catch (Exception e) { 175 System.out.println("其他可能的异常 : " + e); 176 } finally { 177 // 无论前面try catch中发生什么, 我都要执行.... 178 System.out.println("finally"); 179 } 180 181 System.out.println("main end"); 182 } 183 }
1 import org.junit.Test; 2 3 public class WrapperTest { 4 5 @Test 6 public void test1() { 7 Integer i = new Integer(1); 8 Integer j = new Integer(1); 9 System.out.println(i == j); // false. 10 11 Integer m = 1; // Integer.valueOf(1) 12 Integer n = 1; 13 System.out.println(m == n); // true 14 15 Integer x = 128; 16 Integer y = 128; 17 System.out.println(x == y); // false 18 19 } 20 }
1 package com.atguigu.javase.string; 2 3 import org.junit.Test; 4 5 /** 6 * String : 内容不可改变的Unicode字符序列, 对象一旦创建, 内容不能改变. 7 * 对于字符串的内容的任何修改都会产生新对象. 8 * 字符串内部就是使用一个char[] 来保存所有字符的. 9 * 10 * Customer cu = new Cutomer(); 11 * cu.setAge(20); // 内容可以改变的对象 12 * cu.setAge(50); 13 * 14 * 0 2 10 15 21 27 32 34 15 * String string = " abcAQQY 我喜欢你,你喜欢我吗?我不喜欢你 zzz123 "; 16 * 17 * public int length(). string.length() => 35 获取字符串长度(字符数) 18 * public char charAt(int index) 获取指定下标位置处的字符 string.charAt(12) => '欢', string.charAt(18) => '我'; 19 * public char[] toCharArray() 获取内部的char[] 20 * char result[] = new char[value.length]; // 创建一个新数组, 容量和内部的value一样大. 21 * 22 * for (int i = 0; i < value.length; i++) { 23 * result[i] = value[i]; 24 * } 25 * 26 * System.arraycopy(value, 0, result, 0, value.length); 27 * // 第一个参数是源数组对象, 第二个参数是源数组的开始下标, 第三个参数是目标数组对象, 第四个参数是目标数组的开始下标 28 * // 第五个参数是要复制的元素个数. 29 * public boolean equals(Object anObject) 30 * public int compareTo(String anotherString) 31 * public int indexOf(String s) 32 * public int indexOf(String s ,int startpoint) 33 * public int lastIndexOf(String s) 34 * public int lastIndexOf(String s ,int startpoint) 35 * public boolean startsWith(String prefix) 36 * public boolean endsWith(String suffix) 37 * public String substring(int start,int end) 38 * public String substring(int startpoint) 39 * public String replace(char oldChar,char newChar) 40 * public String replaceAll(String old,String new) 41 * public String trim() 42 * public String concat(String str) 43 * public String toUpperCase() 44 * public String toLowerCase() 45 * public String[] split(String regex) 46 */ 47 public class StringTest { 48 49 // 练习 : 把字符串反转一下 50 @Test 51 public void exer13() { 52 String string = " abcAQQY 我喜欢你,你喜欢我吗?我不喜欢你 zzz123 "; 53 char[] arr = string.toCharArray(); 54 for (int i = 0; i < arr.length / 2; i++) { 55 // 首尾交换, i和length - 1 - i 56 char tmp = arr[i]; 57 arr[i] = arr[arr.length - 1 - i]; 58 arr[arr.length - 1 - i] = tmp; 59 } 60 String string2 = new String(arr); 61 System.out.println(string2); 62 } 63 64 @Test 65 public void exer12() { 66 String string = " abcAQQY 我喜欢你,你喜欢我吗?我不喜欢你 zzz123 "; 67 String string2 = ""; 68 for (int i = 0; i < string.length(); i++) { 69 char ch = string.charAt(i); 70 string2 = ch + string2; 71 } 72 System.out.println(string2); 73 } 74 75 @Test 76 public void exer1() { 77 String string = " abcAQQY 我喜欢你,你喜欢我吗?我不喜欢你 zzz123 "; 78 String string2 = ""; 79 for (int i = string.length() - 1; i >= 0 ; i--) { 80 char ch = string.charAt(i); 81 string2 += ch; 82 } 83 System.out.println(string2); 84 } 85 86 @Test 87 public void test5() { 88 String string = " abcAQQY 我喜欢你,你喜欢我吗?我不喜欢你 zzz123 "; 89 System.out.println(string.length()); 90 System.out.println(string.charAt(13)); 91 System.out.println(string.charAt(0)); 92 System.out.println(string.charAt(32)); 93 //System.out.println(string.charAt(500)); 94 System.out.println("**************************"); 95 for (int i = 0; i < string.length(); i++) { 96 char ch = string.charAt(i); 97 System.out.println(ch); 98 } 99 System.out.println("***************************"); 100 char[] chars = string.toCharArray(); 101 for (int i = 0; i < chars.length; i++) { 102 System.out.println(chars[i]); 103 } 104 } 105 106 @Test 107 public void test4() { 108 char[] arr = {'a', '1', 'q', '我', '3', '好', 'o'}; 109 String s1 = new String(arr); // 制作一个副本 110 arr[1] = '大'; 111 System.out.println(s1); 112 113 String s2 = new String(arr, 2, 3);// 第2个参数是开始下标, 第3个参数是长度 114 System.out.println(s2); 115 String s3 = new String(arr, 0, arr.length); 116 System.out.println(s3); 117 } 118 119 @Test 120 public void test3() { 121 String s1 = "atguigu"; 122 String s2 = "java"; 123 String s4 = "java"; 124 String s3 = new String("java"); 125 System.out.println(s2 == s3); 126 System.out.println(s2 == s4); 127 System.out.println(s2.equals(s3)); 128 129 String s5 = "atguigujava"; 130 // 字符串拼接时, 如果有变量参与, 结果一定是新字符串对象, 并且在堆. 131 String s6 = (s1 + s2).intern(); // intern() 作用是把字符串塞入常量区中, 如果常量区中已经有此对象了,则返回常量对象的地址. 132 System.out.println(s5 == s6); // true 133 System.out.println(s5.equals(s6)); 134 135 } 136 137 @Test 138 public void test2() { 139 String s1 = new String(); 140 String s2 = ""; 141 String s3 = null; 142 143 System.out.println(s1 == s2); 144 System.out.println(s1.equals(s2)); 145 146 String s4 = new String("qqq"); 147 148 149 } 150 151 @Test 152 public void test1() { 153 String s1 = "abc"; 154 s1 += "yyy"; 155 System.out.println(s1); 156 } 157 }
1 package com.atguigu.javase.test; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 /** 9 * 枚举 : 对象可数的类型 10 */ 11 enum MyEnum { 12 ONE, TWO, THREE 13 } 14 15 /** 16 * 注解 : 特殊的注释, 不参与程序的执行. 17 */ 18 @Target(ElementType.TYPE) 19 @Retention(RetentionPolicy.RUNTIME) 20 @interface MyAnn { 21 int id() default 2; 22 } 23 24 @MyAnn(id = 1) 25 public class EnumTest { 26 27 int n; 28 29 public static void main(String[] args) { 30 MyEnum e = MyEnum.ONE; 31 MyEnum three = MyEnum.valueOf("THREE"); 32 MyEnum value = MyEnum.values()[1]; 33 System.out.println(value); 34 } 35 }
代码演示:
1 package com.atguigu.javase.test; 2 3 class A { 4 int n; 5 static { 6 System.out.println("A static"); // 1 7 } 8 { 9 System.out.println("A {}"); // 2 10 } 11 A() { 12 super(); 13 System.out.println("A()"); // 3 14 } 15 } 16 class B extends A { 17 int n; 18 static { 19 System.out.println("B static"); // 4 20 } 21 { 22 System.out.println("B {}"); // 5 23 } 24 B() { 25 super(); 26 System.out.println("B()"); // 6 27 } 28 } 29 class C extends B { 30 int n; 31 static { 32 System.out.println("C static"); // 7 33 } 34 { 35 System.out.println("C {}"); // 8 36 } 37 C() { 38 super(); 39 System.out.println("C()"); // 9 40 } 41 } 42 43 public class ObjectTest { 44 45 public static void main(String[] args) { 46 new C(); 47 // 先静后动, 先父后子 48 // 加载A, 再加载B, 再加载C. 49 } 50 51 }

浙公网安备 33010602011771号