java基础知识

一、计算机语言发展历史

  • 第一代语言:机器语言
  • 第二代语言:汇编语言
  • 第三代语言:高级语言(面向过程:C语言。面向对象:java,c++)

1972年c诞生–>1982年c++诞生–>1995年(java javaSE:标准版【占领桌面】 javaME:移动版【占领手机】 javaEE:企业版【占领服务器】)

二、java特性和优势

简单性

  • 面向对象
  • 可移植性
  • 高性能
  • 分布式
  • 动态性
  • 多线程
  • 安全性
  • 健壮性

JDK:java development kit
JRE:java Runtime Environment
JVM:java Virtual Machine

在这里插入图片描述

JDK:是 Java 语言的软件开发工具包(SDK)。包含jre和jvm
[没有JDK的话,无法编译Java程序(指java源码.java文件),如果想只运行Java程序(指class或jar或其它归档文件),要确保已安装相应的JRE。]

三、java基础

1、注释

  • 单行注释 //
  • 多行注释 /**/
  • 文档注释 /** */

2、标识符

类名、变量名和方法名都被成为标识符。
注意:
【1】字母(a-z或者A-Z),美元符($)、或者下划线(_)开始。之后可以是前面提到的以及数字
【2】大小写敏感
【3】 不能使用关键字作为变量名或者方法名

  • 关键字(abstract new try class float public static…)

3、数据类型

  • 强类型语言:变量的使用符合规定、变量先定义后使用
  • 弱类型语言

java数据类型分为两大类

  • 基本类型
    在这里插入图片描述

  • 引用数据类型:类、接口、数组

什么是字节:是计算机中数据处理的基本单位(B)
什么是位:计算机内部数据存储的最小单位
1B=8bit(位)

  • 1KB=1024B
  • 1M=1024KB
  • 1G=1024M

数据类型扩展

package com.yu.type;

public class MyType {
    public static void main(String[] args) {

        int i=5;
        int i1=010;//八进制
        int i2=0x10;//十六进制
        System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);
    }
}

在这里插入图片描述
浮点类型

package com.yu.type;

public class MyType {
    public static void main(String[] args) {

        float a=0.1f; //舍入误差   大约   接近并不等于
        double b=0.1;
        double c=1/10;
        double d=1.0/10;
        System.out.println(a==b);
        System.out.println(c);
        System.out.println(d);
        
        

    }
}

在这里插入图片描述
字符型

package com.yu.type;

public class MyType {
    public static void main(String[] args) {

        String java1 = new String("java");
        String java2 = new String("java");
        System.out.println(java1==java2);
        System.out.println("===================");
        String java3="java";
        String java4="java";
        System.out.println(java3==java4);
        System.out.println("===================");
        System.out.println(java1==java3);



    }
}

在这里插入图片描述
4、类型转换
低------------------------------------>高
byte,short,char–>int–>long–>float–>double

  • 强制类型转换:高–>低
  • 自动类型转换:低–>高

四、变量、常量、作用域

数据类型 变量名=值:可以使用逗号声明多个同类型变量

不合法格式
1、

  int a,b,c=9;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

在这里插入图片描述

2、

      int a=b=c=9;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

在这里插入图片描述
合法的形式

  int a=1,b=2,c=9;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

在这里插入图片描述
全局变量:不初始化,基本类型默认值0 0.0 布尔类型:false.其余默认null
局部变量:必须声明和初始化,只在该方法体内有效
常量:被设定后,不能修改。使用final

变量的 命名规范

  • 变量、类名、方法名:见名知意
  • 类成员变量:首字母小写,驼峰原则。
  • 局部变量:首字母小写,驼峰原则
  • 常量:大写字母和下划线
  • 类名:首字母大写,驼峰原则
  • 方法名 :首字母小写,驼峰原则

五、基本运算符

  • 算术运算符:+、-、*、/,%,++,–
  • 赋值运算符:=
  • 关系运算符:>,<,>=,<=,==
  • 逻辑运算符:&&、||、!
  • 位运算符:&、|、^、>>、<<、>>>
  • 条件运算符:?:

自增自减

package com.yu.type;

public class MyType {

