Loading

1-JavaSE快速入门-1

Base

  • HelloWorld
package com.lotuslaw.base;

/**
 * @author: lotuslaw
 * @create: 2021-05-20-10:05 下午
 * @package: PACKAGE_NAME
 * @description:
 */
public class Demo1_HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
  • 八大数据类型
package com.lotuslaw.base;

/**
 * @author: lotuslaw
 * @create: 2021-05-20-10:11 下午
 * @package: PACKAGE_NAME
 * @description:
 */
public class Demo2_8_base_data_type {
    public static void main(String[] args){
        // 八大基本数据类型
        // 整数
        int num1 = 10;  // 最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 = 40L;  // Long类型要在数字后面加个L

        // 小数:浮点数
        float num5 = 1.22F;  // Float类型要在数字后面加个F
        double num6 = 3.2132e233;

        // 字符,注意单引号
        char name = 'L';
        // 字符串,String,不是关键字,类
        // String name = "lotuslaw"

        // 布尔值
        boolean flag = true;
        // boolean flag = false;
        System.out.println(num5);
    }
}
  • 进制转换与类型拓展
package com.lotuslaw.base;

/**
 * @author: lotuslaw
 * @create: 2021-05-20-10:25 下午
 * @package: PACKAGE_NAME
 * @description:
 */
public class Demo3_conversion_num_system {
    public static void main(String[] args) {
        // 整数拓展: 进制:   二进制0b     十进制      八进制0      十六进制0x
        int i = 10;
        int i2 = 0b10;  // 二进制
        int i3 = 010;  // 八进制
        int i4 = 0x10;  // 十六进制0-9 A-F

        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println(i4);

        /*
        * 浮点数拓展:银行业务怎么表示钱?
        * BigDecimal 数学工具类
        * float:有限  离散  舍入误差  大约  接近但不等于
        * double
        * 最好避免完全使用浮点数进行比较
        * */
        float f = 0.1f;  // 0.1
        double d = 1.0/10;  // 0.1
        System.out.println(f==d);  // false

        float d1 = 231311313131311131f;
        float d2 = d1 + 1;
        System.out.println(d1==d2);

        /*
        * 字符串拓展:
        * 所有的字符本质还是数字
        * 编码:Unicode 表: 2字节  0-65536
        * U0000-UFFFF
        * */
        char c1 = 'a';
        char c2 = '中';
        char c3 = '\u0061';  // a
        System.out.println(c1);
        System.out.println((int)c1);  // 强制转换
        System.out.println(c2);
        System.out.println((int)c2);
        System.out.println(c3);

        /*
        * 转义字符
        * \t 制表符
        * \n 换行符
        * */
        System.out.println("Hello\nWorld");
        // 需要从对象、内存分析
        String sa = new String("Hello World");
        String sb = new String("Hello World");
        System.out.println(sa==sb);  // true
        String sc = "Hello World";
        String sd = "Hello World";
        System.out.println(sc==sd);  // false

        /*
        * 布尔值扩展:
        * */
        boolean flag = true;
        // less is more
        if (flag){
            System.out.println("ok");
        }
    }
}
  • 类型转换
package com.lotuslaw.base;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-3:38 下午
 * @package: PACKAGE_NAME
 * @description:
 */
public class Demo4_type_conversion {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte) i;
        double c = i;

        // 强制转换   (类型)变量名   高-->低
        // 自动转换   低-->高

        System.out.println(i);  // 128
        System.out.println(b);  // -128,内存溢出
        System.out.println(c);  // 128.0

        /*
        *注意点:
        * 1、不能对布尔值进行转换
        * 2、不能把对象类型转换为不相干的类型
        * 3、在把高容量转换到低容量的时候,强制转换
        * 4、转换的时候可能存在内存溢出或者精度问题
        * */
        System.out.println("======================================>");
        System.out.println((int)23.7);  // 23
        System.out.println((int)-23.89f);  // -23
        System.out.println("======================================>");
        char x = 'a';
        int d = x + 1;
        System.out.println(d);  // 98
        System.out.println((char)d);  // b

    }
}
  • 大数溢出
package com.lotuslaw.base;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-3:48 下午
 * @package: PACKAGE_NAME
 * @description:
 */
public class Demo5_large_num_overflow {
    public static void main(String[] args) {
        // JDK新特性,数字之间可以用下划线分割
        int money = 10_0000_0000;
        System.out.println(money);
        int years = 20;
        int total = years * money;
        long total2 = years * money;  // 默认是int,转换之前已经出问题
        long total3 = years * ((long)money);
        System.out.println(total);  // -1474836480
        System.out.println(total3);  // 20000000000
    }
}
  • 变量
package com.lotuslaw.base;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-3:57 下午
 * @package: PACKAGE_NAME
 * @description:
 */
public class Demo6_variable {
    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        int c = 3;
        String name = "lotuslaw";
        char x = 'X';
        double pi = 3.14;
    }
}
  • 作用域
package com.lotuslaw.base;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-4:05 下午
 * @package: PACKAGE_NAME
 * @description:
 */
public class Demo7_action_scope {

    // 属性:变量
    // 实例变量:从属于对象,如果不进行初始化,会初始化为这个类型的默认值 0 0.0
    // 布尔值:默认是false
    // 除了基本类型,其他的默认值都是null
    String name;
    int age;
    // 类变量  static
    static double salary = 2500;

