Java

383 阅读9分钟

注意!!

  1. 每个单词的大小写不能出现问题,Java大小写敏感。
  2. 尽量使用英文。
  3. 文件名和类名(class后的名字)必须保持一致,并首字母大写。
  4. 符号使用英文。

Java的基础语法

注释

public class Hello {
    public static void main(String[] args) {
        //单行注释:注释一行文字  //
        // 输出一个Hello,world!
        System.out.println("Hello,World!");
    }
}

public class Hello {
    public static void main(String[] args) {
        //多行注释:注释一段文字  /* 注释 */
        /*
        我是多行注释
        我是多行注释
        我是多行注释
        */
        System.out.println("Hello,World!");
    }
}

// JavaDoc:文档注释  /** 注释 */
/**
 * @Dsecription hello
 *
 *
 *
 * */

标识符和关键词

关键词

image.png

标识符注意点:

  1. 所有的标识符都应该以字母(A-Z或者a-z),美元符($)、或者下划线(_)开始
  2. 首字符之后可以是字母(A-Z或者a-z),美元符($)、下划线(_)或数字的任何字符组合
  3. 不能使用关键字作为变量名或方法名。
  4. 标识符是大小写敏感的
  5. 可以使用中文命名,但是一般不建议这样去使用,也不建议使用拼音,很Low
  6. 合法标识符:age、$salary._value、_1_value 非合法标识符:123abc、-salary、#abc

标识符的设置与运用

public class Hello {
    public static void main(String[] args) {
        String hello = "qinjiang";
        System.out.println(hello);
    }
}

数据类型

Java属于强类型语言,所有变量都必须先定义才能使用。

image.png

// 十进制 八进制 十六进制
int num1 = 10;
int num2 = 010;
int num3 = 0x10;
System.out.println(num1);
System.out.println(num2);
System.out.println(num3); // 输出10 8 16


// 强制转换
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int)c1);
System.out.println(c2);
System.out.println((int)c2);

转义字符

image.png

类型转换

image.png

public class Hello {
    public static void main(String[] args) {
        int b = 10;
        byte i = (byte) b;
        System.out.println(b);
        System.out.println(i); // 强制转换  (类型)变量名  高---低

        int z = 10;
        double y =  z;
        System.out.println(z);
        System.out.println(y);  // 自动转换   低---高
    }
}

注意
1.不能对布尔值进行转换
2.不能把对象类型转换为不相干类型
3.把高容量转换到低容量时强制转换
4转换的时候可能出现内存溢出,或是精度问题

变量

public class Hello {
    // 类变量  static
    static double salary = 2500;

    // 实例变量:从属于对象,如果不自行初始化,默认值为0  0.0
    // 布尔值: 默认为false
    String name;
    int age;
    public static void main(String[] args) {
        // 局部变量 必须声明和初始化
        int i = 10;
        System.out.println(i);

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

        //实例变量
        // new Hello() 加ait加回车加回车
        Hello hello = new Hello();
        System.out.println(hello.age);
        System.out.println(hello.name);
    }
}

常量

通过final定义

public class Hello {
    static final double PI = 3.14;
    public static void main(String[] args) {
        System.out.println(PI);
    }
}

运算符

image.png 注意

  1. 计算时有long类型,结果为long类型,无long类型则全为int类型。
// 位运算  (效率高)
A = 0011 1100
B = 0000 1101
----------------------------------
A&B = 0000 1100 // 与
A/B = 0011 1101 // 或
A^B = 0011 0001 // 同为0,非为1
~B = 1111 0010 // 与B相反

// << , >>  
int a = 2; // 0000 0010 
int b = 2<<3; // 0001 0000
System.out.println(a); // 2
System.out.println(b); // 16
// 字符串连接符 + 
int a = 10;
int b = 20;
System.out.println(""+a+b); // 1020
System.out.println(a+b+""); // 30
顺序不一样,结果不一样。

包机制

image.png

注意: 其中*代表通配符

image.png

用户交互Scanner

image.png

package scannner;

import java.util.Scanner;

public class dom01 {
    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();
    }
}

image.png


image.png

package scannner;

import java.util.Scanner;

public class dom0 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nestLine方式接收:");

        String s = scanner.nextLine();

        System.out.println("输出的内容是:" +s);

        // 凡事IO流的类如果不关闭就一直占用资源,要习惯用完就关掉
        scanner.close();
    }
}

image.png

选择结构

if结构

package scannner;

import java.util.Scanner;

