栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java基础更新

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java基础更新

java基础(6.20-6.27) MarkDown语法 标题

#->######一级标题到六级标题,从大到小

字体

Hello,World!

字的两边加**,就是粗体

Hello,World!

字的两边加*,就是斜体

Hello,World!

斜体加粗,两边加上***

Hello,World!

字的两边加~~就是废弃

图片

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LUl31lwv-1656236253120)(图片路径)]

超链接

点击跳转到博客

跳转名字

列表
  1. a

  2. b

    1.加空格表示有序列表

  • a

    -加空格就是无序列表

表格
名字性别生日
张三男1997.7
idea的使用 快捷键

1.psvm 2.sout

注释
1. 单行注释(注释一行文字)

//+………

2. 多行注释(注释多行文字)

/……./

3. 文档注释(javaDoc)

标识符

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FGLUxGxY-1656236253121)(file:///C:Users惠普DocumentsTencent Files3025731614ImageC2CImage13B269A162440D573E08954849DABF145.png)]

数据类型 强类型语言

变量的使用要严格符合规定,变量都必须先定义再使用(安全性高,速度相对慢)

Java的数据类型 1.基本类型

数值类型

八种数据类型:byte,short,int,long,float,double,char,boolean

​ long类型要在数字后面加一个L

​ float类型要在数字后面加一个F

long num1=10L;
float num5=50.1F; 

Boolean类型

2.引用类型

类,接口,数组

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xb2TZxTC-1656236253121)(C:Users惠普AppDataRoamingTyporatypora-user-imagesimage-20220620210558114.png)]

public static void main(String[] args) {
        String a="10";
        System.out.println(a);
        //整数拓展 进制,二进制0b 十进制 八进制0 十六进制0x;
        int i=10;
        int i2=010;//八进制0
        int i3=0x10;//十六进制0x
        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("--------------------");
        //浮点数拓展:有限的 离散的 舍入误差 接近不等于,因此最好完全使用浮点数去进行比较
        //银行业务使用 BigDecimal 数学工具类
        float f=0.1f;
        double d=1.0/10;

        System.out.println(f==d);//输出false
        float d1=2313231211112222222f;
        float d2=d1+1;
        System.out.println(d1==d2);//输出true
        System.out.println("--------------------");
        //字符拓展:所有字符本质还是数字
        //编码 Unicode 2字节 0-65536
        //U0000 UFFFF
        char c1='a';
        char c2='中';
        System.out.println(c1);
        System.out.println((int)c1);//强制转换
        System.out.println(c2);
        System.out.println((int)c2);//强制转换
        char c3='u0071';
        System.out.println(c3);

        //转义字符
        //t,n
        System.out.println("你好n");
        System.out.println("hellotworld");
    }
类型转换

由于Java是强类型语言,运算中不同数据类型先转换为同一类型,然后计算

public static void main(String[] args) {
        int i=128;
        //强度高到强度低需要强制转换
        byte k=(byte)i;//强制转换,但内存溢出需要避免
        //强度低到高自动转换
        double j=i;
        System.out.println(i);
        System.out.println(k);
        System.out.println(j);
        //输出:128  -128 128.0   
    }
包机制

作用:区别类名的命名空间
一般利用公司域名倒置作为包名

javaDoc生成文档

使用idea生成Javadoc文档 引用

//加在类上面就是类的注释
public class javaDoc {
    
    //加在方法上,就是方法的注释
    public String test(String name) throws Exception{
        return name;
    }
}

java流程控制 用户交互Scanner

作用:获取用户输入

  • next():不能得到带有空格的字符串
  • nextline():以Enter为结束符号,返回回车之前的所有字符,包括空白
 public static void main(String[] args) {
        //创建一个扫描器的对象,用于接受键盘的数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收");
        //判断用户是够输入字符串
        if(scanner.hasNext()){
            String str=scanner.next();//使用next接收,程序等待用户输入完毕
            System.out.println("输出内容为"+str);
        }
        scanner.close();//凡是IO流的类使用完,记得关闭,避免一直占用资源
    }

-------------------
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入");
        String str=scanner.nextLine();
        System.out.println("输出的内容为"+str);
        scanner.close();
    }
Scanner进阶
 public static void main(String[] args) {
        //输入多个数字,求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果
        Scanner scanner = new Scanner(System.in);

        //和
        double sum=0;
        //计算输入多少个数字
        int m=0;
        //通过循环判断还有多少输入,并在里面对每一次进行求和统计
        while(scanner.hasNextDouble()){//判断是否有输入
            double x=scanner.nextDouble();
            sum+=x;
            m++;
        }
        System.out.println(m+"个数的和为"+sum);
        System.out.println("个数的平均值为"+sum/m);
        scanner.close();
    }