    public static void main(String[] args) {
        int a = 3;
        int b = a++;
        int c = ++a;
        System.out.println(b);//b=3,此时a=4。在将a的值赋值给b,之后,a进行加1运算
        System.out.println(c);//c=5,此时a=5。在进行赋值之前,a先加上1.然后赋值给右侧
        System.out.println(a);

        int d=a--;
        int e=--a;
        System.out.println(d);//d=5
        System.out.println(e);//e=3


    }
}


字符串拼接

package com.yu.type;

public class MyType {

    public static void main(String[] args) {
        int a = 3;
        int b=4;
        System.out.println(a+b);
        System.out.println(""+a+b);
        System.out.println(a+b+"");

    }
}

在这里插入图片描述
放在前面代表字符串拼接。放在后边不影响运算

三元运算符

package com.yu.type;

public class MyType {

    public static void main(String[] args) {
        int a = 3;
        int b=4;
        int c=a>b?1:2;
        System.out.println(c);//表达式判断正确,执行冒号前,错误,执行冒号后

    }
}

六、流程控制

scanner的使用

1、使用next()得到数据

package com.yu.type;

import java.util.Scanner;

public class MyType {

    public static void main(String[] args) {

        //键盘接收数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入:");
        String str=scanner.next();
        System.out.println("输入的内容是:"+str);
    }
}

在这里插入图片描述
注意:使用next()接收数据。1、自动去除有效字符前的空格。2、两个字符中间有空格自动去除后边的字符。3、不能得到带有空格的字符串

2、使用nextLine()接收数据

package com.yu.type;

import java.util.Scanner;

public class MyType {

    public static void main(String[] args) {

        //键盘接收数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入:");
        String str=scanner.nextLine();
        System.out.println("输入的内容是:"+str);
    }
}

在这里插入图片描述
输入的怎样的字符串,就输出怎样的字符串

1、顺序结构
基本算法结构,按照顺序执行
java的基本结构就是顺序结构
在这里插入图片描述

2、选择结构
if选择结构(单选结构)
在这里插入图片描述
if多选择结构

package com.yu.type;

import java.util.Scanner;

public class MyType {

    public static void main(String[] args) {
        //请输入成绩
        System.out.println("请输入您的成绩:");
        Scanner scanner = new Scanner(System.in);
        double score = scanner.nextDouble();

        if (score > 100) {
            System.out.println("输入的分数不合要求");
        } else if (score >= 90) {
            System.out.println("优秀");
        } else if (score >= 80) {
            System.out.println("良好");
        } else if (score >= 60) {
            System.out.println("及格");
        } else {
            System.out.println("不及格");
        }


    }
}

switch选择结构

package com.yu.type;

import java.util.Scanner;

public class MyType {

    public static void main(String[] args) {
        //请输入成绩
        System.out.println("请输入A|B|C:");
        Scanner scanner = new Scanner(System.in);
        String score = scanner.nextLine();

        switch (score) {
            case "A":
                System.out.println("优秀");
                break;
            case "B":
                System.out.println("良好");
                break;
            case "C":
                System.out.println("及格");
                break;
            default:
                System.out.println("不及格");
        }


    }
}

在这里插入图片描述

3、循环结构

  • while循环
  • do…while循环
  • for循环

while循环

package com.yu.type;


public class MyType {

    public static void main(String[] args) {

        while (true) {
            for (int i = 0; i < 10; i++) {
                System.out.println(i);
                if (i == 5)
                    return;

            }
        }

    }
}

在这里插入图片描述
do…while循环

package com.yu.type;


public class MyType {

    public static void main(String[] args) {

        int i = 0;
        int sum = 0;
        do {
            sum += i;
            i++;
        } while (i <= 100);

        System.out.println(sum);

    }
}

在这里插入图片描述

区别:while循环:先判断后执行。do…while循环,先执行后判断。【保证循环体至少执行一次】

for循环打印乘法表

package com.yu.type;


public class MyType {

    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 + " ");

            }
            System.out.println("");

        }


    }
}

在这里插入图片描述

break:强行退出循环
continue:终止某次循环过程,接着执行下一次循环。

七、方法的定义 | 调用 | 重载 | 递归