    // main方法
    public static void main(String[] args) {
        // 局部变量:必须声明和初始化值
        int i = 10;
        System.out.println(i);
        // 变量类型 变量名字 = new com.lotuslaw.base.Demo7_action_scope()
        Demo7_action_scope demo7_action_scope = new Demo7_action_scope();
        System.out.println(demo7_action_scope.age);  // 0
        System.out.println(demo7_action_scope.name);  // null

        // 类变量  static
        System.out.println(salary);

    }
    // 其他方法
    public static void add(){

    }
}
  • 常量
package com.lotuslaw.base;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-4:20 下午
 * @package: PACKAGE_NAME
 * @description:
 */
public class Demo8_constant {
    // 修饰符不存在先后顺序
    static final double PI = 3.14;
    public static void main(String[] args) {
        System.out.println(PI);
    }
}
  • JavaDoc
package com.lotuslaw.base;

/**
 * @author: lotuslaw
 * @version: 1.0
 * @since: 1.8
 * @create: 2021-05-30-8:23 下午
 * @package: com.lotuslaw.base
 * @description:
 */
public class Demo9_Doc {
    String name;

    /**
     * @author lotuslaw
     * @param name
     * @return
     * @throws Exception
     */
    // javadoc -encoding UTF-8 -charset UTF-8 Demo9_Doc.java
    // Tools->Generate JavaDoc     Locale:zh_CN    Other command line arguments   -encoding UTF-8 -charset UTF-8 -windowtitle
    public String test(String name) throws Exception{
        return name;
    }
}

Scanner

  • hasnext
package com.lotuslaw.scanner;

import java.util.Scanner;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-8:49 下午
 * @package: com.lotuslaw.scanner
 * @description:
 */
public class Demo1_hasnext {
    public static void main(String[] args) {
        // 创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收:");
        // 判断用户有没有输入字符串
        if (scanner.hasNext()){
            String str = scanner.next();
            System.out.println("输入的内容为:" + str);
        }
        // 凡是属于IO流的类,用完一定要关掉,节省资源
        scanner.close();
    }
}
  • hasnextLine
package com.lotuslaw.scanner;

import java.util.Scanner;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-8:58 下午
 * @package: com.lotuslaw.scanner
 * @description:
 */
public class Demo2_hasnextLine {
    public static void main(String[] args) {
        // hasnext:以空格为结束符
        // hasnextLine:以回车为结束符
        System.out.println("请输入命令:");
        Scanner scanner = new Scanner(System.in);
        String src = scanner.nextLine();
        System.out.println("输入的内容为:" + src);
        scanner.close();
    }
}
  • hasnextOther
package com.lotuslaw.scanner;

import java.util.Scanner;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-9:08 下午
 * @package: com.lotuslaw.scanner
 * @description:
 */
public class Demo3_hasnextOther {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i = 0;
        float f = 0.0f;
        System.out.println("请输入整数");
        if (scanner.hasNextInt()){
            i = scanner.nextInt();
            System.out.println("输入的整数为:" + i);
        }else {
            System.out.println("输入的不是整数");
        }
        System.out.println("请输入小数");
        if (scanner.hasNextFloat()){
            f = scanner.nextFloat();
            System.out.println("输入的小数为:" + f);
        }
        scanner.close();
    }
}
  • example
package com.lotuslaw.scanner;

import java.util.Scanner;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-9:17 下午
 * @package: com.lotuslaw.scanner
 * @description:
 */
public class Demo4_example {
    public static void main(String[] args) {
        int m = 0;
        double sum = 0.0D;
        System.out.println("请输入数字");
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextDouble()){
            double x = scanner.nextDouble();
            m++;
            sum += x;
            System.out.println("你输入了第" + m + "个数字,当前结果为sum=" + sum);
        }
        System.out.println("最终结果为" + sum/m);
        scanner.close();
    }
}

Operator

  • 算数运算符
package com.lotuslaw.operator;
// 一般利用公司域名倒置作为包名

/**
 * @author: lotuslaw
 * @create: 2021-05-30-7:11 下午
 * @package: com.lotuslaw.operator
 * @description:
 */
public class Demo1_arithmetic_operator {
    public static void main(String[] args) {
        // 二元运算符
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/b);  // 0
        System.out.println((double) a/b);  // 0.5

        long e = 12112341432545453L;
        int f = 123;
        short g = 10;
        byte h = 11;
        System.out.println(e+f+g+h);  // Long
        System.out.println(f+g+h);  // int
        System.out.println(g+h);  // int
    }
}
  • 关系运算符
package com.lotuslaw.operator;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-7:17 下午
 * @package: com.lotuslaw.operator
 * @description:
 */
public class Demo2_relational_operator {
    public static void main(String[] args) {
        // 关系运算符返回的结果:  正确  错误  布尔值
        int a = 10;
        int b = 20;
        int c = 21;
        System.out.println(a==b);
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a!=b);
        System.out.println(c%a);  // 1,取余,模运算
    }
}
  • 自增自减
package com.lotuslaw.operator;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-7:20 下午
 * @package: com.lotuslaw.operator
 * @description:
 */
public class Demo3_auto_increment_reduction {
    public static void main(String[] args) {
        // ++ --
        int a = 3;
        System.out.println(a);
        int b = a++;  // 先赋值,再自增
        int c = ++a;  // 先自增,再赋值
        System.out.println(a);

        System.out.println(b);
        System.out.println(c);
        // 幂运算,很多运算,我们会使用一些工具类去操作
        double d = Math.pow(2, 3);
        System.out.println(d);
    }
}
  • 其他运算符
package com.lotuslaw.operator;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-7:48 下午
 * @package: com.lotuslaw.operator
 * @description:
 */