switch多选择

switch case语句判断一个变量与一系列值中的每个值是否想等,每个值称为一个分支

public static void main(String[] args) {
        char grade='c';//7支持字符串的比较
        switch (grade){
            case 'a':
                System.out.println("优秀");
                break;//一定要加break,否则匹配选项后面的值也会输出
            case 'b':
                System.out.println("良好");
                break;
            case 'c':
                System.out.println("及格");
                break;
            case 'd':
                System.out.println("不及格");
                break;
            default://没有一个符合要求
                System.out.println("未知等级");
        }
增强for循环
public static void main(String[] args) {
        //for循环
        int[] num={10,20,30,40,50};//定义了一个数组
        for(int i=0;i<5;i++){
            System.out.println(num[i]);
        }
        System.out.println("-----------");
        //遍历数组元素
        //增强for循环,简化作用
        for(int x:num){
            System.out.println(x);
        }
    }
水仙花
public static void main(String[] args) {
        //打印三角形,5行,先打空格再打*
        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
                System.out.print("*");
            }
            System.out.println();
        }
    }
//输出
//     *
//    ***
//   *****
//  *******
// *********
java方法 什么是方法

方法是语句的集合,他们在一起执行一个功能

  • 方法是解决一类问题的步骤的有序组合
  • 方法包含于类或对象中
  • 方法在程序中被创建,在其他地方被引用

方法的命名规则:首字母小写,后面是驼峰命名规则

设计方法的原则:一个方法只能完成一个功能,这样有利于后期的拓展

方法的定义和调用

java的方法类似于其他语言的函数,是一段用来完成特定功能的代码片段

定义一个方法

  • 方法头和方法体
  • 修饰符
  • 返回值类型
  • 方法名
  • 参数类型
  • 方法体
//main方法
    public static void main(String[] args) {
        int sum=add(1,2);
        System.out.println(sum);
    }

    //加法
    public static int add(int a,int b){//需要加static,主函数才能调用
        return a+b;
    }
方法的重载

方法名称相同时,编译器会根据调用方法的参数个数,参数类型去逐个匹配,来选择对应的方法,如果匹配失效则编译器报错

  • 方法的名称必须相同
  • 参数列表必须不同(个数不同,或者类型不同,参数排列顺序不同)
  • 方法的返回类型可以相同也可以不相同
  • 仅仅返回类型不同不足以称为方法重载
 //main方法
    public static void main(String[] args) {
        int sum=add(1,2);
        double a=add(1.0,2.0);
        System.out.println(sum);
        System.out.println(a);
    }

    //加法
    public static int add(int a,int b){//需要加static,主函数才能调用
        return a+b;
    }
//方法的重载
    public static double add(double a,double b){//需要加static,主函数才能调用
        return a+b;
    }
命令行传参

有时候希望运行一个程序的时候再传递给它消息,这需要传递命令行参数给main()函数实现

可变参数

不定项参数,一个方法只能指定一个可变参数,它必须是方法的最后一个参数。任何一个普通的参数必须在它之前申明

方法申明中,在指定参数类型后加一个省略号

递归

自己调用自己

  • 递归头:什么时候不调用递归(结束)
  • 递归体:什么时候需要调用自生方法(开始)
public static void main(String[] args) {
        System.out.println(f(5));
    }
    public static int f(int n){
        //边界
        if(n==1){
            return 1;
        }
        //递归调用
        return n*f(n-1);
    }
//5的阶乘
Java数组 数组的定义:相同类型数据的有序集合

数组的声明:

  public static void main(String[] args) {
        int[] nums;//1.定义
        nums=new int[10];//2.表示可以存放10个int类型的数字就是创建一个数组
        //3.给数组元素赋值
        for(int i=0;i
            nums[i]=i+1;
        }
        //输出
        for(int x:nums){
            System.out.println(x);
        }
    }

二维数组

public static void main(String[] args) {
        int[][] array={{1,2},{2,3},{3,4}};
        System.out.println(array[0]);//输出地址
        System.out.println(array[0][1]);//输出2
        printArray(array[0]);//输出1,2
    }
    //打印数组元素
    public static void printArray(int[] arrays){
        for(int i=0;i
            System.out.print(arrays[i]+" ");
        }
        System.out.println();
    }
arrays工具类