用来完成特定功能的代码片段

  • 修饰符:public、private
  • 返回值类型:void、Stiring、int…
  • 方法名:第一个字母小写,驼峰命名、见名知意
  • 参数类型:类、string、int…
  • 方法体

修饰符 返回值类型 方法名(参数类型 参数名){

方法体
return 返回值;

}

调用

  • 对象名.方法名(实参列表)

方法的重载【方法名相同、参数列表不同】

  • 方法名相同
  • 参数列表不同(个数不同或类型不同、参数排列顺序)
  • 方法的返回值类型可相同也可以不同
package com.yu.type;


public class MyType {

    int sum = 0;

    public int add(int a, int b) {
        sum = a + b;
        return sum;

    }

    public int add(int a, int b, int c) {
        sum = a + b + c;
        return sum;
    }

    public void add(int a, int b, int c, int d) {
        sum = a + b + c + d;
        System.out.println(sum);

    }


    public static void main(String[] args) {
        MyType myType = new MyType();
        System.out.println(myType.add(1, 1));
        System.out.println(myType.add(1, 1, 1));
        myType.add(1, 1, 1, 1);


    }
}

在这里插入图片描述
递归

递归结构包括两部分:

  • 递归头:什么时候不调用自身方法,没有头,则进入死循环,一直调用自己
  • 递归体:什么时候需要调用自身方法
package com.yu.type;


public class MyType {
    public static void main(String[] args) {
        System.out.println(MyType.recursion(4));
    }

    public static int recursion(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n * recursion(n - 1);//调用自己
        }
    }


}

在这里插入图片描述
在这里插入图片描述

八、数组

  • 相同数据类型的集合
  • 按照一定的先后顺序排列
  • 每个数据称为数组元素,用下标来访问
  int [] array1={1,2,3,4,5};
  • 一维数组
package com.yu.type;


public class MyType {
    public static void main(String[] args) {
        //1、声明数组
        int[] array = null;

        //创建数组
        array = new int[5];

        //赋值
        array[0] = 1;
        array[1] = 2;
        array[2] = 3;
        array[3] = 4;
        array[4] = 5;

        //打印
        for (int i = 0; i < 5; i++) {
            System.out.println(array[i]);

        }
    }


}

在这里插入图片描述

在这里插入图片描述

  • 二维数组
    在这里插入图片描述
    相当于一维数组里边存储的是一维数组
    1、静态数组
package com.yu.type;


public class MyType {
    public static void main(String[] args) {
        //静态赋值
        int [][] array={{1,2,3},{4,5,6},{7,8,9,10}};

        //打印数组
        for (int i = 0; i < array.length; i++) {
            for (int j=0;j<array[i].length;j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println("");

        }
    }


}

在这里插入图片描述

2、动态数组

package com.yu.type;

public class MyArray {
    public static void main(String[] args) {
        
        //动态定义二维数组
        int[][] array = new int[2][5];

        //动态赋值
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                array[i][j] = (int) (Math.random() * 10);
            }

        }

        //打印
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
            }
            System.out.println("");

        }
    }
}

在这里插入图片描述

九、面向对象编程

类:属性+方法
三大特性:

  • 封装
  • 继承
  • 多态

类与对象的关系
类:是一种抽象的数据类型,对某一类事物整体描述、定义。不代表某一个具体的事物(动物、植物、、、)
对象:抽象概念的具体实例(沙皮狗、藏獒、猪笼草、菟丝子、、、)

使用new 关键字创建对象

package com.yu.type;


public class MyType {
    //属性
    String name;
    int age;

    //方法
    public void display() {
        System.out.println("我的名字是:" + name + " 年龄是:" + age);
    }

    public static void main(String[] args) {
        MyType myType = new MyType();
        myType.age = 6;
        myType.name = "小明";
        myType.display();

    }


}

在这里插入图片描述
构造器:用来初始化话值
在new 出新的对象的时候,默认调用无参构造器。若是显示定义了有参构造器,则必须定义无参构造器

1、封装
属性私有,get/set

  • 提高程序的安全性、保护数据
  • 隐藏代码的实现细节
  • 统一接口
  • 系统可维护增加
package com.yu.type;