public class Demo4_other_operator {
    public static void main(String[] args) {
        // 与and 或or 非(取反)
        boolean a = true;
        boolean b = false;
        System.out.println(a&&b);
        System.out.println(a||b);
        System.out.println(!(a&&b));
        // 短路运算
        /*
        * 位运算:效率高
        * & | ^ (抑或) ! >> (相当于*2) << (/2)
        * */
        // += -= *= /=
        int c = 10;
        int d = 20;
        c += d;
        System.out.println(c);
        // 字符串连接 +
        System.out.println(c+d+"");
        System.out.println(""+c+d);
        // 三元运算符  ?
        int score = 80;
        String type = score < 60 ? "不及格":"及格";
        System.out.println(type);
    }
}

Structure

  • 顺序结构
package com.lotuslaw.structure;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-9:25 下午
 * @package: com.lotuslaw.structure
 * @description:
 */
public class Demo1_sequential_structure {
    public static void main(String[] args) {
        System.out.println("HelloWorld1");
        System.out.println("HelloWorld2");
        System.out.println("HelloWorld3");
        System.out.println("HelloWorld4");
        System.out.println("HelloWorld5");
    }
}
  • 选择结构if-else
package com.lotuslaw.structure;

import java.util.Scanner;

/**
 * @author: lotuslaw
 * @create: 2021-05-30-9:30 下午
 * @package: com.lotuslaw.structure
 * @description:
 */
public class Demo2_selective_structure {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        /*
        System.out.println("请输入内容");
        String s = scanner.nextLine();
        // equals:判断字符串是否相等
        if (s.equals("Hello")){
            System.out.println(s);
        }
        System.out.println("End");
         */
        /*
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        if (score >= 60){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }
         */
        /*
        System.out.println("请输入成绩");
        int score = scanner.nextInt();
        if (score == 100){
            System.out.println("恭喜满分");
        }else if (score >= 90){
            System.out.println("优秀");
        }else if (score >= 60 && score <= 88){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }
         */
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        if (score > 2){
            if (score > 4){
                System.out.println("ok");
            }else {
                System.out.println("not ok");
            }
        }
        scanner.close();
    }
}
  • 选择结构switch
package com.lotuslaw.structure;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-2:56 下午
 * @package: com.lotuslaw.structure
 * @description:
 */
public class Demo3_selective_structure_switch {
    public static void main(String[] args) {
        /*
        char grade = 'E';
        // 无break case 穿透
        switch (grade){
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
                break;
            case 'D':
                System.out.println("挂科");
                break;
            default:
                System.out.println("other");
        }
        */
        String name = "lotuslaw";
        // JDK7的新特性,支持字符串
        // 字符的本质还是数字

        // 反编译  java---class(字节码)---反编译(IDEA)
        switch (name){
            case "hehe":
                System.out.println("hehe");
                break;
            case "lotuslaw":
                System.out.println("lotuslaw");
                break;
            default:
                System.out.println("woca");
        }
    }
}
  • 循环结构while
package com.lotuslaw.structure;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-3:15 下午
 * @package: com.lotuslaw.structure
 * @description:
 */
public class Demo4_loop_structure_while {
    public static void main(String[] args) {
        // 输出1-100
        int i = 0;
        while (i < 100){
            i ++;
            System.out.println(i);
        }
        int j = 0;
        int sum = 0;
        while (j <= 100){
            sum += j;
            j++;
        }
        System.out.println(sum);
    }
}
  • 循环结构do-while
package com.lotuslaw.structure;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-3:28 下午
 * @package: com.lotuslaw.structure
 * @description:
 */
public class Demo5_loop_structure_do_while {
    public static void main(String[] args) {
        // do-while至少保证循环被执行一次
        int i = 0;
        int sum = 0;
        do {
            i++;
            sum += i;
        }while (i<100);
        System.out.println(sum);
    }
}
  • 循环结构for
package com.lotuslaw.structure;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-3:33 下午
 * @package: com.lotuslaw.structure
 * @description:
 */
public class Demo5_loop_structure_for {
    public static void main(String[] args) {
        int sum = 0;
        // 100.for
        for (int i=0;i<=100;i++){
            sum += i;
        }
        System.out.println(sum);
        // 死循环
        /*
        for (; ; ){
            // 表达式
        }
         */
        int oddSum = 0;
        int evenSum = 0;
        for (int i = 0; i < 100; i++) {
            if (i % 2 != 0){
                oddSum += i;
            }else {
                evenSum += i;
            }
        }
        System.out.println("oddSum:"+oddSum);
        System.out.println("evendSum:"+evenSum);
    }
}
  • 九九乘法表
package com.lotuslaw.structure;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-3:49 下午
 * @package: com.lotuslaw.structure
 * @description:
 */
public class Demo6_loop_structure_9_9 {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+j*i+"\t");
            }
            System.out.println();
        }
    }
}
  • 循环结构增强for循环
package com.lotuslaw.structure;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-3:59 下午
 * @package: com.lotuslaw.structure
 * @description:
 */
public class Demo7_loop_structure_enhancement_for {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};  // 定义了一个数组
        // 遍历数组的元素
        for (int x: numbers){
            System.out.println(x);
        }
        for (int i = 0; i < numbers.length; i++){
            System.out.println(numbers[i]);
        }
    }
}
  • 循环结构break_continue
package com.lotuslaw.structure;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-4:04 下午
 * @package: com.lotuslaw.structure
 * @description:
 */
public class Demo8_loop_structure_break_continue {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            System.out.println(i);
            if (i == 30){
                break;
            }
        }
        for (int i = 0; i < 100; i++) {
            if (i % 10 == 0){
                continue;
            }
            System.out.println(i);
        }
    }
}
  • 循环结构标签continue