java提供的数组工具类

 public static void main(String[] args) {
        int[] a={19,1,2,8,3,7,32};
        System.out.println(Arrays.toString(a));//输出
       Arrays.sort(a);//排序
        System.out.println(Arrays.toString(a));
        //填充
        Arrays.fill(a,0);
        System.out.println(Arrays.toString(a));
    }
输出
[19, 1, 2, 8, 3, 7, 32]
[1, 2, 3, 7, 8, 19, 32]
[0, 0, 0, 0, 0, 0, 0]
冒泡排序

思想:每一次比较都会产生一个最大的数字或者最小的数字

 public static void main(String[] args) {
        int[] a={1,9,2,8,3,7,6};
        int[] s = sort(a);
        System.out.println(Arrays.toString(s));
    }
    public static int[] sort(int [] array){
        int temp=0;
        for(int j=0;j//外层循环控制次数
            for (int i = 0; i < array.length - 1 - j; i++) {
                if (array[i]>array[i+1]) {
                    temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                }
            }
        }
        return array;
    }
输出
    [1, 2, 3, 6, 7, 8, 9]
稀疏矩阵的压缩存储
 public static void main(String[] args) {
        //创建一个二维数组11*11 0表示没有棋子,1表示黑棋,2表示白棋
        int[][] arr=new int[11][11];
        arr[1][2]=1;
        arr[2][3]=2;
        //输出原矩阵
        System.out.println("输出原来矩阵的值");
        for(int[] array:arr){
            for(int x:array){
                System.out.print(x+" ");
            }
            System.out.println();
        }
        System.out.println("================================");
        //转换为稀疏数组保存
        //1.获取有效值的个数
        int sum=0;
        for(int i=0;i<11;i++)
            for(int j=0;j<11;j++)
                    if(arr[i][j]!=0)
                        sum++;
        System.out.println("有效值的数量"+sum);
        //创建稀疏矩阵
        int[][] arr2=new int[sum+1][3];
        arr2[0][0]=11;
        arr2[0][1]=11;
        arr2[0][2]=sum;
        //遍历二维数组,将非0的值存放到稀疏数组
        int count=0;
        for(int i=0;i
            for(int j=0;j
                if(arr[i][j]!=0){
                    count++;
                    arr2[count][0]=i;//存放行坐标
                    arr2[count][1]=j;//存放列坐标
                    arr2[count][2]=arr[i][j];//存放值
                }
            }
        }
        //输出稀疏矩阵
        for(int i=0;i
            System.out.println(arr2[i][0]+"t"+arr2[i][1]+"t"+arr2[i][2]);

        }
        System.out.println("根据稀疏数组还原到原数组=================================");
        //1.读取稀疏数组
        int[][] arr3=new int[arr2[0][0]][arr2[0][1]];//来决定这个数组需要开辟的空间是多少
        //2还原他的值
        for(int i=1;i
            for(int y:x){
                System.out.print(y+" ");
            }
            System.out.println();
        }
    }

静态方法
dem001类
public class demo01 {
    public static void main(String[] args) {
        //其他类的非静态方法可以通过类名+‘.’的方式调用
        Student.say();
        //如果是非静态方法,就需要实例化这个类
        Student student = new Student();
        student.shout();
    }
}
学生类
public class Student {
    //静态方法
    public static void say(){
        System.out.println("学生说话");
    }
    //非静态方法
    public void shout(){
        System.out.println("学生呐喊");
    }
}
面向对象(oop)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z45cOMEV-1656236253122)(C:Users惠普AppDataRoamingTyporatypora-user-imagesimage-20220623210703967.png)]

创建与初始化对象

使用new关键字创建对象

除了分配内存空间之外,还会给创建好的对象进行初始化,以及对类中的构造器的调用

构造器(构造方法)

在进行创建对象的时候必须调用的

构造器的两个特点

  • 和类的名字相同的
  • 没有返回值类型

注意点

  • new本质上是在调用构造函数
  • 存在有参构造,那么无参构造就要显示表示
  • 用来初始化值
//学生类
public class Student {
    //属性
    String name;
    int age;
    //方法
    public void say(){
        System.out.println(this.name+"在说话");
    }
}
//一个项目应该只存在一个main方法
public class Application {
    //类是抽象的,实例化之后会返回一个自己的对象
    public static void main(String[] args) {
        Student zhangsan = new Student();
        zhangsan.name="zhangsan";
        zhangsan.age=11;
        System.out.println(zhangsan.name);
        System.out.println(zhangsan.age);
        Student xiaoming = new Student();
        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);
    }
}
输出:
    zhangsan
	11
	null
	0