public class MyType {
   public String name;
   public int age;
   public String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

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

测试

package com.yu.type;

public class Test {
    public static void main(String[] args) {
        MyType myType = new MyType();
        myType.setName("小明");
        myType.setAge(18);
        myType.setSex("男");
        System.out.println("姓名:"+myType.getName()+"\n"+"年龄:"+myType.getAge()+"\n"+"性别:"+myType.sex);
    }
}

在这里插入图片描述
2、继承
java中只有单继承没有多继承 extends

  • 子类拥有父类不是私有的属性和方法
  • 可以扩展父类
  • 重写父类的方法

super注意点:
1、super调用父类的构造方法,必须在构造方法的第一个
2、super只能出现在子类的方法或者构造方法中
3、super和this不能同时调用构造方法
4、this调用本类构造方法,this指向当前对象

package com.yu.type;

public class Father {
    public String name;
    public int age = 45;

    public Father() {
        System.out.println("我是父亲的无参构造");
    }

    public void say() {
        System.out.println("我是爸爸");
    }
}

package com.yu.type;

public class Son extends Father {

    public Son() {
        super();//调用父类的无参构造,必须写在构造方法的第一个
        System.out.println("我是儿子的无参构造");
    }

    //重写父类的方法
    public void say() {
        super.say();//调用父类的方法
        System.out.println("我是儿子");
    }

    //扩展父类没有的方法
    public void display() {
        System.out.println("===父亲的年龄是===" + super.age);
    }


    public static void main(String[] args) {
        Son son = new Son();
        son.say();

        //引用父类的属性
        System.out.println("父亲的年龄是:" + son.age);

        //通过super引用
        son.display();


    }
}

在这里插入图片描述
重写

  • 方法名必须相同
  • 参数列表相同
  • 修饰符:可以扩大不能缩小。父类是public 子类不能是private
  • 跑出的异常可以缩小。父类的异常范围比子类的异常范围大

重载

  • 方法名相同
  • 参数列表必须不同

3、多态

多态是方法的多态,属性没有多态
存在的条件

  • 存在继承关系
  • 子类重写父类方法
  • 父类引用指向子类对象
package com.yu.type;

public class Father {

    public void say() {
        System.out.println("我是爸爸");
    }
}

package com.yu.type;

public class Son extends Father {

    //重写父类的方法
    public void say() {
        System.out.println("我是儿子");
    }

    //扩展父类没有的方法
    public void display() {
        System.out.println("我是新扩展的方法");
    }


}

package com.yu.type;

public class TestFS {
    public static void main(String[] args) {

        //创建子类对象
        Son son = new Son();//调用的方法都是自己的或者继承父类的
        Father son1 = new Son();//不能调用子类独有的方法
        son.say();
        son.display();
        son1.say();//子类重写父类的方法,执行子类的方法


    }
}

向上转型:子类转换为父类
向下转型:父类转换为子类(强制转换)

static
通过类名.属性名 类名.方法名 调用

package com.yu.type;

public class TestStatic {
    {
        //匿名代码块,在构造函数之前调用,第二个执行
        System.out.println("匿名代码块");
    }

    static {
        //静态代码块,初始胡值,只执行一次.第一个执行
        System.out.println("静态代码块");
    }

    public TestStatic() {
        //第三个执行
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        TestStatic testStatic = new TestStatic();
        System.out.println("-----------------");
        TestStatic testStatic1 = new TestStatic();

    }
}


在这里插入图片描述

抽象类

使用abstract修饰的类称为抽象类
使用abstract修饰的方法叫做抽象方法

抽象类

  • 不能new抽象类,只能通过子类实现
  • 抽象类中可以写普通方法
  • 抽象方法必须写在抽象类中
package com.zheng.demo7;

public abstract class Animal {

    //抽象方法
    public abstract void say();

    //普通方法
    public void display() {
        System.out.println("我是动物");
    }

}

实现抽象类

package com.zheng.demo7;

public class Cat extends Animal {

    public void f() {
        System.out.println("我是小狗");
    }

    //重写父类的方法
    public void say() {
        System.out.println("汪汪汪......");

    }
}

测试

package com.zheng.demo7;

public class Test {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.say();
        cat.f();
        cat.display();//子类调用父类的方法
        System.out.println("++++++++++");