public class zhang {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();

        if (score==100){
            System.out.println("满分");
        } else if (score<100 && score>=90){
            System.out.println("A级");
        }else if (score<90 && score>=80){
            System.out.println("B级");
        }else if (score<80&& score>=70){
            System.out.println("C级");
        }else if (score<70 && score>=60){
            System.out.println("D级");
        }else if (score<60 && score>=0){
            System.out.println("E级");
        }else {
            System.out.println("不合理");
        }

        scanner.close();
    }
}

Switch结构

package scannner;

public class switchdom01 {
    public static void main(String[] args) {
        char grade = 'A';

        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("未知等级");
        }

    }
}

循环结构

while循环

先判断后执行

package scannner;

public class whiledom01 {
    public static void main(String[] args) {
        // 计算 1+2+3...+100=?
        int i = 0;
        int sum = 0;

        while (i<=100){
            sum += i;
            i++;
        }
        System.out.println(sum);
    }
}

do...while循环

先试行后判断

package scannner;

public class whiledom01 {
    public static void main(String[] args) {
        // 计算 1+2+3...+100=?
        int i = 0;
        int sum = 0;

        do{
            sum += i;
            i++;
        }while (i<=100);
        System.out.println(sum);
    }
}

for循环

for的小技巧
---------------------------------------
100.for 加 回车 (可以快速生成)
---------------------------------------
for (int i = 0; i < 100; i++) {
    
}


// 遍历数组的技巧
public class math {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40};

        for (int i=0;i<=3;i++){
            System.out.println(numbers[i]);
        }
// 上下结果相同
        for (int x :numbers){
            System.out.println(x);
        }
    }
}

package scannner;

public class fordom01 {
    public static void main(String[] args) {
        // 计算0到100之间奇数和偶数的和

        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);
        System.out.println("偶数的和"+evenSum);

    }
}

方法的定义与调用

package scannner;

public class dom{
    public static void main(String[] args) {
        int sum = odd(1,2);
        System.out.println(sum);
    }
    
    // 加法
    public static int odd (int a,int b){
        return a+b;
    }
}

可通过 odd(1,2); 加Alt+回车, 快速引用

方法的重载

image.png

package scannner;

public class dom{
    public static void main(String[] args) {
        int sum = odd(1,2);
        double odd = odd(2, 3);
        System.out.println(sum);
        System.out.println(odd);
    }
    public static int odd (int a,int b){
        return a+b;
    }
    public static double odd (double a,double b){
        return a+b;
    }
}

image.png

可变参数

image.png

package scannner;

public class dom{
    public static void main(String[] args) {
        dom dom = new dom();
        dom.add(0 ,1 ,2);
    }
    public void add(int...i){
        System.out.println(i[0]);
        System.out.println(i[1]);
        System.out.println(i[2]);
    }
}

递归

package scannner;

public class dom{
    public static void main(String[] args) {
        System.out.println(f(5));
    }
    public static int f(int n){
        if (n==1){
            return 1;
        }else {
            return n*f(n-1); // 自己调用自己
        }
    }
}

数组

数组的创建

有上下两种写法
int[] nums;   // 首选
int num[];

image.png

 int[] nums; // 1.声明一个数组
 nums = new int[10]; // 2.创造一个数组
 
 int[] nums = new int[10];

数组的初始化

package scannner;

public class dom{
    public static void main(String[] args)  {
        // 静态初始化: 创建 + 赋值
       int[] a = {1,2,3,4};
        System.out.println(a[0]);

        // 动态初始化: 没有赋值的数组默认为0
        int[] b = new int[10];
        b[0] = 1;
    }
}

二维数组

package scannner;

public class dom{
    public static void main(String[] args)  {
        int[][] array = {{1,2},{3,4},{5,6}};
        System.out.println(array[0][0]); // 1
    }
}

冒泡排序

package scannner;

import java.lang.reflect.Array;
import java.util.Arrays;

public class dom{
    public static void main(String[] args) {
        int[] a = {56,25,3,655,15,26,44,2,56,5};
        int[] sort = sort(a);
        System.out.println(Arrays.toString(sort));
    }
    public static int[] sort(int[] array){
        int temp = 0;
        // 外层循环,判断要走多少次
        for (int i=0;i<array.length;i++){
            // 内层循环,比较两个数,若第一个比第二个大,则交换位置
            for (int j=0 ;j<array.length-1;j++){
                if (array[j+1]>array[j]){
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1]=temp;
                }
            }
        }
        return array;
    }
}