package com.lotuslaw.structure;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-4:13 下午
 * @package: com.lotuslaw.structure
 * @description:
 */
public class Demo9_loop_label_goto {
    public static void main(String[] args) {
        outer:for (int i = 101; i <= 150; i++){
            for (int j = 2; j < i / 2; j++){
                if (i % j == 0){
                    continue outer;
                }
            }
            System.out.print(i + "\t");
        }
    }
}
  • 打印三角形
package com.lotuslaw.structure;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-4:22 下午
 * @package: com.lotuslaw.structure
 * @description:
 */
public class Demo10_loop_structure_example {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--){
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++){
                System.out.print("*");
            }
            for (int j = 1; j < i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Array

  • array
package com.lotuslaw.array;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-10:17 上午
 * @package: com.lotuslaw.array
 * @description:
 */
public class Demo1_array {
    public static void main(String[] args) {
        // 数组:相同数据类型的有序集合
        int[] num1;  // 声明一个数组
        // int num2[];  // 定义-C,C++风格
        num1 = new int[10];  // 创建一个数组,分配内存空间
        // int nums[] = new int[10];
        for (int i = 0; i < 10; i++) {
            num1[i] = i;
        }
        System.out.println(num1[3]);
    }
}
  • 初始化
package com.lotuslaw.array;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-10:44 上午
 * @package: com.lotuslaw.array
 * @description:
 */
public class Demo2_array_initialization {
    public static void main(String[] args) {
        // 静态初始化:创建+赋值
        int[] a = {1, 2, 3, 4, 5, 6, 7};
        System.out.println(a[2]);
        // 动态初始化:包含默认初始化
        int[] b = new int[10];
        b[0] = 10;
        System.out.println(b[0]);
    }
}
  • 增强for循环
package com.lotuslaw.array;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-11:56 上午
 * @package: com.lotuslaw.array
 * @description:
 */
public class Demo3_array_for_Each {
    public static void main(String[] args) {
        int[] arrays = {1, 2, 3, 4, 5};
        // 增强for循环
        for (int array : arrays) {
            System.out.println(array);
        }
        printArray(arrays);
        System.out.println();
        printArray(reverse(arrays));
    }
    public static void printArray(int[] arrays){
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i] + " ");
        }
    }
    public static int[] reverse(int[] arrays){
        int[] result = new int[arrays.length];
        for (int i = 0, j=result.length - 1; i < arrays.length; i++, j--) {
            result[j] = arrays[i];
        }
        return result;
    }
}
  • 多维数组
package com.lotuslaw.array;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-12:21 下午
 * @package: com.lotuslaw.array
 * @description:
 */
public class Demo4_multidimensional_array {
    public static void main(String[] args) {
        int[][] array = {{1, 2}, {2, 3}, {3, 4}};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.println(array[i][j]);
            }
        }
    }
}
  • 数组操作
package com.lotuslaw.array;

import java.util.Arrays;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-12:30 下午
 * @package: com.lotuslaw.array
 * @description:
 */
public class Demo5_array_Arrays {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 3, 2, 34, 21};
        System.out.println(a);
        // 打印数组元素
        System.out.println(Arrays.toString(a));
        // 数组排序
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
        // 数组填充
        // Arrays.fill(a, 0);
        Arrays.fill(a, 2, 4, 0);  // 左闭右开
        System.out.println(Arrays.toString(a));
    }
}
  • 冒泡排序
package com.lotuslaw.array;

import java.util.Arrays;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-1:04 下午
 * @package: com.lotuslaw.array
 * @description:
 */
public class Demo6_bubble_sort {
    public static void main(String[] args) {
        bubbleSort(new int[] {1, 2, 4, 5, 3, 4, 5, 2});
    }
    public static void bubbleSort(int[] array){
        int temp = 0;
        boolean flag = false;
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] >= array[j+1]){
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    flag = true;
                }
            }
            if (!flag){
                System.out.println(Arrays.toString(array));
                return;
            }
        }
        System.out.println(Arrays.toString(array));
    }
}
  • 稀疏数组
package com.lotuslaw.array;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-1:49 下午
 * @package: com.lotuslaw.array
 * @description:
 */
public class Demo7_sparse_array {
    public static void main(String[] args) {
        int[][] array1 = new int[11][11];
        array1[1][2] = 1;
        array1[2][3] = 2;
        System.out.println("输出数组");
        for (int[] ints : array1) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
        int sum = 0;
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                if (array1[i][j] != 0){
                    sum++;
                }
            }
        }
        System.out.println("有效值个数:" + sum);
        int[][] array2 = new int[sum+1][3];
        int count = 0;
        array2[0][0] = array1.length;
        array2[0][1] = array1[0].length;
        array2[0][2] = sum;
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                if (array1[i][j] != 0){
                    count++;
                    array2[count][0] = i;
                    array2[count][1] = j;
                    array2[count][2] = array1[i][j];
                }
            }

        }
        // 输出稀疏数组
        System.out.println("输出稀疏数组");
        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i][0] + "\t" + array2[i][1] + "\t" + array2[i][2] + "\n");
        }
        // 还原
        int[][] array3 = new int[array2[0][0]][array2[0][1]];
        for (int i = 1; i < array2.length; i++) {
            array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }
        // 打印
        for (int[] ints : array3) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
    }
}

Method

  • method
package com.lotuslaw.method;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-8:58 下午
 * @package: com.lotuslaw.method
 * @description:
 */