        Animal cat1 = new Cat();//父类不能调用子类扩展的方法
        cat1.say();
        cat1.display();


    }
}

在这里插入图片描述
接口
只有规范,声明接口的关键字是interface

作用

  • 约束一些共同的规则,然后子类完善
  • 定义一些方法,子类实现
  • 接口不能被实例化,没有构造方法
  • implement继承多个接口
  • 子类继承一个接口要重写接口中的所有方法

A接口

package com.zheng.demo8;

public interface A {
    public void a1();
}

B接口

package com.zheng.demo8;

public interface B {
    public void b1();
}

C接口

package com.zheng.demo8;

public interface C {
    public void c1();
}

测试

package com.zheng.demo8;

public class Test implements A, B, C {
    public void a1() {
        System.out.println("继承A接口");
    }

    public void b1() {
        System.out.println("继承B接口");
    }

    public void c1() {
        System.out.println("继承C接口");
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.a1();
        test.b1();
        test.c1();
    }
}

在这里插入图片描述

十、异常

java.lang.Throwable作为所有异常的超类
异常类分为:错误:Error和异常:Exception

在这里插入图片描述
错误:Error
Error类对象由java虚拟机生成并且抛出,大多数错误与代码的编写者所执行的操作无关。

运行时异常:一般由程序逻辑错误引起。

错误是致命的,是程序无法控制和处理的。java虚拟机一般会终止线程;异常一般情况下能被程序处理。

package com.zheng.demo11;

public class Test {


    public static void main(String[] args) {

        int a = 4;
        int b = 0;

        try {
            System.out.println(a / b);
        } catch (Exception e) {
            e.getStackTrace();
            System.out.println("分母不能为0");
        } finally {
            System.out.println("都执行");
        }

    }

}


在这里插入图片描述
程序会继续运行 、不会停止

  • try:可能发生异常的的代码
  • catch:用来捕获异常信息
  • finally:不管代码发生不发生异常都会执行

throw:一般在方法中使用

package com.zheng.demo11;

public class Test {
    
    public static void main(String[] args) {
        
        new Test().show(4, 0);
    }

    public void show(int a, int b) {
        if (b == 0) {
            throw new ArithmeticException();//在方法中使用,主动抛出异常
        }
        System.out.println(a / b);
    }

}

在这里插入图片描述

throws:抛出异常,调用改方法的时候处理异常

package com.zheng.demo11;

public class Test {

    public static void main(String[] args) {

        try {
            new Test().show(4, 0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
            System.out.println("分母不能为零");
        }
    }

    public void show(int a, int b) throws ArithmeticException {

        System.out.println(a / b);
    }

}

在这里插入图片描述
自定义异常

  • 创建自定义异常类继承Exception
  • 在方法中通过throw关键字抛出异常
  • 在当前抛出异常的方法中处理异常,使用try…catch捕获处理;或者在方法的声明处通过throws关键字抛出异常,进行下一步操作
  • 在调用该异常的方法中捕获处理异常

自定义异常类

package com.zheng.demo11;

public class MyException extends Exception {
    private int number;

    public MyException(int number) {
        this.number = number;
    }

    @Override
    public String toString() {
        return "MyException{" +
                "number=" + number +
                '}';
    }
}

通过向调用者抛出异常,让调用者处理异常信息

package com.zheng.demo11;

public class Test {

    //可能存在异常的方法
    public void show(int a) throws MyException {
        if (a > 5) {
            throw new MyException(a);
        }
        System.out.println("ok");
    }

    public static void main(String[] args) {
        try {
            new Test().show(1);
        } catch (MyException e) {
            System.out.println(e);
        }
    }


}

在这里插入图片描述

在方法内处理异常

package com.zheng.demo11;

public class Test {

    //可能存在异常的方法
    public void show(int a) {
        if (a > 5) {
            try {
                throw new MyException(a);
            } catch (MyException e) {
                System.out.println("在方法内处理异常");
                e.printStackTrace();
            }
        }
        System.out.println("ok");
    }

    public static void main(String[] args) {
        new Test().show(9);

    }


}

在这里插入图片描述

posted on 2022-08-28 22:20  热爱技术的小郑  阅读(21)  评论(0编辑  收藏  举报