面向对象

方法的调用

静态调用

有static

image.png

非静态调用

无static,需要用到new dom3;

image.png

注意!! (以下是错误用法,方法加载完成的时间不同)

// 和类一起加载
public static void a(){
    b();
}

// 类实例化之后才出现
public void b() {

}

构造器

特点

  1. 和类名相同
  2. 没有返回值

作用

  1. new 本质是在调用构造器
  2. 用来初始化对象的值

注意点

  1. 定义有参构造后,如果想调用无参构造,显示的定义一个无参构造
  2. 快捷键Alt + fn + insert可以快速构造函数。

image.png

创建对象的内存分析

image.png

image.png

封装

属性私有, get/set

快捷键Alt + fn + insert可以快速构造方法。

意义

  1. 提高程序的安全性,保护数据。
  2. 隐藏代码的实现细节
  3. 统一接口
  4. 系统的可维护性增加

image.png

继承

extands的意思是扩展,子类是父类的扩展

image.png

image.png

可以通过Ctrl+H查看继承树

image.png

super详解

子类调用父类的内容或方法

image.png

image.png

当父类用private私有时,子类无法调用出父类的内容及方法

image.png

方法重写

子类从父类继承的某个实例方法无法满足子类的功能需要时,需要在子类中对该实例方法进行重新实现,这样的过程称为重写,也叫做覆写、覆盖。

image.png

也可以通过Alt + fn + insert快速生成重写

image.png

以上调用了非静态方法,若为静态方法:调用的类只和左边定义的类有关(如下),非静态重写了父类的方法(如上)

image.png

多态

即同一种方法根据发送对象的不同而采用不同的行为方式,必须是父子类。

image.png

instanceof详解

用于鉴别是否有父子关系

image.png

类型之间的转换

image.png

抽象类

image.png

image.png

注意

  1. 不能new这个抽象类,只能靠子类去实现它;约束!
  2. 抽象类中可以写普通的方法
  3. 抽象方法只能在抽象类中

接口

只有规范,不能写方法

第一部分:用interface代替class来创造接口

注意:在接口里定义的属性都为常量

image.png

第二部分:用implements来连接接口

image.png

内部类

内部类和外部类

image.png

静态内部类

静态内部类无法访问非静态属性 ,因为静态内部类率先加载。

image.png

局部内部类

image.png

匿名内部类

package dom01;

public class Ourer {
    public static void main(String[] args) {
//        Apple apple = new Apple();  
        
        // 没有名字初始化类,不用将实例保存在变量中
        new Apple().eat();
        
        new UserSService(){
            @Override
            public void hello() {
                
            }
        };
    }
}
class Apple{
    public void eat(){
        System.out.println("1");
    }
}

interface UserSService{
    void hello();
}

异常机制

8c254a717b166bf6da52f5532ccd64e.jpg

异常种类

fd2b8e4cd769cdad01792fcbba3a3b7.jpg

处理异常

处理异常的五个关键词 try , catch , finally , throw , throws

1.处理异常

public class dom01 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;

        try { // try监控区域
            System.out.println(a/b);
        }catch (ArithmeticException e){ // 捕获区域
            // ArithmeticException 对应了上面的异常体系图
            System.out.println("程序出现异常,变量b不能为零");
        }catch (Error e){
            System.out.println("出现异常");
            // catch只会生效一个一个,把最大的放在最后面。 
            // 假设要捕获多个异常,按照从小到大开始捕捉
        }
        finally { // 善后处理
            System.out.println("finally");
        }

        // finally可以不要
    }
}
  1. 快捷键创造
package dom01;

public class Text {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;

        //  选中此行代码 System.out.println(a/b); 摁住Ctrl+Alt+T可生成快捷键,生成以下代码

        try {
            System.out.println(a/b);
        } catch (Exception e) {
            e.printStackTrace(); // 打印错误的栈信息
        } finally {
        }
    }
}

3.抛出异常

package dom01;

public class Text {
    public static void main(String[] args) {
        try {
            new Text().test1(1,0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }

    // 假设方法处理不了这个异常,方法上主动抛出
    public void test1(int a,int b) throws ArithmeticException{
        if (b==0){
            throw new ArithmeticException(); // 主动抛出异常,主要用在方法中
        }
    }
}

自定义异常

5d0399c9d301533af59a59692ee02b1.jpg

image.png

e977b45871fbf4bec1c0155bf9f52e1.jpg