public class Demo1_method {
    // main方法,尽量保持干净整洁
    public static void main(String[] args) {
        int sum = add(1, 2);
        System.out.println(sum);
        test();
    }
    public static int add(int a, int b){
        return a + b;
    }
    public static void test(){
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--){
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++){
                System.out.print("*");
            }
            for (int j = 1; j < i; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
  • 方法重载
package com.lotuslaw.method;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-9:34 下午
 * @package: com.lotuslaw.method
 * @description:
 */
public class Demo2_method {
    public static void main(String[] args) {
        int max = max(10, 20);
        System.out.println(max);
        double max2 = max(10.0, 20.0);
        System.out.println(max2);
    }
    public static int max(int num1, int num2){
        int result = -1;
        if (num1 == num2){
            System.out.println("num1==num2");
            return -1;
        }
        if (num1 > num2){
            result = num1;
        }else {
            result = num2;
        }
        return result;
    }
    // 方法的重载,方法名相同,参数列表不同
    public static double max(double num1, double num2){
        double result = -1;
        if (num1 == num2){
            System.out.println("num1==num2");
            return -1;
        }
        if (num1 > num2){
            result = num1;
        }else {
            result = num2;
        }
        return result;
    }
}
  • 命令行传参
package com.lotuslaw.method;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-9:55 下午
 * @package: com.lotuslaw.method
 * @description:
 */
public class Demo3_command_line_args_passing {
    /*
    * cd method 目录下
    * javac Demo3_command_line_args_passing.java
    * cd src 目录下
    * java com.lotuslaw.method.Demo3_command_line_args_passing this is lotuslaw
    * */
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++){
            System.out.println("args[" + i + "]:" + args[i]);
        }
    }
}
  • 可变参数
package com.lotuslaw.method;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-10:06 下午
 * @package: com.lotuslaw.method
 * @description:
 */
public class Demo4_method_variable_parameter {
    public static void main(String[] args) {
        Demo4_method_variable_parameter demo4_method_variable_parameter = new Demo4_method_variable_parameter();
        demo4_method_variable_parameter.test(1, 2, 3, 4, 5);
    }
    public void test(int... i){
        System.out.println(i[0]);
        System.out.println(i[1]);
        System.out.println(i[2]);
        System.out.println(i[3]);
        System.out.println(i[4]);
    }
}
  • 递归
package com.lotuslaw.method;

/**
 * @author: lotuslaw
 * @create: 2021-06-05-10:13 下午
 * @package: com.lotuslaw.method
 * @description:
 */
public class Demo5_method_recursion {
    public static void main(String[] args) {
        int result = f(5);
        System.out.println(result);
    }
    /*
    * 递归结构包括两个部分:
    * 递归头:什么时候不调用自身方法,如果没有头,将陷入死循环;
    * 递归体:什么时候需要调用自身方法。
    * */
    public static int f(int n){
        if (n == 1){
            return 1;
        }else {
            return n * f(n - 1);
        }
    }
}

Oop

package com.lotuslaw.oop.base;

import java.io.IOException;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-2:20 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
// Demo1_oop_method类
public class Demo1_oop_method {
    // main方法
    public static void main(String[] args) {
        /*
        * 方法:
        * 修饰符 返回值类型 方法名(参数列表:参数类型 参数名){
        *   方法体
        *   return 返回值
        * }
        * */
    }
    public String sayHello(){
        return "HelloWorld";
    }
    public int max(int a, int b){
        return a > b ? a : b;
    }
    public void readFile(String file) throws IOException{

    }
}
  • 方法调用
package com.lotuslaw.oop.base;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-2:34 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
public class Demo2_oop_method_calling {
    public static void main(String[] args) {
        // 实例化类
        Student student = new Student();
        student.say();
    }
    // 和类一起加载的
    public static void a(){
        // b();  // 会报错
    }
    // 类实例化后才存在
    public void b(){

    }
}
  • 实参与形参
package com.lotuslaw.oop.base;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-2:44 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
public class Demo3_oop_parameter {
    public static void main(String[] args) {
        // 实参与形参的类型要对应
        int add = add(1, 2);
        System.out.println(add);
    }
    public static int add(int a, int b){
        return a + b;
    }
}
  • 值传递
package com.lotuslaw.oop.base;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-2:51 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
// JAVA是值传递
public class Demo4_oop_value_propagation {
    public static void main(String[] args) {
        int a = 1;
        Demo4_oop_value_propagation.change(a);
        System.out.println(a);
    }
    public static void change(int a){
        a = 10;
    }
}
  • 引用传递
package com.lotuslaw.oop.base;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-2:53 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
// 引用传递:对象,本质还是值传递
public class Demo5_oop_reference_propagation {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);
        Demo5_oop_reference_propagation.change(person);  // 传递的是对象,对象的属性可以改变
        System.out.println(person.name);
    }
    public static void change(Person person){
        person.name = "lotuslaw";
    }
}
class Person{
    String name;  // null
}
  • 类与对象的创建
package com.lotuslaw.oop.advanced;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-5:15 下午
 * @package: com.lotuslaw.oop.advanced
 * @description:
 */
public class Student {
    // 属性:字段
    String name;
    int age;

    // 方法
    public void study(){
        System.out.println(this.name + "在学习");
    }
}
package com.lotuslaw.oop.advanced;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-5:26 下午
 * @package: com.lotuslaw.oop.advanced
 * @description:
 */