封装

属性私有,get/set(可以做一些安全性的验证)

作用:

  • 提高程序的安全性,保护数据
  • 隐藏代码的实现细节
  • 统一接口
  • 增加了系统的可维护性
public class Student {
    //私有属性
    private String name;
    private int age;
    //使用get方法获取
    public String getName() {
        return name;
    }
    //使用set方法设置
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.setAge(11);
        student.setName("张三");
        System.out.println(student.getAge());
        System.out.println(student.getName());
    }
}

继承
  • 关键字:extends,子类是父类的扩展
  • 子类和父类之间具有“is a”的关系
  • 子类继承父类就会拥有父类的全部方法
  • java中,所有类都直接或者间接继承Object类
  • java中只有单继承没有多继承,一个儿子只能有一个爸爸,但是一个爸爸可以有多个儿子
  • 私有属性无法被继承
public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();
    }
}

public class Student extends Person{

}

public class Person {
    public void say(){
        System.out.println("大声说话");
    }
}

super关键字

super的注意点:

  • super调用父类的构造方法必须在构造方法的第一个
  • super必须只能出现在子类的方法或者构造方法中
  • super和this不能同时调用构造方法

super和this的qubie

代表对象不同

  • this调用当前对象
  • super代表父类对象的应用

前提:

  • this:没有继承也可以使用
  • super:只能在继承条件下才可以使用

构造方法

  • this()本类的构造
  • super()父类的构造
public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.test1("aaa");
        student.test2();
    }
}

public class Person {
    protected String name="张三";

    public Person() {
        //默认先调用父类构造方法
        System.out.println("调用了Person类的无参构造");
    }

    public void print(){
        System.out.println("张三");
    }

}
public class Student extends Person{
    private String name="zhangsan";

    public Student() {
        System.out.println("调用了Student的无参构造");
    }

    public void print(){
        System.out.println("zhangsan");
    }
    public void test1(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }
    public void test2(){
        this.print();
        super.print();
    }
}
输出:
调用了Person类的无参构造
调用了Student的无参构造
aaa
zhangsan
张三
zhangsan
张三
方法的重写

需要有继承关系,子类重写父类的方法

  • 方法名称必须要相同
  • 参数列表必须相同
  • 修饰符:范围可以扩大但是不能缩小,public->protected->default->private
  • 抛出的异常:范围,可以被缩小,但是不能被扩大;ClassNotFoundException(大)

重写,子类的方法和父类必须一致,方法体不同

为什么需要重写:

1.父类的功能,子类不一定需要,或者不一定满足

Alt+Insert;Override

public class Application {
    //静态方法和非静态方法有区别
    静态方法:方法的调用只和左边定义的数据类型有关
    非静态方法:重写
    public static void main(String[] args) {
        Student student = new Student();
        student.print();
        //父类的引用指向子类,子类重写了父类的方法
        Person person=new Student();
        person.print();
    }
}

public class Student extends Person{
    @Override//重写
    public void print() {
        System.out.println("Student");
    }
}

public class Person {
    //重写都是方法的重写和属性无关
    public void print(){
        System.out.println("Person");
    }
}
多态

多态的注意事项

  1. 多态是方法的多态,属性是没有多态的
  2. 父类和子类有联系,类型转换异常 ClassCastException
  3. 存在条件:继承关系,方法需要重写,父类的引用指向子类的对象Father f1=new Son()

不能重写的情况

  • static方法,属于类,不属于实例
  • final常量
  • private方法
public class Application {
    public static void main(String[] args) {
        //student能调用的方法都是自己的或者继承父类的
        Student student = new Student();
        //父类可以指向子类但是不能调用子类独有的方法
        Person person=new Student();
        //对象能执行哪些方法,主要看对象左边的类型,和右边的关系不大
        student.run();
        person.run();
        student.say();
        ((Student)person).say();
    }
}

public class Person {
    public void run(){
        System.out.println("Person run");
    }
}

public class Student extends Person{
    @Override
    public void run() {
        System.out.println("Student Run");
    }
    public void say(){
        System.out.println("student say");
    }
}