public class Person {
    // 一个类即使什么都不写,它也会存在一个方法
    // 显式的定义构造器
    // 构造器:1、和类名相同;2、没有返回值
    String name;
    // 实例化初始值
    // 1、使用new关键字,本质是在调用构造器
    // 2、构造器一般用来初始化值
    public Person(){
        this.name = "lotuslaw";
    }
    // 有参构造:一旦定义了有参构造,无参构造就必须显式定义
    public Person(String name){
        this.name = name;
    }
}
package com.lotuslaw.oop;


import com.lotuslaw.oop.advanced.Person;
import com.lotuslaw.oop.advanced.Student;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-5:15 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
// 一个项目应该只存在一个main方法
// 以类的方式组织代码,以对象的形式封装数据
public class Application {
  public static void main(String[] args){
    // 类:抽象的,需要实例化
        Student xiaoming = new Student();
        Student xiaohong = new Student();
        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);
        xiaoming.name = "小明";
        xiaoming.age = 13;
        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);
        System.out.println("=============");
        // 使用该无参构造,必须要有显式无参构造
        Person person1= new Person();
        System.out.println(person1.name);
        Person xiaopi = new Person("xiaopi");
        System.out.println(xiaopi.name);
  }
}
  • 创建对象内存分析
package com.lotuslaw.oop.memoryAnalysis;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-5:53 下午
 * @package: com.lotuslaw.oop.memoryAnalysis
 * @description:
 */
public class Pet {
    public String name;
    public int age;
    // 无参构造
    public void shout(){
        System.out.println("叫了一声");
    }
}
package com.lotuslaw.oop;


import com.lotuslaw.oop.memoryAnalysis.Pet;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-5:15 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
// 一个项目应该只存在一个main方法
// 以类的方式组织代码,以对象的形式封装数据
public class Application {
  public static void main(String[] args){
    Pet dog = new Pet();
    dog.name = "旺财";
    dog.age = 3;
    dog.shout();
    System.out.println(dog.name);
    System.out.println(dog.age);
    Student s1 = new Student();
    s1.setName("lotuslaw");
    System.out.println(s1.getName());
    s1.setAge(1202);
    System.out.println(s1.getAge());
  }
}
  • 封装
package com.lotuslaw.oop.encapsulation;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-6:20 下午
 * @package: com.lotuslaw.oop.encapsulation
 * @description:
 */
// 类:private 私有
/*
* 封装:属性私有,get/set
* 1、提供程序的安全性,保护数据
* 2、隐藏代码的实现细节
* 3、统一接口
* 4、系统可维护增加了
* */
public class Student {
    private String name;
    private int id;
    private char sex;
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age > 120 || age < 0){
            this.age = 3;
        }else {
            this.age = age;
        }
    }

    // 提供一些可以操作这些属性的方法
    // 提供一些public的get、set方法
    public String getName(){
        return this.name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name){
        this.name = name;
    }
}
  • 继承
package com.lotuslaw.oop.extend;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-8:28 下午
 * @package: com.lotuslaw.oop.extend
 * @description:
 */
// 人:父类
// 在Java中,所有的类都默认直接或间接继承Object类
// Java中只有单继承,没有多继承
public class Person /*extends Object*/{
    /*
    * public
    * protected
    * default
    * private
    * */

    public Person() {
        System.out.println("Person无参执行了");
    }

    // 私有的东西无法被继承
    protected String name = "lotuslaw";
    public int money = 10_0000_0000;
    public void say(){
        System.out.println("说了一句话");
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}
package com.lotuslaw.oop.extend;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-8:28 下午
 * @package: com.lotuslaw.oop.extend
 * @description:
 */
// 学生:子类
// 子类继承了父类,就会拥有父类的全部方法
public class Student extends Person {
    // control + h

    public Student() {
        // 隐藏代码:默认调用了父类的无参构造
        super();  // 调用父类的构造器必须在子类构造器的第一行
        System.out.println("Student无参执行了");
    }