static
public class Student {
    private static double score;
    public int age;
    public void go(){
        System.out.println("go");
    }
    public static void run(){
        System.out.println("run");
    }
    public static void main(String[] args) {
        Student student = new Student();
        //实例化调用非静态属性
        System.out.println(student.age);
        //通过类名调用静态属性
        System.out.println(Student.score);
        student.go();
        Student.run();
    }
}
public class Person {
    //2.赋初值
    {
        System.out.println("匿名代码块");
    }
    //1.只执行一次
    static{
        System.out.println("静态代码块");
    }
    //3.
    Person(){
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Person person = new Person();
        Person person1 = new Person();
    }
}
输出:
静态代码块
匿名代码块
构造方法
匿名代码块
构造方法
抽象类
//abstract
//抽象方法:只有方法的名字,没有方法的实现
//抽象类不能new,只能子类实现它,抽象类可以写普通方法,抽象方法所在的类必须是抽象类
//提高开发效率
public abstract class action {
    public abstract void doSomething();
}

//继承了抽象类的子类,必须重写抽象类中所有的方法,除非子类也是抽象类,类是单继承,接口可以多继承
public class A extends action{
    @Override
    public void doSomething() {

    }
}

接口:

接口是一种规范,interface

  • 是一种规范
  • 定义一些方法,让不同的人去实现
  • 默认public abstract
  • 接口不能被实例化,接口中没有构造方法
  • 通过implements可以实现多个接口
  • 一个普通类实现接口就一定要重写接口中定义的所有方法
//类通过implements关键字实现接口
//需要重写接口中定义的所有方法
public class UserServiceImpl implements UserService,TimeService{
    @Override
    public void run() {

    }

    @Override
    public void time() {

    }
}

public interface TimeService {
    void time();
}

public interface UserService {
    //接口中默认所有定义都是抽象的,接口需要实现类
     void run();
}

内部类

在一个类中再定义一个类,例如A类中定义了B类那么B类就称为A类的内部类

public class Outer {
    public int id=10;
    public void outer(){
        System.out.println("外部类");
    }
    class inner{
        public void in(){
            System.out.println("内部类");
        }

        //获得外部类的私有属性
        public void getId(){
            System.out.println(id);
        }
    }
}

public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();
        //通过外部类来实例化内部类
        Outer.inner inner = outer.new inner();
        inner.getId();
    }
}
异常机制

程序出现了非预期的情况

总结:

  • 处理运行异常时,采用逻辑去合理规避同时辅助try-catch处理
  • 在多重catch块后面,加一个cath(Exception)来处理可能被遗忘的异常
  • 对于不确定的代码可以加try-catch处理潜在的异常
  • 尽量去处理异常
  • 处理异常需要具体问题具体分析
  • 尽量添加finally语句释放占用的资源

快捷键:选中ctrl +alt +T

关键字:try,catch,finally,throw,throws

public class Test {
    public static void main(String[] args) {
        int a=1;
        int b=0;
        
    //如果要捕获多个异常,应该从小到大的顺序
        try {//try监控区域
            System.out.println(a/b);
        }catch (ArithmeticException e){//catch(想要捕获的异常类型)捕获异常
            System.out.println("b不能为0");
        }finally {//处理善后工作,总共会出现
            System.out.println("finally");
        }
    }
}

自定义的异常

public class Test {
    public static void test(int a) throws myException {
        System.out.println("传递的参数"+a);
        if(a>10){
            throw new myException();//抛出
        }
        System.out.println("OK");
    }
    public static void main(String[] args) {
        try {
            test(1);
        } catch (myException e) {
            e.printStackTrace();
        }
    }
}

public class Test {
    public static void main(String[] args) {
        //没有名字初始化类,不需要将实例保存到变量中
        new Apple().eat();
    }
}
class Apple{
    public void eat(){
        System.out.println("eat apple");
    }
}

语句释放占用的资源

快捷键:选中ctrl +alt +T

关键字:try,catch,finally,throw,throws

public class Test {
    public static void main(String[] args) {
        int a=1;
        int b=0;
        
    //如果要捕获多个异常,应该从小到大的顺序
        try {//try监控区域
            System.out.println(a/b);
        }catch (ArithmeticException e){//catch(想要捕获的异常类型)捕获异常
            System.out.println("b不能为0");
        }finally {//处理善后工作,总共会出现
            System.out.println("finally");
        }
    }
}

自定义的异常

public class Test {
    public static void test(int a) throws myException {
        System.out.println("传递的参数"+a);
        if(a>10){
            throw new myException();//抛出
        }
        System.out.println("OK");
    }
    public static void main(String[] args) {
        try {
            test(1);
        } catch (myException e) {
            e.printStackTrace();
        }
    }
}

public class Test {
    public static void main(String[] args) {
        //没有名字初始化类,不需要将实例保存到变量中
        new Apple().eat();
    }
}
class Apple{
    public void eat(){
        System.out.println("eat apple");
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995336.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号