    private String name = "LOTUSLAW";
    public void test(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }
}
package com.lotuslaw.oop.extend;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-8:29 下午
 * @package: com.lotuslaw.oop.extend
 * @description:
 */
// 教师:子类
public class Teachers extends Person{
}
package com.lotuslaw.oop.extend;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-8:59 下午
 * @package: com.lotuslaw.oop.extend
 * @description:
 */
// 重写都是方法的重写,和属性无关
public class B {
    public void test(){
        System.out.println("B=>test()");
    }
}
package com.lotuslaw.oop.extend;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-8:59 下午
 * @package: com.lotuslaw.oop.extend
 * @description:
 */
public class A extends B{
    /*
    * 重写:
    * 需要有继承关系,子类重写父类的方法
    * 子类的方法和父类的方法必须相同,方法体不同
    * 1、方法名必须相同
    * 2、参数列表必须相同
    * 3、修饰符:范围可以扩大,但不能缩小:public>protected>default>private
    * 4、抛出的异常,范围可以被缩小,但不能扩大
    * */
    @Override  // 注解:有功能的注释
    public void test() {
        System.out.println("A=>test()");
    }
}
package com.lotuslaw.oop;


import com.lotuslaw.oop.extend.A;
import com.lotuslaw.oop.extend.B;
import com.lotuslaw.oop.extend.Student;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-5:15 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
// 一个项目应该只存在一个main方法
// 以类的方式组织代码,以对象的形式封装数据
public class Application {
  public static void main(String[] args){
    Student student = new Student();
    student.say();
    System.out.println(student.getMoney());
    student.test("lotuslaw");
  }
}
package com.lotuslaw.oop;


import com.lotuslaw.oop.extend.A;
import com.lotuslaw.oop.extend.B;
import com.lotuslaw.oop.extend.Student;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-5:15 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
// 一个项目应该只存在一个main方法
// 以类的方式组织代码,以对象的形式封装数据
public class Application {
  public static void main(String[] args){
    // 静态方法与非静态方法区别很大
    // 静态方法:方法的调用只和定义的数据类型有关
    // 非静态方法:重写
    A a = new A();
    a.test();  // A
    // 父类的引用指向了子类
    B b = new A();  // 子类重写了父类的方法
    b.test();  // B
  }
}
  • 多态
package com.lotuslaw.oop.polymorphic;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-9:19 下午
 * @package: com.lotuslaw.oop.polymorphic
 * @description:
 */
public class Person {
    public void run(){
        System.out.println("run");
    }
}
package com.lotuslaw.oop.polymorphic;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-9:19 下午
 * @package: com.lotuslaw.oop.polymorphic
 * @description:
 */
public class Student extends Person{
    /*
    * 多态是方法的多态
    * 父类和子类,有联系,类型转换异常ClassCastException
    * 存在条件:存在继承关系,方法重写,父类引用指向子类 father f1 = new Son();
    * static方法,属于类,它不属于实例
    * final常量
    * private方法无法重写
    * */
    @Override
    public void run() {
        System.out.println("son");
    }
    public void eat(){
        System.out.println("eat");
    }
}
package com.lotuslaw.oop.polymorphic;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-9:55 下午
 * @package: com.lotuslaw.oop.polymorphic
 * @description:
 */
public class Teacher extends Person{
}
package com.lotuslaw.oop;


import com.lotuslaw.oop.extend.A;
import com.lotuslaw.oop.extend.B;
import com.lotuslaw.oop.extend.Student;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-5:15 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
// 一个项目应该只存在一个main方法
// 以类的方式组织代码,以对象的形式封装数据
public class Application {
  public static void main(String[] args){
    // 一个对象的实际类型是确定的,可以指向的引用类型就不确定了
    // Student能调用的方法都是自己或者继承父类的
    Student s1 = new Student();
    // 父类的引用指向子类
    // 父类型,可以指向子类,但是不能调用子类独有的方法
    Person s2 = new Student();
    Object s3 = new Student();
    // 对象能执行哪些方法,主要看左边的类型,和右边关系不大
    s2.run();  // 子类重写了父类的方法,执行子类的方法
    s1.run();
    s1.eat();
  }
}
  • instancaof
package com.lotuslaw.oop;


import com.lotuslaw.oop.extend.A;
import com.lotuslaw.oop.extend.B;
import com.lotuslaw.oop.extend.Student;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-5:15 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
// 一个项目应该只存在一个main方法
// 以类的方式组织代码,以对象的形式封装数据
public class Application {
  public static void main(String[] args){
    // X instancaof Y 是否能编译通过,取决于X指向的类型与Y是否存在父子关系
    Object object = new Student();
    System.out.println(object instanceof Student);
    System.out.println(object instanceof Person);
    System.out.println(object instanceof Object);
    System.out.println(object instanceof Teacher);
    System.out.println(object instanceof String);
    System.out.println("================");
    Person person = new Student();
    System.out.println(person instanceof Student);
    System.out.println(person instanceof Person);
    System.out.println(person instanceof Object);
    System.out.println(person instanceof Teacher);
    // System.out.println(person instanceof String);  // 编译报错
    Student student = new Student();
    System.out.println(student instanceof Student);
    System.out.println(student instanceof Person);
    System.out.println(student instanceof Object);
    // System.out.println(student instanceof Teacher);  // 编译报错
    // System.out.println(student instanceof String);  // 编译报错
  }
}
  • 类型转换
package com.lotuslaw.oop;


import com.lotuslaw.oop.extend.A;
import com.lotuslaw.oop.extend.B;
import com.lotuslaw.oop.extend.Student;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-5:15 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
// 一个项目应该只存在一个main方法
// 以类的方式组织代码,以对象的形式封装数据
public class Application {
  public static void main(String[] args){
    // 类型之间的转换  父  子
    // 高                   低
    Student student = new Student();
    // 将student对象转换为Student类型,就可以使用Student类型的方法
    // ((Student) student).eat();
    // 子类转换为父类,可能会丢失一些自己本来的一些方法
    Person person = student;
  }
}
  • 抽象类
package com.lotuslaw.oop.abstractClass;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-10:33 下午
 * @package: com.lotuslaw.oop.abstractClass
 * @description:
 */
// abstract 抽象类:类,extends: 单继承,接口可以多继承
public abstract class Action {
    // 约束~有人帮我们实现
    // abstract,抽象方法,只有方法名字,没有方法的实现
    public abstract void doSomething();
    // 不能new抽象类,只能靠子类去实现它,约束
    // 抽象类可以写普通方法,抽象方法只能存在于抽象类
    // 抽象的抽象:约束
}
package com.lotuslaw.oop.abstractClass;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-10:35 下午
 * @package: com.lotuslaw.oop.abstractClass
 * @description:
 */
public class A extends Action{
    // 抽象类的方法,继承它的子类都必须要实现它的方法,除非子类也是抽象类
    @Override
    public void doSomething() {

    }
}
  • 接口
package com.lotuslaw.oop.interfacePackage;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-10:51 下午
 * @package: com.lotuslaw.oop.interfacePackage
 * @description:
 */
public interface TimeService {
    void timer();
}
package com.lotuslaw.oop.interfacePackage;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-10:45 下午
 * @package: com.lotuslaw.oop.interfacePackage
 * @description:
 */
// interface定义的关键字,接口都需要有实现类
// 接口不能被实例化,因为接口中没有构造方法
public interface UserService {
    // 常量 public static final
    // int age = 99;
    // 接口中的所有定义其实都是抽象的public abstract
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}
package com.lotuslaw.oop.interfacePackage;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-10:48 下午
 * @package: com.lotuslaw.oop.interfacePackage
 * @description:
 */
// 抽象类:extends,只能单继承
// 类可以实现一个接口,implements接口
// 实现了接口的类必须要重写接口的方法
// 伪多继承
public class UserServiceimpl implements UserService, TimeService{
    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }

    @Override
    public void timer() {

    }
}
  • 内部类
package com.lotuslaw.oop.innerClass;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-10:58 下午
 * @package: com.lotuslaw.oop.innerClass
 * @description:
 */
public class Outuer {
    private int id = 10;
    public void out(){
        System.out.println("这是外部类的方法");
    }
    // 加static变成静态内部类
    public class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }
        // 获得外部类的私有属性
        public void getID(){
            System.out.println(id);
        }
    }
    public void method(){
        // 局部内部类
        class Inner2{
            public void in(){

            }
        }
    }
}
// 一个Javal类中可以有多个class类,但只能有一个public class类
class A {
    public static void main(String[] args) {

    }
}
package com.lotuslaw.oop;


import com.lotuslaw.oop.innerClass.Outuer;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-5:15 下午
 * @package: com.lotuslaw.oop
 * @description:
 */
// 一个项目应该只存在一个main方法
// 以类的方式组织代码,以对象的形式封装数据
public class Application {
  public static void main(String[] args){
    Outuer outuer = new Outuer();
    // 通过外部类实例化内部类
    Outuer.Inner inner = outuer.new Inner();
    inner.in();
    inner.getID();
  }
}

Exception

  • 异常
package com.lotuslaw.exception;

/**
 * @author: lotuslaw
 * @create: 2021-06-06-11:14 下午
 * @package: com.lotuslaw.exception
 * @description:
 */
public class Demo1 {
    public static void main(String[] args) {
        System.out.println(11/0);
    }
}
  • 异常捕获与抛出
package com.lotuslaw.exception;

/**
 * @author: lotuslaw
 * @create: 2021-06-07-8:21 上午
 * @package: com.lotuslaw.exception
 * @description:
 */
// 自定义的异常类
public class MyException extends Exception{
    // 传递数字
    private int detail;

    public MyException(int a) {
        this.detail = a;
    }
    // toString,异常的打印信息
    @Override
    public String toString() {
        return "MyException{" + "detail=" + detail + '}';
    }
}
package com.lotuslaw.exception;


/**
 * @author: lotuslaw
 * @create: 2021-06-07-8:00 上午
 * @package: com.lotuslaw.exception
 * @description:
 */
public class Test {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try {
            new Test().test(a, b);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }
    public void a(){b();}
    public void b(){a();}
    // 假设方法中处理不了这个异常,方法上抛出异常
    public void test(int a, int b) throws ArithmeticException{
        if (b == 0){
            throw new ArithmeticException(); // 主动抛出异常,一般在方法中使用
        }
    }
    public void test2(int a) throws MyException{
        System.out.println("传递的参数为" + a);
        if (a > 10){
            throw new MyException(a);  // 抛出
        }
        System.out.println("OK");
    }
}
package com.lotuslaw.exception;


/**
 * @author: lotuslaw
 * @create: 2021-06-07-8:00 上午
 * @package: com.lotuslaw.exception
 * @description:
 */
public class Test {
    public static void main(String[] args) {
        try {
            // System.out.println(a / b);
            new Test().a();
        }catch (Error e){
            System.out.println("程序出现异常,变量b不能为0");
        }catch (Exception e){
            System.out.println("Exception");
        }catch (Throwable e){
            System.out.println("Throwable");
        }finally {
            System.out.println("finally");
        }
    }
    public void a(){b();}
    public void b(){a();}
    // 假设方法中处理不了这个异常,方法上抛出异常
    public void test(int a, int b) throws ArithmeticException{
        if (b == 0){
            throw new ArithmeticException(); // 主动抛出异常,一般在方法中使用
        }
    }
    public void test2(int a) throws MyException{
        System.out.println("传递的参数为" + a);
        if (a > 10){
            throw new MyException(a);  // 抛出
        }
        System.out.println("OK");
    }
}
package com.lotuslaw.exception;


/**
 * @author: lotuslaw
 * @create: 2021-06-07-8:00 上午
 * @package: com.lotuslaw.exception
 * @description:
 */
public class Test {
    public static void main(String[] args) {
        try {
            new Test().test2(11);
        } catch (MyException e) {
            e.printStackTrace();
        }
    }
    public void a(){b();}
    public void b(){a();}
    // 假设方法中处理不了这个异常,方法上抛出异常
    public void test(int a, int b) throws ArithmeticException{
        if (b == 0){
            throw new ArithmeticException(); // 主动抛出异常,一般在方法中使用
        }
    }
    public void test2(int a) throws MyException{
        System.out.println("传递的参数为" + a);
        if (a > 10){
            throw new MyException(a);  // 抛出
        }
        System.out.println("OK");
    }
}
package com.lotuslaw.exception;

/**
 * @author: lotuslaw
 * @create: 2021-06-07-8:08 上午
 * @package: com.lotuslaw.exception
 * @description:
 */
public class Test2 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try {
            System.out.println(a / b);  // 选择代码,command + option +
        } catch (Exception e) {
            e.printStackTrace();  // 打印错误的栈信息
        } finally {
        }
    }
}
posted @ 2021-06-12 19:15  lotuslaw  阅读(60)  评论(0编辑  收藏  举报