JavaSE 基础语法、流程控制和方法、数组、面向对象、异常

JavaSE

计算机基础

Java是编程语言

计算机computer 硬件和软件组成 硬件及冯诺伊曼体系结构

计算机软件:系统软件和应用软件 【IntelliJ IDEA】

Windows常用快捷键

功能键and快捷键 ctrl+c=复制 ctrl+v=粘粘 ctrl+a=全选 ctrl+x=剪切 ctrl+z=撤销 ctrl+s=保存 ctrl+f4=关闭

基本DOS命令 开始搜索:命令提示符 # 盘符切换 #查看当前目录下所有文件dir #切换目录 cd change directory #清理屏幕 cls #退出终端 exit #查看电脑Ip ipconfig #打开应用 calc mspaint notepad 计算机语言发展史:计算方式二进制

Java01:帝国诞生1972C诞生 1982C++诞生 Java2标准版(J2SE) 移动版(J2ME) 企业版(J2EE)

Java02:特性和优势 简单性 面向对象 可移植性 高性能 分布式 动态性 多线程 安全性 健壮性 Java03:三大版本 Java SE:标准版 基础核心 Java ME:嵌入式开发只了解 Java EE:E企业级开发

Java04:JDK:Java Development Kit JRE:Java Runtime Environment JVM:Java Virtual Machine

Java05:安装开发环境 JDK下载安装 配置环境变量 Hello world Notepad++安装使用 Java06:Helloworld详解 建立文件夹 然后添加文件用notepad++ 名称Hello.java 显示Java文件类型才可以

    public static void main(String[] args){   
        System.out.print("Hello,World!");    
    }
}

Java07:编译型和解释型 先编译到操作系统平台才会解释 源程序>Java编译器>字节码>>类装载器>字节码校验器>解释器>操作系统平台

Java08:IDEA安装 官网 JetBrains: Essential tools for software developers and teams IDEA 集成开发环境

博客的重要性

博客的重要:

总结和思考 CSDN QQ注册 博客源推荐申请学习 WCRCP typecho Markdown语法 文本编辑器typora

Java基础语法

Java01基础语法

注释三种

1 单行注释 后跟代码  2 多行注释 注释   3 文档注释 在建立idea java后 psvm sout回车弹出代码

Java02:标识符 

Abstract assert boolean break byte Case catch char class const Continue default do double else Enum extends final finally float For goto if implements import Instanceof int interface long native New package private protected public Return strictfp short static super Switch synchronized this throw throws Transient try void volatile while

所有标识符应该以字母(A-Z或者a-z),美元符($) 下划线()开始 首字符之后(A-Z或者a-z),美元符($) 下划线() 还有数字

Java03:数据类型 基本类型和引用类型

数据类型

public class Demo03 {
    public static void main(String[] args) {
        String a ="hello";
        int num = 10;
        System.out.println(a);
        System.out.println(num);

    }
}

执行结果:

hello 10

Process finished with exit code 0

public class Demo03 {
    public static void main(String[] args) {
        //八大数据基本类型

        //整数
        int num1 =10;  //最常用
        byte num2=20;
        short num3=30;
        long num4=30L;  //long类型要在数字后面加个L

        //小数:浮点数
        float num5=50.1f;
        double num6=3.141592653589793238462643;

        //字符
        char name ='国';
        //字符串,String不是关键字,类
        //String namea = "阿拉璨";

        //布尔值 是非 占一位
        boolean flag =true;
        boolean flag =false;

引用数据类型 类 接口 数组

Java04:数据类型扩展及面试题讲解

public class Demo03 {
    public static void main(String[] args) {
        //整数扩展 进制 二进制 十进制 八进制 十六进制
        int i =10;
        int i2=010;//八进制
        int i3=0x10; //十六进制          0~9   A~F   16
        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);     //快捷键(sout)

        //浮点数
        //float;有限 离散 舍入误差 大约 接近但不等于
        //double;
        //最好完全使用浮点数进行比较
        //BigDecimal  数学工具类

        float f=0.1f;
        double d=1.0/10;//0.1

        System.out.println(f==d); //false
        float d1=23131312312312313f;
        float d2=d1 + 1;

        System.out.println(d1=d2);//true

        //字符扩展
        char c1='a';
        char c2='中';
        System.out.println(c1);
        System.out.println((int)c1);//强制执行
        System.out.println(c2);
        System.out.println((int)c2);//强制执行
        //所有字符本质还是数字
        //编码  Unicode  表:97=A   2字节    65536  Excel   2 16 = 65536
        // U0000  Uffff
        char  c3 = '\u0061’ ;
        System.out.println(c3);//a
        //转义字符
        //\t  制表符
        //\n  换行
        //……
        System.out.println("Hello\nWorld");
        System.out.println("=============================================");
        String sa =new String(original:"hello world");
        String sb =new String(original:"hello world“);
        System.out.println(sa=sb);

        String sc ="hello world";
        String sd ="hello world";
        System.out.println(sc=sd);

        //对象 从内存分析
        
        //布尔值扩展
        boolean flag = true;
        if (flag==true){} //新手
        if (flag){}//老手
        //Less is More ! 代码要精简易读

}

Java05:类型转换 强制类型转换 自动类型转换

低- - - - - - - - - - - - - - - - - - ->高

byte,short,char->int->long->float->double

public class Demo03 {
    public static void main(String[] args) {
        int  i  = 128;
        byte b =(byte) i;
        //内存溢出128
        //强制转换  (类型)变量名   高- - - - >低
        //自动转换   低- - - - ->高
        System.out.println(i);
        System.out.println(b);
        /*1 不能对布尔值转换
        2 不能把对象类型转换为不相干的类型
        3 再把高容量转换到低容量的时候,强制转换
        4转换的时候可能存在内存溢出,或者精度问题
        
        */

        System.out.println(“==============”);
        System.out.println((int)23.7) ;//23
        System.out.println((int)-45.89f);  //-45   


        System.out.println(“==================”);
        char  c = 'a';
        int   d = c+1;
        System.out.println(d);
        System.out.println(d);  //强转 ((char)d )
    }
}    
public class Demo03{
            public static  void main(String[] args){
                //操作比较大的数的时候,注意溢出问题
                //JDK7新特性,数字之间可以使用下划线分割
                int money = 10_0000_0000;
                int years = 20;
                int total = money*years;   //-1474836480,计算的时候溢出了
                long total2 = money*years; //默认是int,转换之前已经存在问题了;

                long total3 = money*((long)years); //先把一个数转换成long
                System.out.println(total3);
            }
}    

Java基础06:变量、常量、作用域

变量:可以变化的量

type varName [=value] [{,varName[=value]}];
//数据类型    变量名 = 值; 可以使用逗号隔开来声明多个同类型变量
// 变量作用域  类变量  实例变量  局部变量
public class Variable{
    Static int allClicks=0;     //类变量
    String str = "hello world"; //实例变量

    public void method(){
        int  i =0;  //局部变量
    }
}
public class Demo03{
            public static  void main(String[] args){
                
                    //类变量 static
                    static double salary = 2500;
                    //属性:变量

                    //实例变量:从属于对象;如果不自行初始化,这个类型默认值0 0.0
                    //布尔值: 默认是false
                    //除了基本类型,其余默认值都是null;
                    String name;
                    int age;

                    //main方法
                    public static  void main(String[] args){

                        //局部变量; 必须声明和初始化值
                        int i= 10;
                        System.out.println(i);

                        //变量类型 变量名字 = new Demo03();
                        Demo03 demo03 = new Demo03();
                        System.out.println(demo03.age);
                        System.out.println(demo03.name);
                        //类变量 static
                        System.out.println(salary);

                    }

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

                    }
                }
//常量:不会变动的值 常量名一般用大写字符
final 常量名=值;
final double PI=3.14;
public class Demo03{
    public static void main(String[] args) {
        //修饰符,不存在先后顺序
        static final double PI = 3.14;
        public static void main (String[]args){
            System.out.println(PI);
        }
    }    

Java07基本运算符

变量的命名规范
所有变量、方法、类名:见名知意
类成员变量:首字母大小写和驼峰原则:monthSalary除第一个单词以外,后面的单词首字母大写lastname  lastName
局部变量:首字母小写和驼峰原则
常量:大写字母和下划线:MAX_VALUE
类名:首字母大写和驼峰原则:Man,GoodMan
方法名:首字母小写和驼峰原则:run(),runRun()
运算符 Java支持
算术运算符:+,   - ,  * ,  /,  %,  ++,  - -,
赋值运算符: =
关系运算符:>,  <,  >=,  <=,  ==,   !=instanceof
逻辑运算符: &&,  ||,  !
位运算符:  |, ^,  ~,      >>,   <<,  >>>
条件运算符:?
扩展运算符: +=,  -=,*=,/=

详解

Java中的基本运算符主要分为以下几个类别,它们各自具有特定的作用,用于执行不同的操作:

  1. 算术运算符

作用:执行基本的数学运算。

包括:

• +:加法。用于数值或字符串的相加。 • -:减法。用于数值之间的减法。 • *:乘法。用于数值的乘积计算。 • /:除法。用于数值的除法运算。注意,整数相除时结果是整数(向下取整),除非其中一个操作数是浮点数。 • %:取模(求余)。返回两个整数相除后的余数。 • ++:自增。放在变量前(前置自增)或后(后置自增)表示变量值增加1。前置自增会先改变变量值再参与表达式计算,后置自增则先参与表达式计算再改变变量值。 • --:自减。类似于自增,只是使变量值减少1。

2.关系运算符

作用:比较两个操作数的大小关系或是否相等,返回布尔值(true 或 false)。

包括:

• ==:等于。检查两个操作数的值是否相等。 • !=:不等于。检查两个操作数的值是否不相等。 • <:小于。判断左操作数是否小于右操作数。 • >:大于。判断左操作数是否大于右操作数。 • <=:小于等于。判断左操作数是否小于等于右操作数。 • >=:大于等于。判断左操作数是否大于等于右操作数。

3.逻辑运算符

作用:用于连接或组合布尔表达式,返回复合的布尔结果。

包括:

• &&(逻辑与):当且仅当两个操作数都为 true 时,整个表达式结果才为 true。否则为 false。具有短路性质,即如果左侧表达式为 false,则右侧表达式无需计算。 • ||(逻辑或):只要有一个操作数为 true,整个表达式结果就为 true。否则为 false。同样具有短路性质,如果左侧表达式为 true,则右侧表达式无需计算。 • !(逻辑非):对单个布尔操作数取反,即 true 变为 false,false 变为 true。

4.赋值运算符

作用:将一个值赋给变量。

作用:将一个值赋给变量。

包括:

• =:基本赋值运算符,将右边的值赋给左边的变量。 • +=, -=,*=,/=,%=:扩展赋值运算符,分别对应于加法、减法、乘法、除法、取模的复合赋值形式。它们将右边的值与左边变量的当前值进行相应算术运算后,将结果赋回给该变量。

5.位运算符

作用:直接对整型数据的二进制位进行操作。

包括:

• &:按位与。对两个操作数的每个对应位进行逻辑与运算。 • |:按位或。对两个操作数的每个对应位进行逻辑或运算。 • ^:按位异或。对两个操作数的每个对应位进行逻辑异或运算。 • ~:按位取反(补码)。对一个操作数的所有位进行逻辑非运算。 • <<:左移。将操作数的二进制表示向左移动指定的位数,右侧空出的位补零。 • >>:右移(算术右移)。将操作数的二进制表示向右移动指定的位数,对于有符号数,左侧空出的位根据原最高位(符号位)补相同值(0或1)。 • >>>:无符号右移。将操作数的二进制表示向右移动指定的位数,无论原数是有符号还是无符号,左侧空出的位一律补零。

package operator;

public class Demo03{
    public static void main(String[] args){
        //二元运算符 算术运算符
        // Ctrl + D:复制当前行到下一行
        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/(double)b);
    }
}

赋值运算符:

public class Demo03{
    public static void main(String[] args){
        long a = 123123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;
        System.out.println(a+b+c+d); //long
        System.out.println(b+c+d); //int
        System.out.println((double)c+d); //int
        
    }
}

关系运算符:

public class Demo03{
    public static void main(String[] args){
        //关系运算符返回的结果: 正确,错误    布尔值

        int a = 10;
        int b = 20;
        int c = 21;
        
        //取余,模运算
        System.out.println(c%a);  // c / a   21/10=2……1    /=➗     ……余
        
        System.out.println(a>b);  //大于
        System.out.println(a<b);  //小于
        System.out.println(a==b);  //等于
        System.out.println(a!=b);  //不等于
    }
}

Java08:自增自减运算符

public class Demo03{
    public static void main(String[] args){
        //++   --   自增,自减    一元运算符
        int a =3;
        
        int b =a++; //a  a=a+1;  
        // 执行完这行代码后,先给b赋值,在自增

        System.out.println(a);
        int c =++a;//a a=a+1;
        //执行完这个代码前,先给自增,再给b赋值

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        
        //幂运算  2^3  2*2*2=8  很多运算我们会使用一些工具类来操作
        double pow = Math.pow(3,2);
        System.out.println(pow);
    }
}

Java基本09:逻辑运算符和位运算符

package operator;
//逻辑运算符
public class Demo03{
    public static void main(String[] args){
        //与(and)或(or) 非(取反)
        boolean a = true;
        boolean b = false;

        System.out.println("a && b:"+(a&&b));//逻辑与运算:两个变量都为真,则结果才为true
        System.out.println("a || b:"+(a||b));//逻辑或运算;两个变量有一个为真,则结果为true
        System.out.println("!(a && b):"+!(a&&b));//如果是真,则变为假,如果是假则变为真
        
        //短路运算
        int c = 5;
        boolean d =((c<4)&&(c++<4));
        System.out.println(d);
        System.out.println(c);
        
    }
}
package operator;

public class Demo03{
    public static void main(String[] args){
       /*
       * a =0011 1100
       * b =0000 1101
       *
       * A&b = 0000 1100
       * A/B = 0011 1101
       * A^B = 0011 0001
       * ~B  = 1111 0010
       *
       * 2*8=16   2*2*2*2
       * 效率极高!!!
       * <<    *2
       * >>    /2
       *
       * 0000    0000        0
       * 0000    0001        1
       * 0000    0010        2
       * 0000    0011        3
       * 0000    0100        4
       * 0000    1000        8
       * 0001    0000        16
       * */

        System.out.println(2<<3);
    }

Java基础10:三元运算符及小节

package operator;

public class Demo03{
    public static void main(String[] args){
        int a =10;
        int b =20;
        
        a+=b;  //a = a+b
        a-=b;  //a = a-b

        System.out.println(a);
        
        
        //字符串连接符   + ,String
        System.out.println(a+b);
        System.out.println(""+a+b );

        System.out.println(a+b+"");
       
    }
}
package operator;

//三元运算符
public class Demo03{
    public static void main(String[] args){
        //x ? y :z
        //如果x==true,则结果为y,否则为z

        int score = 80;
        String type =score < 60 ?"不及格":"及格";//必须会写
        //if
        System.out.println(type);
    }
}

扩展

优先级

运算符优先级是指在编程语言中,当一个表达式包含多个运算符时,编译器或解释器按照特定的规则来确定各运算步骤执行顺序的先后次序。不同的编程语言可能有不同的运算符优先级规定,这里主要概述C语言的运算符优先级:

C语言运算符优先级概览:

  1. 最高级: • 圆括号 ():用于强制改变运算顺序。 • 下标运算符 []:访问数组元素。 • 指向结构体成员运算符 ->:访问指针所指向结构体的成员。 • 结构体成员运算符 .:直接访问结构体或联合体的成员。

  2. 第二级: • 单目运算符: • 逻辑非 ! • 按位取反 ~ • 自增 ++、自减 -- • 正号 -(仅作为单目运算符使用时) • 类型转换 (类型) • 取地址 & • sizeof:获取变量或类型的大小

  3. 第三级: • 条件运算符(三目运算符) ?:

  4. 第四级: • 赋值运算符: • 简单赋值 = • 复合赋值(如 +=, -=, *=, /=, %= 等)

  5. 第五级: • 逗号运算符 ,:用于将多个表达式组合成一个复合表达式,从左到右依次计算各个子表达式。

  6. 第六级: • 按位或运算符 |

  7. 第七级: • 按位异或运算符 ^

  8. 第八级: • 按位与运算符 &

  9. 第九级: • 关系运算符: • 小于 < • 大于 > • 小于等于 <= • 大于等于 >=

  10. 第十级: • 相等性运算符: • 等于 == • 不等于 !=

注意以下几点:

• 结合性:对于同级别的运算符,如果它们相邻且操作数相同,还需要遵循一定的结合性来确定运算顺序。大部分二元运算符(如算术、逻辑和关系运算符)是左结合的,即从左到右进行结合。例如,a + b + c 中,会先计算 a + b 再加上 c。但有一些运算符如自增、自减(作为前置或后置形式)、赋值运算符和条件运算符是右结合的,即从右到左进行结合。 • 优先级与嵌套:高优先级的运算符先于低优先级的运算符进行计算。如果有括号存在,括号内的表达式会先于括号外的运算进行计算,不受括号外运算符优先级的影响。 • 特殊情况:上述列表基于常规的C语言运算符优先级,实际编程中还可能遇到特定的运算符或语法构造,其优先级和结合性可能与常规运算符有所不同。例如,某些C++特有的运算符(如重载运算符、特殊成员函数等)不在上述列表中。

总之,理解并熟练运用运算符优先级是编写正确、可读性强的C语言代码的关键之一。在处理复杂表达式时,如果对优先级不确定,可以使用括号来明确指定运算顺序,避免依赖默认优先级导致的潜在错误或混淆。

Java基础11:包机制

为了更好地组织类,Java提供了包机制,用于区别类名的命名空间

包:文件名 两个文件名重复了,重命名或者新建一个文件包搭入不同文件包

包语句语法格式为

package pkg1 [.pkg2[.pkg3…]];

一般利用公司域名倒置作为包名 www.alacan.come come.alacan.www

为了能够使某一个包的成员,我们需要在Java程序中明确导入该包。使用”import“完成此功能

import package1[.package2…].(classname|*);

IDEA >NEW>package>name: come.Alacan

IDEA改名字 Refoctor>Rename

Java基础12:JavaDoc生成文档

javadoc:命令是用来生成自己API文档的

jdk帮助文档 SE

Overview (Java Platform SE 8 ) (oracle.com)

参数信息 <>@author 作者名 //会写 <>@version 版本号 //会写 <>@since 指明需要最早使用的jdk版本 <>@param 参数名 <>@return 返回值情况 <>@throws 异常抛出情况

注解:/**回车

package com.Alacan;

/**
 * @author  Alacan
 * @version  1.0
 * @since 1.8
 */
public class Doc {

    String name;  //属性

    /**
     *
     * @param name
     * @return
     */
    public String test(String name){
        return name;  //方法
    }

    /**
     * @author  Alacan //随便填写
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name)throws Exception{
        return name;  //方法
    }

}

选中Doc> Open In 中 Explorer 从文件夹中打开 打开在文件夹里>通过命令行中操作>导航栏前面加CMD或者删除内容输入amd>可以属性调节字体大小也可以不调节

-encoding UTF-8 -charset UTF-8这中间参数为了能让它多显示中文 遇到乱码问题可以解决掉

Javadoc命令 -encoding UTF-8 -charset UTF-8去执行文件

查看文件夹会发现多了一堆看不懂的东西 其中有index-.html

//index意思是首页

点开发现跟官方代码一模一样什么都没有只有一个

Doc类点开发现是设置的1.8版本 后面所有的方法可以看到是我们自己写的

总之这个文档生成了 用我们命令行生成

总结:要练习 学会查找使用IDEA生产JavaDoc文档!

命令行:javadoc 参数 Java文件

//基础部分的一切知识,后面几乎每天都用

流程控制和方法

Java流程控制01:用户交互Scanner

java.until.Scanner 是Java5新特征 通过Scanner类来获取用户输入

基本语法

scanner s = new Scanner(System.in);

通过Scanner 类的 next()与 nextLine()方法获取输入的字符串,再读取前我们一般需要使用 hasNext()和hasNextLine()判断是还有输入的数据

next():

package com.Alacan.scanner;

import java.util.Scanner;

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

        //创建一个扫描对象,用于接受键盘数据
        Scanner scanner = new Scanner (System.in);
        System.out.println("使用next方式接收:");

        //判断用户有没有输入字符串
        if(scanner.hasNext()){
            //使用next方式接收
            String str = scanner.next();  //程序会等待用户输入完毕
            System.out.println("输出的内容为"+str);

        }

        //凡是属于Io流的类如果不关闭会一直占用资源,养成良好习惯用完就关
        scanner.close();

    }

}

执行输出:

nextLine:

package com.Alacan.scanner;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] arges){
        
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用nextLine方式接收:");
       //判断是否还有输入
        if(scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println("输出内容为:"+str);

        }


        scanner.close();
    }
}

执行输出结果:

两者对不输出结果发现

next()输出内容只输出hello

nextLine()输出内容hello world都输出了

Sanner对象

next()://空格后不输出

1:一定要读取到有效字符才可以结束输入

2:对输入有效字符前遇到的空白,next()方法会自动将其去掉

3:只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符

4:next()不能得到带有空格的字符串

nextLine()://敲回车前所有字符都可以输出

1:以enter作为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符

2:可以获得空白

next Line简写

package com.Alacan.scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入数据:");
        //判断是否还有输入
        if(scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println("输出内容为:"+str);
        }


        scanner.close();
        }

}

输出结果:

Java流程控制02:Scanner进阶使用

package com.Alacan.scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in); //固定格式


        //从键盘接收数据
        int i = 0;
        float f = 0.0f;

        System.out.println("请输入整数:");
        //if判断  如果……那么……    else否则
        if (scanner.hasNextInt()){
            i =  scanner.nextInt();
            System.out.println("整数数据:"+i);
        }else{
            System.out.println("输入的不是整数数据!");

        }


        System.out.println("请输入小数:");

        //if判断  如果……那么……    else否则
        if (scanner.hasNextFloat()){
            f =  scanner.nextFloat();
            System.out.println("小数数据:"+f);
        }else{
            System.out.println("输入的不是小数数据!");

        }


        scanner.close();

    }

}

执行结果正确情况:

错误的情况:

点击这个类 ctrl+单机鼠标 进入类的源码看一看里面到底有哪些方法

点击这个类 ctrl+单机鼠标 进入类的源码看一看里面到底有哪些方法

写一个案例

package com.Alacan.scanner;

import java.util.Scanner;//标准格式

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

            m = m+1;//m++

            sum = sum + x;
            
        }
        System.out.println(m+"个数和为"+sum);
        System.out.println(m+"个数的平均值是"+(sum / m));


        scanner.close();//标准格式
    }

}

执行输入结果:

输入添加下面这个句子

System.out.println("你输入了第"+m+"个数据,然后当前结果sum="+sum);
package com.Alacan.scanner;

import java.util.Scanner;//标准格式

public class Demo03 {
    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();
            m = m+1;//m++
            sum = sum + x;
            System.out.println("你输入了第"+m+"个数据,然后当前结果sum="+sum);
        }
        System.out.println(m+"个数和为"+sum);
        System.out.println(m+"个数的平均值是"+(sum / m));


        scanner.close();//标准格式
    }

}

执行输出结果:

随便输入第三行发现条件不循环导致循环结束

首先while循环不起来输入到下一个语句最后程序关闭在scanner.close();

Java控制03:顺序结构

Java的基本结构就是顺序结构,除非特别指明,否则按照顺序一句一句执行

顺序结构是最简单的算法结构

代码检测

package com.Alacan.struct;

public class ShunXuDemo {
    public static void main(String[] args){
         System.out.println("hello1");
         System.out.println("hello2");
         System.out.println("hello3");
         System.out.println("hello4");
         System.out.println("hello5");
         
    }
}

执行如下: 顺序执行完全没问题

语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构

Java流程控制04:if选择结构

if单选择结构:很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程在程序中用if语句来表示

if(布尔表达式){
    //如果布尔表达式为true将执行的语句
        }

代码测试

package com.Alacan.struct;

import java.util.Scanner;

public class ifDemo01 {
    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");
        scanner.close();
    }
}

执行结果:随便输入内容结果是End证明程序结束

在输出一下:还是and

为了使equals成立输入”Hello“ 结果先输入Hello,在输入End证明执行力了

表达式成立才会实行这个语句,否则 会直接跳过

if双选择结构

有一个需求,公司要收购一个软件,成功了,给人支付100万,失败了,自己找人开发。这样一个需求if就搞不定,我们需要有两个判断,需要一个双选择结构,所以就有了if-else结构

语法

if(布尔表达式){
    //如果布尔表达式的值为true
}else{
    //如果布尔表达式的值为false
}

代码测试

package com.Alacan.struct;

import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args){
        //考试分数大于60就是及格,小于60就是不及格

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");
        int score = scanner.nextInt(); //nextInt因为最小的必须是整数,否则程序就报错,直接停止  score是成绩

        if(score>60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }


        scanner.close();
    }
}

输出结果:60输出不及格

输出结果:80表示及格

if多选择结构

上一个代码并不符合实际情况,真实情况可能存在ABCD,存在区间多级判断,比如90-100为A,80-90为B……,生活中选择也不只两个,所以我们要用if多选择结构。

语法

if(布尔表达式1){
    //如果布尔表达式 1的值为true执行代码
}else if(布尔表达式2){
    //如果布尔表达式 2的值为true执行代码
}else if(布尔表达式3){
    //如果布尔表达式 3的值为true执行代码
}else{
    //如果以上布尔值表达式都不为true的执行代码
        }
package com.Alacan.struct;

import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args){
        //考试大于60分及格,小于60分不及格
        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("不及格");
        }  else{
            System.out.println("成绩不合法");
        }

        scanner.close();
    }
}

执行操作输入102

成绩不合法

输入100,恭喜满分

证明只要输出里面任何一句话都会执行往下走 剩下就不会实行 可以进行试验

if语句至少有一个else语句,else语句在所有的else if语句之后

if语句可以有若干个else if语句,它们必须在else语句之前

一旦其中一个else if 语句检测为true,其他的else if以及else语句都将跳过执行

嵌套的if结构

使用嵌套的if……else语句是合法的。也就是说你可以在另一个if或者else if语句中使用if或者else if语句。可以像if语句一样嵌套else if……else

语法

if(布尔表达式1){
    //如果布尔表达式 1的值为true执行代码
if(布尔表达式2){
    //如果布尔表达式 2的值为true执行代码 
    }
}    

switch多选择结构

Java流程控制05:switch多选择结构

多选择还有一个实现方式是switch case语句

switch case语句判断一个变量与一系列值中某个值是非相等,每个值被称为一个分支。

switch语句中变量类型可以是

byte、short、int或者char

从Java SE 7 开始

switch支持字符串String类型了

同时case标签必须为字符串常量或字面量

语法:

switch(expression){
    case value :
        //语句
        break;//可选
    case value :
            //语句
        break;//可选
        //你可以有任意数量的case语句
        default : //可选
        //语句
        }

代码:

package com.Alacan.struct;

public class SwitchDemo01 {
    public static void main(String[] args){
        //case穿透
        char grade ='C';

        switch(grade){
            case 'A':      //case判断 case ’A‘固定格式
                System.out.println("优秀");
                break;//可选 如果不输入break它后面的就会全部输出来
            case 'B':
                System.out.println("良好");
            case 'C':
                System.out.println("及格");
            case 'D':
                System.out.println("再接再厉");
            case 'E':
                System.out.println("挂科");
                //最好每写完一个就输出一个break
        }
    }
}

执行结果后

及格 再接再厉 挂科

Process finished with exit code 0

代码

package com.Alacan.struct;

public class SwitchDemo01 {
    public static void main(String[] args){
        //case穿透   switch匹配一个值
        char grade ='F';

        switch(grade){
            case 'A':           //case判断 case ’A‘固定格式
                System.out.println("优秀");
                break;//可选 如果不输入break它后面的就会全部输出来
            case 'B':
                System.out.println("良好");
            case 'C':
                System.out.println("及格");
            case 'D':
                System.out.println("再接再厉");
            case 'E':
                System.out.println("挂科");
            default:
                System.out.println("未知等级");
        }
    }
}

执行结果

未知等级

Process finished with exit code 0

总结:switch执行case语句的时候它会进行匹配,匹配成功的话会返回当前case的值然后根据他是否有break语句判断它是否输出,没有加break语句它会把以下程序全部输出,所以我们每写完一个case都要把break加上

从Java SE 7 开始

switch支持字符串String类型了实行代码:

package com.Alacan.struct;

public class SwitchDemo02 {
    public static void main(String[] args){
         String name = "阿拉璨";
         //JDK7的新特性,7以后才支持表达式结果可以是字符串!!
        //字符的本质还是数字
        
        //反编译  java---class(字码节文件)---反编译(IDEA)
        
         switch (name){
             case"阿拉":
                  System.out.println("阿拉");
                  break;
             case"阿拉璨":
                 System.out.println("阿拉璨");
                 break;
             default:
                 System.out.println("撤回!");
         }
    }
}

执行结果:

阿拉璨

Process finished with exit code 0

反编译

搜索点开文件随便打开一个我们代码发现都是乱码

在IDEA中打开文件

打开文件夹之后Java文件

把对应的class文件拷过去或者直接拖过去

然后点开IDEA发现图标有变化变成01

然后双击打开它

这个就是反编译之后的文件啦点击代码页按照下面操作进行对比

对比我们写的文字和它所判断的一样 我们发现自己的switch(name)它的是switch(name.hashCode())

Java控制流程06:while循环详解

循环结构

while循环

do……while循环

for循环

在Java5中引入了一种主要用于数组的增强型for循环

while是基本循环它的结构是:

while(布尔表达式){
    //循环内容
}

只要布尔表达式为true,循环会一直执行下去

我们在大多数情况会让循环停止下来我们需要让一个表达式失效的方式来结束循环

少部分情况需要循环一直执行,比如服务器的请求响应监听等

循环条件一直为true就会造成无限循环【死循环】我们正常的业务编程中应该尽量避免程序死循环会影响程序性能或者造成程序卡死崩溃

代码:

package com.Alacan.struct;

public class WhileDemo01 {

    public static void main(String[] args){
        //输出1~100

        int i = 0;

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


    }
}

执行结果成功

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Process finished with exit code 0

伪代码就是假代码没有意义只看不执行

package com.Alacan.struct;

public class WhileDemo02 {
    public static void main(String[] args) {
        //while true 死循环
        while(true){
            //等待客户端连接
            //定时检查
        }
    }
}

计算1+2+3+4+5+……+100=?

package com.Alacan.struct;

public class WhileDemo02 {
    public static void main(String[] args) {
        //计算
        

        int i = 0;
        int sum =0;

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

        System.out.println(sum); 
    }
}

执行结果:

5050

Process finished with exit code 0

Java控制流程07:DOWhile循环

对于while而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次

do…while循环和while循环相似,不同的是,do…while循环至少会执行一次

do{
    //代码语句
}while(布尔表达式);

代码:

package com.Alacan.struct;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i =0;
        int sum =0;

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

        System.out.println(sum);
    }
}

执行结果:

5050

Process finished with exit code 0

while和do…while区别:

while先判断后执行。do…while先执行后判断

Do…while总是保证循环体会被至少执行一次!这是他们的主要差别

代码:

package com.Alacan.struct;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int a =0;
        while(a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("=====================================");
        do{
            System.out.println(a);
            a++;
        }while(a<0);
    }
}

执行结果:

===================================== 0

Process finished with exit code 0

a=0 a<0所以不走上面循环

Java控制流程08:For循环详解

虽然所有循环都可以用while和do…while表示,但Java提供了另一种语句——for循环,使一些循环结构变得更加简单。

for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构

for循环执行的次数是在执行前就确定的

语法

for(初始化;布尔式表达;更新){
    //代码语句
        }

代码:

package com.Alacan.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        int a = 1;//初始化条件

        while(a<=100) { //条件判断
            System.out.println(a);//循环体
            a += 2; //迭代
        }

        System.out.println("while循环结束! ");

        //初始化 条件判断 迭代
        for(int i=1;i<=100;i++){
            System.out.println(i);
        }

        System.out.println("for循环结束!");
    }
}

执行结果:

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 while循环结束! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 for循环结束!

Process finished with exit code 0

关于for循环有以下几点说明:

最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。

然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。

执行一次循环后,更新控制循环变量(迭代因子控制循环变量的增减)。

再次检查布尔表达式。循环执行上面过程

快捷键

100.for 回车

for (int i = 0; i < 100; i++) {   
 }

for死循环写法

for (; ; ) {
    
}

练习:计算0到100之间的奇数和偶数的和

package com.Alacan.struct;

public class ForDemo01 {
    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; //oddSum =oddSum + i
            }
        }

        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);

    }
}

执行结果:

奇数的和:2500 偶数的和:2450

Process finished with exit code 0

练习2:使用while或for循环输出1—1000之间能被5整除的数,并且每行输出3个

package com.Alacan.struct;

public class ForDemo01 {
    public static void main(String[] args) {
       //练习2:使用while或for循环输出1—1000之间能被5整除的数,并且每行输出3个

        for (int i = 0; i <= 1000; i++) {
            if(i%5==0){
                System.out.println(i+"\t");
            }
            if(i%(5*3)==0){  //每行
                System.out.println();
                //System.out.print("\n");
            }
        }
        //println输出完会换行
        //print输出完不会换行

    }
}

执行结果:

0

5 10 15

20 25 30

35 40 45

50 55 60

65 70 75

80 85 90

95 100 105

110 115 120

125 130 135

140 145 150

155 160 165

170 175 180

185 190 195

200 205 210

215 220 225

230 235 240

245 250 255

260 265 270

275 280 285

290 295 300

305 310 315

320 325 330

335 340 345

350 355 360

365 370 375

380 385 390

395 400 405

410 415 420

425 430 435

440 445 450

455 460 465

470 475 480

485 490 495

500 505 510

515 520 525

530 535 540

545 550 555

560 565 570

575 580 585

590 595 600

605 610 615

620 625 630

635 640 645

650 655 660

665 670 675

680 685 690

695 700 705

710 715 720

725 730 735

740 745 750

755 760 765

770 775 780

785 790 795

800 805 810

815 820 825

830 835 840

845 850 855

860 865 870

875 880 885

890 895 900

905 910 915

920 925 930

935 940 945

950 955 960

965 970 975

980 985 990

995 1000

Process finished with exit code 0

Java流程控制09:打印九九乘法表

package com.Alacan.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        
            for (int i = 1; i <= 9; i++) {
                System.out.println(1+"*"+i+"="+(1*i));

        }
    }
}





执行结果:

11=1 12=2 13=3 14=4 15=5 16=6 17=7 18=8 1*9=9

Process finished with exit code 0

代码:

package com.Alacan.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        //我们先打印第一列
        //我们把一个固定的1用循环包起来
        //去掉重复项,i<=j
        //调整样式 

        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(j+"*"+i+"="+(j*i) + "\t");
            }
            System.out.println();
        }
    }
}

执行结果:

Java流程控制10:增强for循环

Java5引入了一种主要用于数组或集合的增强型for循环

Java增强for循环语法:

for(声明语句:表达式)
{
    //代码语句
}    

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。该作用域限定在循环语句块,其值与此时数组元素的值相等

表达式:表达式是要访问的数组名,或者返回值为数组的方法

代码:

package com.Alacan.struct;

public class ForDemo05 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义了一个数组


        for (int i = 0;i<5;i++){
            System.out.println(numbers[i]);
        }

        System.out.println("=========================");
        //遍历数组的元素
        for (int x:numbers){
            System.out.println(x);
        }
    }
}

执行结果:

10 20 30 40

50

====================================

10 20 30 40 50

Process finished with exit code 0

Java流程控制11:break continue goto

break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句(break语句也在swith语句中使用)

代码:

package com.Alacan.struct;

public class BreakDemo01  {
    public static void main(String[] args) {
        int i = 0;
        while(i<100){
            i++;
            System.out.println(i);
            if (i==30){
                break;
            }
        }
    }
}

执行结果:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Process finished with exit code 0

continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定

代码:

package com.Alacan.struct;

public class ContinueDemo01 {
    public static void main(String[] args) {
        int i =0;
        while(i<100){
            i++;
            if (i%10==0){
                System.out.println();
                continue;
            }
            System.out.print (i);
        }
    }
}

执行结果:

123456789 111213141516171819 212223242526272829 313233343536373839 414243444546474849 515253545556575859 616263646566676869 717273747576777879 818283848586878889 919293949596979899

Process finished with exit code 0

//break在任何循环语句的主体部分,均可用break控制循环的流程

break用于强行退出循环,不执行循环中剩余的语句(break语句也在switch语句中使用)

continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判断

goto关键字很早就在程序设计语言中出现,尽管goto仍是Java中一个保留字,但并未在语言中得到正式使用,Java没有goto,然而在break和continue这两个关键字身上,我们仍然能看出goto的影子加上带标签的break和continue

标签指后面跟一个冒号的标识符,例如:label:

对于Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环由于break和continue关键字通常只中断当前循环,但随着标签使用,它们会中断到存在标签的地方

代码:

package com.Alacan.struct;

public class LabelDemo {
    public static void main(String[] args) {
        //打印101-150之间所有的质数
        //质数指大于1的自然数中,除了1和它本身以外不再有其他因数的自然数

        int count = 0;

        outer:for (int i=101;i<150;i++){
            for (int j = 2;j<i/2;j++){
                if(i % j == 0){
                    continue outer;
                }
            }
            System.out.println(i+" ");
        }

    }

}

执行结果:

101 103 107 109 113 127 131 137 139 149

Process finished with exit code 0

Java流程控制12:打印三角形级Debug

练习题代码:

package com.Alacan.struct;

public class TestDemo {

    public static void main(String[] args) {
        //打印三角形 五行

        for (int i = 1; i < 5; i++) {
            for (int j = 5; j >= i; j--) {  //快捷键fori
                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();
        }

    }
}

执行结果:
 

     *
    ***
   *****
  *******

Process finished with exit code 0

IDEA右上角有一个爬虫不会的话点击爬虫会详细分析

Java方法01:什么是方法?

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

1方法是解决一类问题的步骤的有序组合

2方法包含于类或对象中

3方法在程序中被创建,在其他地方被引用

设计方法的原则:方法的本意是功能块,就是实现某个功能的语句块的集合。我们设计方法的时候,最好保持方法的原子性,就是一个方法只完成一个功能,这样有利于我们后期的扩展

代码:

package com.Alacan.method;

public class Demo01 {
    //方法
    public static void main(String[] args) {
        int sum = add(1,2);
         System.out.println(sum);

    }

    //加法
    public static  int add(int a, int b){
        return a+b;
    }
}

执行结果等于3

代码:

package com.Alacan.method;

public class Demo01 {
    //方法
    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 = 0;i <= 1000;i++){
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){  //每行
                System.out.println();
                //System.out.println("\n");
            }
        }
    }

输出结果:

0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180 185 190 195 200 205 210 215 220 225 230 235 240 245 250 255 260 265 270 275 280 285 290 295 300 305 310 315 320 325 330 335 340 345 350 355 360 365 370 375 380 385 390 395 400 405 410 415 420 425 430 435 440 445 450 455 460 465 470 475 480 485 490 495 500 505 510 515 520 525 530 535 540 545 550 555 560 565 570 575 580 585 590 595 600 605 610 615 620 625 630 635 640 645 650 655 660 665 670 675 680 685 690 695 700 705 710 715 720 725 730 735 740 745 750 755 760 765 770 775 780 785 790 795 800 805 810 815 820 825 830 835 840 845 850 855 860 865 870 875 880 885 890 895 900 905 910 915 920 925 930 935 940 945 950 955 960 965 970 975 980 985 990 995 1000 Process finished with exit code 0

Java方法02:方法的定义和调用

Java的方法类似与其他语言的函数,是一段用来完成特定功能的代码片段,一般情况下,定义一个方法需要以下语法:

方法包含一个方法头和方法体,下面是一个方法的所有部分:

修饰符:修饰符,这是可选的,告诉编译器如何调用该方法。定义了该方法的访问类型。

返回值类型:方法可能会返回值。returnValueType是方法返回值的数据类型,有些方法执行所需的操作,但没有返回值。在这种情况下,returnValueType是关键字void。

方法名是方法的实际名称。方法名和参数表共同构成方法签名。

参数类型:参数像是一个占位符,当方法被调用时,传递给参数。这个值被称为实参或变量。参数列表是指方法的参数类型、顺序和参数的个数。参数是可选的,方法可以不包含任何参数。

形式参数:在方法被调用时用于接收外界输入的数据

实参:调用方法时实际传给方法的数据

方法体:方法体包含具体的语句,定义该方法功能。

语法:

修饰符 返回值类型 方法名(参数类型 参数名){
    
    ···
    方法体
    ···
    return 返回值;
}
package com.Alacan.method;

public class Demo01 {
    //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 = 0;i <= 1000;i++){
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){  //每行
                System.out.println();
                //System.out.println("\n");
           }
        }
    }
}
package com.Alacan.method;

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

        int max = max(10,20);
        System.out.print(max);

    }
    //比大小
    public static int max(int num1,int num2){

        int result = 0;

        if (num1==num2){
            System.out.print("num1==num2");
            return 0;//终止方法
        }
        if (num1>num2){
            result = num1;
        }else{
            result = num2;
        }
        return  result;
    }
}

执行结果:

20 Process finished with exit code 0

方法调用

调用方法:对象名.方法名(实参列表)

Java支持两种调用方法的方式,根据方法是否返回值来选择

当方法返回一个值的时候,方法调用通常被当作一个值,例如:

int larger = max(30,40);

如果方法返回值是void,方法调用一定是一条语句。

System.out.println("Hello,Alacan!");

Java方法03:方法的重载

重载就是在一个类中,有相同的函数名称,但形参不同的函数

方法的重载规则:

方法名称必须相同

参数列表必须不同(个数不同 或类型不同 参数排列顺序不同等)

方法的返回类型可以相同也可以不相同

仅仅返回类型不同不足以成为方法的重载

实现理论:方法名称相同时,编译器会根据调用方法的参数个数、参数类型等逐个匹配,已选择对应的方法,如果匹配失败则编译器报错

package com.Alacan.method;

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

        double max = max(10.0,20.0);
        System.out.print(max);
        System.out.println("Hello,Alacan!");

    }
    //比大小
    public static double max(double num1,double num2){

        double result = 0;

        if (num1==num2){
            System.out.print("num1==num2");
            return 0;//终止方法
        }
        if (num1>num2){
            result = num1;
        }else{
            result = num2;
        }
        return  result;
    }
}

输出结果:

20.0Hello,Alacan!

Process finished with exit code 0

package com.Alacan.method;

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

        double max = max(10.0, 20.0);
        System.out.println(max);

    }

    //比大小
    public static double max(double num1, double num2) {...
   }

    public static int max(int num1, int num2) {...
    }

    public static int max(int num1, int num2, int num3) {...
}

Java方法04:命令行传递参数

有时候你希望运行一个程序时候在传递给它消息。这要靠传递命令行参数给main()函数实现

public class CommandLine{
    public static void main(String[] args) {
        for(int i=0;i < args.length; i++){
            System.out.println("args["+i+"]:"+args[i]);
        }
    }
}

不执行打开Terminal 打开不了然后在Demo03打开文件夹输入cmd查找

cd..\返回上一级

Java方法05:可变参数

JDK1.5开始,Java支持传递同类型的可变参数给一个方法

在方法声明中在指定参数后加一个省略号(...)

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

语法:

public static void printMax(double... numbers) {
    if (numbers.length == 0){ 
        System.out.println("No argument passed");
        return;
    }
    
    double result = numbers[0];
    
    //排序
    for (int i = 1; i < numbers.length; i++) {
        if (numbers[i] > result ){
            result = numbers[i];
        }
    }
    System.out.println("The max value is"+result );
}

代码:

package com.Alacan.method;

public class Demo04 {
    public static void main(String[] args) {
        Demo04 demo04 = new Demo04();
        demo04.test(1);
    }
    public void test(int...i){
        System.out.println(i[0 ]);
    }
}

输出执行结果:

1

Process finished with exit code 0

package com.Alacan.method;

public class Demo04 {
    public static void main(String[] args) {
        Demo04 demo04 = new Demo04();
        demo04.test(1,2,3,4,45,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]);
        System.out.println(i[5]);
    }
}
package com.Alacan.method;

public class Demo04 {
    public static void main(String[] args) {
        Demo04 demo04 = new Demo04();
        demo04.test(1,2,3,4,45,5);
    }
    public void test(int x,int...i){

    }
}

Java方法06:递归讲解

A方法调用B方法,很容易理解

递归就是:A方法调用A方法,就是自己调用自己

利用递归可以用简单的程序来解决一些复杂的问题。它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算,大大地减少了程序的代码量。递归的能力在于用有限的语句来定义无限集合。

递归结构包括两个部分:

递归头:什么时候不调用自身方法。如果没有头,将陷入死循环。

递归体:什么时候需要调用自身方法

package com.Alacan.method;

public class Demo05 {
    public static void main(String[] args) {
        Demo05 test = new Demo05();
        test.test();
    }
    public void test(){
         test();
    }
}

代码:

package com.Alacan.method;
//递归思想
public class Demo06 {

    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);
        }
    }
}

输出结果:

120

Process finished with exit code 0

数组

Java数组01:什么是数组

数组的定义:

数组是相同类型数据的有序集合

数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成

其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们

Java数组02:数组声明创建

首先必须声明数组变量,才能在程序中使用数组。下面是声明数组变量的语法:

dataType[] arrayRefVar = new dataType[arraySize];

数组的元素是通过索引访问的,数组索引从0开始

获取数组长度:

arrays.length

代码:

package com.Alacan.array;

public class ArrayDemo01 {
    //变量类型   变量名字   =   变量的值;
    //数组类型

    public static void main(String[] args) {
        int[] nums;   //1.定义

        nums = new int[10];  //2.创建一个数组 这里面可以存放10个int类型的数字

        //3.给数组元素中赋值
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        nums[3] = 4;
        nums[4] = 5;
        nums[5] = 6;
        nums[6] = 7;
        nums[7] = 8;
        nums[8] = 9;
        nums[9] = 10;

        System.out.println(nums[9]);


    }
}

输出结果:

10

Process finished with exit code 0

代码:

package com.Alacan.array;

public class ArrayDemo01 {
    //变量类型   变量名字   =   变量的值;
    //数组类型

    public static void main(String[] args) {
        int[] nums;   //1.声明一个数组

        nums = new int[10];  //这里面可以存放10个int类型的数字 2.创建一个数组

        //3.给数组原数组赋值
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        nums[3] = 4;
        nums[4] = 5;
        nums[5] = 6;
        nums[6] = 7;
        nums[7] = 8;
        nums[8] = 9;
        nums[9] = 10;

        //计算所有元素的和
        int sum = 0;
        //获取数组长度:arrays.length
        for (int i = 0; i < nums.length; i++) {
            sum = sum + nums[i];
        }
        System.out.println("总和为:"+sum);
    }
}

输出结果:

总和为:

55

Process finished with exit c

图解:

Java数组03:三种初始化及内存分析

三种初始化:

静态初始化

int[] a = {1,2,3};
Man[] mans = {new Man(1,1),new Man(2,2)};

动态初始化:

int[] a = new int[2];
a[0]=1;
a[1]=2;

数组的默认初始化:

数组是引用类型,它的元素相当于类的实例变量,因此数组一经分配空间,其中的每个元素也被按照实例变量同样的方式被隐式初始化。

代码:

package com.Alacan.array;

public class ArrayDemo02 {
    public static void main(String[] args) {
        //静态初始化
        int[] a = {1,2,3,4,5,6,7,8};
        //Man[] mans = {new Man(),new Man()}; 理解一下
        System.out.println(a[0]);

        //动态初始化 包含默认初始化
        int[] b = new int[10];
        b[0] = 10;

        System.out.println(b[0]);
        //b1没有赋值也存在默认值
        System.out.println(b[1]);
    }
}

执行结果:

输出结果:

1 10 0

Process finished with exit code 0

Java数组04:下标越界和小结

数组的四个基本特点:

其长度是确定的,数组一旦被创建,它的大小就是不可以改变的

其元素必须是相同类型,不允许出现混合类型。

数据中的元素可以是任何数据类型,包括基本类型和引用类型。

数组变量属引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量。

数组本身就是对象,Java中对象是在堆中的,因此数据组无论保存原始类型还是其他类型,数组对象本身是在堆中的

数组边界:

下标的合法区间:[0,length-1] 如果越界就会出错

public static void main(String[] args) {
    int[] a = new int[2];
    System.out.println(a[2]);
}

ArrayIndexOutOfBoundsException:数组下标越界异常

小结:

数组是相同数据类型(数据类型可以为任意类型)的有序集合

数组也是对象。数组类型相当于对象的成员变量

数组长度的确定的不可变的。如果越界则报:ArrayIndexOutOfBoundsException

Java数组05:数组的使用

普通For循环

For-Each 循环

数组作方法入参

数组做返回值

普通for循环代码:

package com.Alacan.array;

public class ArrayDemo03 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4,5};

        //打印全部的数组元素
        for (int i = 0; i < arrays.length; i++) {
            System.out.println(arrays[i]);
        }
        System.out.println("=========");
        //计算所有元素的和
        int sum = 0;
        for (int i = 0; i < arrays.length; i++) {
            sum = sum + arrays[i];
        }
        System.out.println("sum="+sum);

        //查找最大元素
        int max = arrays[0];

        for (int i = 0; i < arrays.length; i++) {
            if (arrays[i]>max){
                max = arrays[i];
            }
        }

        System.out.println("max="+max);
    }
}

执行结果:

1 2 3 4

5

sum=15 max=5

Process finished with exit code 0

进阶代码:

package com.Alacan.array;

public class ArrayDemo04 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4,5};

        //JDK1.5 没有下标
//        for (int array : arrays){
//            System.out.println(array);
//        }
        //prinyArray(arrays);

        int[] reverse = reverse(arrays);
        prinyArray(reverse);//需要引用打印数组元素里的方法参数
    }

    //反转数组
    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[] = arrays[i];
            result[j] = arrays[i];
        }

        return result;
    }

    //打印数组元素
    public static void prinyArray(int[] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i]+" ");
        }
    }

}

输出结果:

5 4 3 2 1 Process finished with exit code 0

For-Each 循环:

package com.Alacan.array;

public class ArrayDemo05 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4,5};

        //JDK1.5 没有下标
        for (int array : arrays){
            System.out.println(array);
        }
    }

}

输出结果:

1 2 3 4 5

Process finished with exit code 0

代码:

package com.Alacan.array;

public class ArrayDemo05 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4,5};

        printArray(arrays);
    }
    //打印数组元素
    public static void printArray(int[] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i]+" ");
        }
    }

}

执行结果:

1 2 3 4 5 Process finished with exit code 0

Java数组06:二维数组

多维数组:

多维数组可以看成是数组的数组,比如二维数组就是一个特殊的一维数组,其每一个元素都是一个一维数组

二维数组:

int a[][] = new int[2][5];

解析:

以上二维数组a可以看成一个两行五列的数组。

思考多维数组的使用?

num[1][0];

看图理解:

详细图解:

一维数组变二维数组定义:

package com.Alacan.array;

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

        //[4][2]
        /*
        1,2      array[0]
        2,3      array[1]
        3,4      array[2]
        4,5      array[3]
        */
        int[][] array = {{1,2},{2,3},{3,4},{4,5}};

        System.out.println(array[0]);
    }
}    

执行结果:

[I@4554617c Process finished with exit code 0

查看里面每一个值:

package com.Alacan.array;

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

        //[4][2]
        /*
        1,2      array[0]
        2,3      array[1]
        3,4      array[2]
        4,5      array[3]
        */
        int[][] array = {{1,2},{2,3},{3,4},{4,5}};
        

        prinyArray(array[0]);
    }
    //打印数组元素
    public static void prinyArray(int[] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i]+" ");
        }
    }
}

执行结果:

1 2 Process finished with exit code 0

关于取数:

package com.Alacan.array;

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

        //[4][2]
        /*
        1,2      array[0]
        2,3      array[1]
        3,4      array[2]
        4,5      array[3]
        */
        int[][] array = {{1,2},{2,3},{3,4},{4,5}};
        
        /*索引0=1 1=2 2=3 3=4 4=5
        取{4,5}就是首先先取4,它是第4个集合{4,5}位列第一位
        索引4=[3]第1位1=[0]  [3][0]    
        取5,它和4都是元素属于一个集合也是第4个集合索引4=[3]
        {4,5}取5位于第2位索引5=[1]  [3][1]    
         */
        
        System.out.println(array[3][0]);
        System.out.println(array[3][1]);
    }
    //打印数组元素
    public static void prinyArray(int[] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i]+" ");
        }
    }
}

输出结果:

4 5

Process finished with exit code 0

写一个循环,把它们都循环出来:多维数组

package com.Alacan.array;

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

        //[4][2]
        /*
        1,2      array[0]
        2,3      array[1]
        3,4      array[2]
        4,5      array[3]
        */
        int[][] array = {{1,2},{2,3},{3,4},{4,5}};

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

执行结果:

1 2 2 3 3 4 4 5

Process finished with exit code 0

Java数组07:Arrays类讲解

Arrays类:

数组的工具类java.util.Arrays

由于数组对象本身并没有什么方法可以供我们调用,但API中提供了一个工具类Arrays供我们使用,从而可以对数据对象进行一些基本操作。

查看JDK帮助文档

Arrays类中的方法都是static修饰的静态方法,在使用的时候可以之间使用类名进行调用,而”不用“使用对象来调用(注意:是”不用“而不是”不能“)

具有以下常用功能:

给数组赋值:通过fill方法。

对数组排序:通过sort方法,按升序。

比较数组:通过equals方法比较数组中元素值是否相等。

查找数组元素:通过binarySearch 方法能对排序好的数组进行二分查找法操作

万能方法代码:

package com.Alacan.array;

import java.util.Arrays;

public class ArrayDemo06 {
    public static void main(String[] args) {
        //输入Arrays输出import java.util.Arrays;

        int[] a = {1,2,3,4,9090,31231,543,21,3,23};

        System.out.println(a);  //输出[I@4554617c

打印数组元素:

package com.Alacan.array;

import java.util.Arrays;

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

        int[] a = {1,2,3,4,9090,31231,543,21,3,23};

        System.out.println(a);  //[I@4554617c
        //打印数组元素Arrays.toString
        System.out.println(Arrays.toString(a));
    }
}    

输出结果:

[I@4554617c [1, 2, 3, 4, 9090, 31231, 543, 21, 3, 23]

Process finished with exit code 0

自己写方法:

package com.Alacan.array;

import java.util.Arrays;

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

        int[] a = {1,2,3,4,9090,31231,543,21,3,23};

        System.out.println(a);  //[I@4554617c
        //打印数组元素Arrays.toString
        //System.out.println(Arrays.toString(a));
        printArray(a);
    }

    public  static void printArray(int[] a){
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+",");
        }
    }
}

输出结果;

[I@4554617c 1,2,3,4,9090,31231,543,21,3,23, Process finished with exit code 0

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

        int[] a = {1,2,3,4,9090,31231,543,21,3,23};

        System.out.println(a);  //[I@4554617c
        //打印数组元素Arrays.toString
        System.out.println(Arrays.toString(a));
        printArray(a);
    }

    public  static void printArray(int[] a){
        for (int i = 0; i < a.length; i++) {
            if (i==0){
                System.out.print("[");
            }
            if (i==a.length-1){
                System.out.print(a[i]+"]");
            }else{
                System.out.print(a[i]+", ");
            }
        }
    }
}

输出结果:

[I@4554617c [1, 2, 3, 4, 9090, 31231, 543, 21, 3, 23] [1, 2, 3, 4, 9090, 31231, 543, 21, 3, 23] Process finished with exit code 0

排序代码:

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

        int[] a = {1,2,3,4,9090,31231,543,21,3,23};

        System.out.println(a);  //[I@4554617c
        //打印数组元素Arrays.toString
        //System.out.println(Arrays.toString(a));
        Arrays.sort(a);//数组进行排序

        System.out.println(Arrays.toString(a));
    } 

    public  static void printArray(int[] a){
        for (int i = 0; i < a.length; i++) {
            if (i==0){
                System.out.print("[");
            }
            if (i==a.length-1){
                System.out.print(a[i]+"]");
            }else{
                System.out.print(a[i]+", ");
            }
        }
    }
}

输出结果: [I@4554617c [1, 2, 3, 3, 4, 21, 23, 543, 9090, 31231]

Process finished with exit code 0

代码:

package com.Alacan.array;

import java.util.Arrays;

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

        int[] a = {1,2,3,4,9090,31231,543,21,3,23};

        System.out.println(a);  //[I@4554617c
        //打印数组元素Arrays.toString
        //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));
    }

    public  static void printArray(int[] a){
        for (int i = 0; i < a.length; i++) {
            if (i==0){
                System.out.print("[");
            }
            if (i==a.length-1){
                System.out.print(a[i]+"]");
            }else{
                System.out.print(a[i]+", ");
            }
        }
    }
}

输出结果[I@4554617c [1, 2, 3, 3, 4, 21, 23, 543, 9090, 31231] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Process finished with exit code 0

Java数组08:冒泡排序

冒泡排序无疑是最为出名的排序算法之一,总共有八大排序!

冒泡的代码还是相当简单的,两层循环,外层冒泡轮数,里层依次比较,江湖中人人尽皆知。

我们看到嵌套循环,应该立马就可以得出这个算法的时间复杂度为O(n2)。

思考:如何优化

package com.Alacan.array;

import java.util.Arrays;

public class ArrayDemo07 {
    public static void main(String[] args) {
        int[] a = {1,4,5,6,72,2,2,2,25,6,7};

        int[] sort = sort(a);//调用完我们自己写的排序方法以后,返回一个排序后的数组

        System.out.println(Arrays.toString(sort));
    }

    //冒泡排序
    //1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置
    //2.每一次比较,都会产生一个最大的,或者最小的数字;
    //3.下一轮则可以少一次排序;
    //4.依次循环,直到结束!

    public static int[] sort(int[] array){
        //临时变量
        int temp = 0;

        //外层循环,判断我们这个要走多少次;
        for (int i = 0; i < array.length-1; i++) {
            //内层循环,比较判断两个数,如果第一个数,比第二个数大,则交换位置
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j+1]>array[j]){  //if (array[j+1]<array[j]){  输出结果从小到大
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }

        return array;

    }
}

输出结果:

[72, 25, 7, 6, 6, 5, 4, 2, 2, 2, 1]

Process finished with exit code 0

优化思路代码:

package com.Alacan.array;

import java.util.Arrays;

public class ArrayDemo07 {
    public static void main(String[] args) {
        int[] a = {1,4,5,6,72,2,2,2,25,6,7};

        int[] sort = sort(a);//调用完我们自己写的排序方法以后,返回一个排序后的数组

        System.out.println(Arrays.toString(sort));
    }

    //冒泡排序
    //1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置
    //2.每一次比较,都会产生一个最大的,或者最小的数字;
    //3.下一轮则可以少一次排序;
    //4.依次循环,直到结束!

    public static int[] sort(int[] array){
        //临时变量
        int temp = 0;

        //外层循环,判断我们这个要走多少次;
        for (int i = 0; i < array.length-1; i++) {

            boolean flag = false;//通过false标识位减少没有意义的比较

            //内层循环,比较判断两个数,如果第一个数,比第二个数大,则交换位置
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j+1]<array[j]){  //if (array[j+1]>array[j]){  输出结果从大到小
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    flag = true;
                }
            }

            if (flag==false){
                break;
            }
        }

        return array;

    }
}

输出结果:

[1, 2, 2, 2, 4, 5, 6, 6, 7, 25, 72]

Process finished with exit code 0

Java数组09:稀疏数组

稀疏数组介绍:

当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组

稀疏数组的处理方式是:

记录数组一共几行几列,有多少个不同值

把具有不同值的元素和行列及值记录在一个小规模的数组中,从而缩小程序的规模

原始数组代码:

package com.Alacan.array;

public class ArrayDemo08 {
    public static void main(String[] args) {
        //1.创建一个二维数组 11*11  0:没有棋子, 1:黑棋 2:白棋
        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();
        }
    }
}

输出结果:

输出原始的数组 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Process finished with exit code 0

转化稀疏数组代码:

package com.Alacan.array;

public class ArrayDemo08 {
    public static void main(String[] args) {
        //1.创建一个二维数组 11*11  0:没有棋子, 1:黑棋 2:白棋
        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();
        }
        System.out.println("===============================");
        //转化成稀疏数组保存
        //获取有效值的个数

        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (array1[i][j]!=0){
                    sum++;
                }
            }
        }
        System.out.println("有效价值的个数:"+sum);

        //2.创建一个稀疏数组
        int[][] array2 = new int[sum+1][3];

        array2[0][0] = 11;
        array2[0][1] = 11;
        array2[0][2] = sum;

        //遍历二维数组,将非零的值,存放稀疏数组中
        int count=0;
        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]+"\t");
        }
    }
}

输出结果:

输出原始的数组 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

有效价值的个数:2

稀疏数组

11 11 2 1 2 1 2 3 2

Process finished with exit code 0

输出还原的数组:

package com.Alacan.array;

public class ArrayDemo08 {
    public static void main(String[] args) {
        //1.创建一个二维数组 11*11  0:没有棋子, 1:黑棋 2:白棋
        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 < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (array1[i][j]!=0){
                    sum++;
                }
            }
        }
        System.out.println("有效价值的个数:"+sum);

        //2.创建一个稀疏数组
        int[][] array2 = new int[sum+1][3];

        array2[0][0] = 11;
        array2[0][1] = 11;
        array2[0][2] = sum;

        //遍历二维数组,将非零的值,存放稀疏数组中
        int count=0;
        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]+"\t");
        }
        System.out.println("===============================");
        System.out.println("还原");
        //1.读取稀疏数组
        int[][] array3 = new int[array2[0][0]][array2[0][1]];

        //2.给其中的元素还原它的值
        for (int i = 1; i < array2.length; i++) {
            array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }

        //3.打印
        System.out.println("输出还原的数组");

        for (int[] ints : array1){
            for (int anInt : ints){
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }
    }
}

输出结果:

输出原始的数组 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 有效价值的个数:2 稀疏数组 11 11 2 1 2 1 2 3 2

还原 输出还原的数组 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Process finished with exit code 0

面向对象

Java面向对象01:什么是面向对象

Java的核心思想就是OOP OO指面向对象 P指编程 OOP:面向对象编程

面向过程 & 面向对象

面向过程思想:

步骤清晰简单,第一步做什么,第二步做什么……

面对过程适合处理一些较为简单的问题

面向对象思想:

物以类聚,分类的思维模式,思考问题首先会解决问题需要哪些分类,然后对这些分类进行单独思考。最后,才对某个分类下的细节进行面向过程的思索。

面向对象适合处理复杂的问题,适合处理需要多人协作的问题!

对于描述复杂的事物,为了从宏观上把握、整体上分析,我们需要使用面向对象的思路来分析整个系统。但是,具体到微观操作,仍然需要面向过程的思路去处理

什么是面向对象?

面向对象编程(Object-Oriented Programming,OOP)

面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据。

抽象

三大特性:

封装

继承

多态

从认识论角度考虑是先有对象后有类。对象,是具体的事物。类,是抽象的,是对象的抽象

从代码运行角度考虑是先有类后有对象。类是对象的模板。

Java面向对象02:回顾方法的定义

方法的定义

修饰符

返回类型

package com.Alacan.oop.demo01;

//Demo01类
public class Demo01 {
    //main方法
    public static void main(String[] args) {

    }

    /*
    修饰符  返回值类型 方法名(...){
        方法体
        return 返回值;
    }
     */

    public String sayHello(){
        return "hello,world";
    }
    //public void hello(){
    //    return;
    //}
    public int max(int a,int b){
        return a>b ? a : b;//三元运算符!
    }

}

break:跳出switch,结束循环和return的区别

package com.Alacan.oop.demo01;

//Demo01类
public class Demo01 {
    //main方法
    public static void main(String[] args) {

    }

    /*
    修饰符  返回值类型 方法名(...){
        方法体
        return 返回值;
    }
     */
    //return 结束方法,返回一个结果
    public String sayHello(){
        return "hello,world";
    }
    public void print(){
        return;
    }
    public int max(int a,int b){
        return a>b ? a : b;//三元运算符!
    }

}

方法名:注意方法就可以 见名知意

参数列表:(参数类型,参数名)…

异常抛出 疑问 后面学

//数组下标越界:ArrayIndexOutOfBoundsException
public void readFile (String file) throws IOException{
    
}

Java面向对象03:回顾方法的调用

方法的调用:递归

静态方法

非静态方法

package com.Alacan.oop.demo01;

public class Demo02 {
    public static void main(String[] args) {
        //实例化这个类new
        //对象类型  对象名 = 对象值

        Student student = new Student();
        student.say();
        }

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

    //实例化之后才存在
    public void b(){

    }
} 
package com.Alacan.oop.demo01;

//学生类
public class Student {

    //非静态方法
    public void say(){
        System.out.println("学生说话了");
    }
}

形参和实参

package com.Alacan.oop.demo01;

public class Demo03 {

    public static void main(String[] args) {
        //实际参数和形式参数的类型要对应!
        int add = Demo03.add(1,2);
        System.out.println(add);
    }

    public static int add(int a,int b){
        return a+b;
    }
}

输出结果:

3

Process finished with exit code 0

值传递和引用传递

package com.Alacan.oop.demo01;

//值传递
public class Demo04 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);//1

        Demo04.change(a);

        System.out.println(a);//1

    }
    //返回值为空
    public static void change(int a){
        a = 10;
    }
}

输出结果:

1 1

Process finished with exit code 0

package com.Alacan.oop.demo01;

//引用传递:对象, 本质还是值传递
public class Demo05 {
    public static void main(String[] args) {
        Perosn perosn = new Perosn();

        System.out.println(perosn.name);//null

        Demo05.change(perosn);

        System.out.println(perosn.name);//阿拉璨 
    }

    public static void change(Perosn perosn){
        perosn.name = "阿拉璨";
    }
}
//定义了一个perosn类,有一个属性:name
class Perosn{
    String name;//null
}

输出结果:

null 阿拉璨

Process finished with exit code 0

this关键字

代表当前对象自身的引用

面向对象04:类与对象的关系

类是一种抽象的数据类型,它是对某一类事物整体描述/定义,但是并不能代表某一个具体的事物。

动物 植物 手机 电脑……

Person类,Pet类,Car类等,这些类都是用来描述/定义某一类具体的事物应该具备的特点和行为

对象是抽象概念的具体实例

张三就是人的一个具体实例,张三家里的旺财就是狗的一个具体实例

能够体现出特点,展现功能的是具体的实例,而不是一个抽象的概念

package com.Alacan.oop.demo002;

//学生类
public class Student {

    //属性:字段
    String name;  //null
    int age;      //0

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

//Person-->身高,体重,年龄,国家
package com.Alacan.oop.demo002;

//一个项目应该只存在一个main方法
public class Application {

    public static void main(String[] args) {

        //类:抽象的,实例化
        //类实例化后会返回一个自己的对象!
        //student对象就是一个Student类的具体实例!

        Student xiaoming = new Student();
        Student xiaosong = new Student();

        xiaoming.name = "小铭";
        xiaoming.age = 19;

        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);

        xiaosong.name = "小宋";
        xiaosong.age = 19;

        System.out.println(xiaosong .name);
        System.out.println(xiaosong.age);
    }
}

输出结果:

小铭 19 小宋 19

Process finished with exit code 0

Java面向对象05:构造器详解:

创建与初始化对象

使用new关键字创建对象

使用new关键字创建的时候,除了分配内存空间外,还会给 创建好的对象 进行默认的初始化以 及对类中构造器的调用

类中的构造器也称为构造方法,是在进行创建对象的时候必须要调用的。并且构造器有以下俩个特点:

1.必须和类的名字相同

2.必须没有返回类型,也不能写void

构造器必须要掌握

package com.Alacan.oop.demo002;

//java===》class
public class Person {
    //一个类即使什么都不写,他也会存在一个方法
    //显示的定义构造器
    String name;

    //实例化初始值
    //使用new关键字,必须要有构造器
    public Person(){
        this.name = "alacan";
    }


    //有参构造:一旦定义了有参构造,无参就必须显式定义
    public Person(String name){

        this.name = name;

    }
}
package com.Alacan.oop.demo002;

//一个项目应该只存在一个main方法
public class Application {

    public static void main(String[] args) {
        //new 实例化一个对象
        Person person = new Person();

        System.out.println(person.name);

    }
}

输出结果:

alacan

Process finished with exit code 0

package com.Alacan.oop.demo002;

//java===》class
public class Person {
    //一个类即使什么都不写,他也会存在一个方法
    //显示的定义构造器
    String name;
    
    //1.使用new关键字,必须要有构造器
    //2.用来初始化值
    public Person(){

    }
    
    //有参构造:一旦定义了有参构造,无参就必须显式定义
    public Person(String name){
        this.name = name
    }
    //alt+ insert
}
package com.Alacan.oop.demo002;

//一个项目应该只存在一个main方法
public class Application {

    public static void main(String[] args) {
        //new 实例化一个对象
        Person person = new Person("alacan");

        System.out.println(person.name);

    }
}

输出结果: alacan

Process finished with exit code 0

苹果:option+触屏条Ins查看构造 安卓:alt+insert

点开点击ok

构造器

1.和类名相同!

2.没有返回值

作用:

1.new 本质在调用构造器

2.初始化对象的值

注意点:

1.定义有参构造之后,如果想使用无参构造,显示的定义一个无参的构造

Alt + Insert

Java面向对象06:创建对象内存分析

package com.Alacan.oop.demo03;

public class Pet {
    String name;
    int age;

    //无参构造

    public void shout(){
        System.out.println("叫了一声");
    }
}
package com.Alacan.oop.demo03;

public class Application {

    public static void main(String[] args) {

        Pet dog = new Pet();

        dog.name = "莱福";
        dog.age = 1;
        dog.shout();

        System.out.println(dog.name);
        System.out.println(dog.age);

        Pet cat = new Pet();
    }
}

Java面向对象07:简单小结类与对象

package com.Alacan.oop.demo03;

public class Application {

    public static void main(String[] args) {
        /*
        1.类与对象:
                类是一个模板:抽象,对象是一个具体的实例
                
        2.方法:
              定义、调用!
              
        3.对应的引用
        引用的类型: 基本类型(8)
                对象是通过引用来操作的:栈----->堆
                
        4.属性:字段 Field 成员变量
          默认初始化:
               数字:0   0.0
              char: u0000
           boolean: false
               引用:null
        修饰符  属性类型  属性名 = 属性值!
        
        5.对象的创建和使用:
                       -必须使用new 关键字创造对象,构造器 Person alacan = new Person();
                       -对象的属性   alacan.name
                       -对象的方法   alacan.sleep()
                       
        6.类:
             静态的属性    属性
             动态的行为    方法
             
             封装  继承  多态
             */
    }
}

Java面向对象08:封装详解

该露的露,该藏得藏

我们程序设计要追求”高内聚,低耦合”。高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用。

封装(数据的隐藏)

通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏。

记着这句话:属性私有, get / set

Split Right|_||

package com.Alacan.oop.demo04;

//类 private: 私有
public class Student {


    private String name;//名字
    private int id; //学号
    private  char sex;  //性别

    //提供一些可以操作这个属性的方法!
    //提供一些 public 的 get、set方法
    //get获得这个数据
    public String getName(){
        return this.name;
    }
    //set 给这个数据设置值
    public void setName(String name){
        this.name = name;

    }
}
package com.Alacan.oop.demo04;

public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();

        s1.setName("莱福");

        System.out.println(s1.getName());
    }
}

输出结果:

莱福

Process finished with exit code 0

package com.Alacan.oop.demo04;

public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();

        s1.setName("莱福");

        System.out.println(s1.getName());

        s1.setAge(888);//888不合法的
        System.out.println(s1.getAge());
    }
}
package com.Alacan.oop.demo04;

//类 private: 私有
public class Student {


    private String name; //名字
    private int id;  //学号
    private char sex;  //性别
    private int age;  //年龄

    //提供一些可以操作这个属性的方法!
    //提供一些 public 的 get、set方法
    //get获得这个数据
    public String getName(){
        return this.name;
    }
    //set 给这个数据设置值
    public void setName(String name){
        this.name = name;

    }

    //alt + insert
    //第四个 Getter and Setter
    //shift 单机全选OK 或者ctrl+A
    public int getId() {
        return id;
    }

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

    public char getSex() {
        return sex;
    }

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

    public int getAge() {
        return age;
    }

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

    }
}

输出结果:

莱福 3

Process finished with exit code 0

总结封装的意义:

1.提高程序的安全性,保护数据

2.隐藏代码的实现细节

3.统一接口

4.系统可维护增加了

Java面向对象09:什么是继承?

继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模

extands的意思是“扩展”。子类是父类的扩展。

Java中类只有单继承,没有多继承!

继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等。

继承关系的俩个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends表示。

子类和父类之间,从意义上讲应该具有“is a”的关系。

object类

super

方法重写

package com.Alacan.oop.demo05;

public class Application {

    public static void main(String[] args) {

        Student student = new Student();
        student.say();
        System.out.println(student.money);
    }
}
package com.Alacan.oop.demo05;

//学生 is 人 : 派生类 , 子类
//子类继承父类,就会拥有父亲的全部方法!
public class Student extends Person{

    Person person;
    //ctrl + H
}

输出结果:

说了一句话 1000000000

Process finished with exit code 0

在Java中,所有的类,都默认间接继承object
// ctrl + H

Java面向对象10:Super详解

package com.Alacan.oop.demo05;

public class Application {

    public static void main(String[] args) {

        Student student = new Student();
        //student.test("阿拉璨");
        student.test1();
    }
}
package com.Alacan.oop.demo05;

//学生 is 人 : 派生类 , 子类
//子类继承父类,就会拥有父亲的全部方法!
public class Student extends Person{

    private String name = "alacan";

    public void print(){
        System.out.println("Student");
    }
    public void test1() {
        print();
        this.print();
        super.print();
    }

    public void test(String name) {
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }
}
package com.Alacan.oop.demo05;

//在Java中,所有的类,都默认间接继承object
//Person人 : 父类
public class Person {
    protected String name = "Alacan";

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

}

输出结果:

Student Student Person

Process finished with exit code 0

package com.Alacan.oop.demo05;

public class Application {

    public static void main(String[] args) {

        Student student = new Student();
        
        //student.test("阿拉璨");
        //student.test1();
    }
}
package com.Alacan.oop.demo05;

//学生 is 人 : 派生类 , 子类
//子类继承父类,就会拥有父亲的全部方法!
public class Student extends Person{

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

    private String name = "alacan";

    public void print(){
        System.out.println("Student");
    }
    public void test1() {
        print(); //student
        this.print(); //student
        super.print(); //student
    }

    public void test(String name) {
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }
}
package com.Alacan.oop.demo05;

//在Java中,所有的类,都默认间接继承object
//Person人 : 父类
public class Person {

    public Person(){
        System.out.println("Person无参执行了");
    }
    protected String name = "Alacan";

    //私有的东西无法被继承!
    public void print(){
        System.out.println("Person");
    }

}

输出结果:

Person无参执行了 Student无参执行了

Process finished with exit code 0

总结:

super注意点:

1.super调用父类的构造方法,必须在构造方法的第一个

2.super必须只能出现在子类的方法或者构造方法中!

3.super 和 this不能同时调用构造方法!

Vs this :

代表的对象不同:

this:本身调用者这个对象

super:代表父类对象的应用

前提:

this:没有继承也可以使用

super:只能在继承条件小才可以使用

构造方法:

this();本类的构造!

super():父类的构造!

Java面向对象11:方法重写

package com.Alacan.oop.demo05;

public class Application {

    public static void main(String[] args) {
        A a = new A();
        a.test();
    }
}
package com.Alacan.oop.demo05;

public class A extends B{
    public static void test(){
        System.out.println("A=>test()");

    }
}
package com.Alacan.oop.demo05;

//重写是方法的重写,和属性无关
public class B {

    public static void test(){
        System.out.println("B=>test()");
    }
}

输出结果:

A=>test()

Process finished with exit code 0

package com.Alacan.oop.demo05;

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.Alacan.oop.demo05;
//继承
public class A extends B{
    public static void test(){
        System.out.println("A=>test()");

    }
}
package com.Alacan.oop.demo05;

//重写是方法的重写,和属性无关
public class B {

    public static void test(){
        System.out.println("B=>test()");
    }
}

输出结果:

A=>test() B=>test()

Process finished with exit code 0

静态方法重写:

package com.Alacan.oop.demo05;

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.Alacan.oop.demo05;
//继承
public class A extends B{
    //Override 重写
    @Override //注解:有功能的注释!
    public void test() {
        System.out.println("A=>test()");
    }
}
package com.Alacan.oop.demo05;

//重写是方法的重写,和属性无关
public class B {

    public void test(){
        System.out.println("B=>test()");
    }
}

重写总结:

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

1.方法名必须相同

2.参数列表必须相同

3.修饰符:范围可以扩大: public >Protected > Defalut > private

4.抛出的异常:范围,可以被缩小,但不能扩大:ClassNotfoundException - - -> Exception (大)

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

为什么需要重写?

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

Alt + Insert : override 重写

Java面向对象12:什么是多态

动态的编译:类型:可扩展性

即同一方法可以根据发送对象的不同而采用多种不同的行为方式。

一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多

多态存在的条件:

有继承关系

子类重写父类方法

父类引用指向子类对象

注意:多态是方法的多态,属性没有多态性

instanceof

子类继承父类的全部方法:

package com.Alacan.oop.demo06;

public class Student extends Person {
}
package com.Alacan.oop.demo06;

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

        //一个对象的实际类型是确定的
        //new Student();
        //new Person();

        //可以指向的引用类型就不确定了:父类的引用指向子类的类型
        Student s1 = new Student();
        Person  s2 = new Student();
        Object  s3 = new Student();

        s2.run();
    }
}
package com.Alacan.oop.demo06;

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

输出结果:

run

Process finished with exit code 0

重写:

package com.Alacan.oop.demo06;

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

        //一个对象的实际类型是确定的
        //new Student();
        //new Person();

        //可以指向的引用类型就不确定了:父类的引用指向子类的类型
        Student s1 = new Student();
        Person  s2 = new Student();
        Object  s3 = new Student();

        s2.run(); //子类重写父类的方法,执行子类的方法
        s1.run();
    }
}
package com.Alacan.oop.demo06;

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

执行结果:

son son

Process finished with exit code 0

ge com.Alacan.oop.demo06;

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

        //一个对象的实际类型是确定的
        //new Student();
        //new Person();

        //可以指向的引用类型就不确定了:父类的引用指向子类的类型
        
        //Student 能调用的方法都是自己的或者继承父类的
        Student s1 = new Student();
        //Person父类型,可以指向子类,但是不能调用子类独有的方法
        Person  s2 = new Student();
        Object  s3 = new Student();

        //对象能执行哪些方法,主要看对象左边类型,和右边关系不大
        ((Student)s2).eat(); //子类重写父类的方法,执行子类的方法
        s1.eat();
    }
}
package com.Alacan.oop.demo06;

public class Student extends Person {
    @Override
    public void run() {
        System.out.println("son");
    }
    public void eat(){
        System.out.println("eat");
    }
}

输出结果:

eat eat

Process finished with exit code 0

多态注意事项:

1.多态是方法的多态,属性没有多态

2.父类和子类,有联系 类型转换异常! ClassCastException!

3.存在条件:继承关系,方法需要重写,父类的引用指向子类对象!

Father f1 = new Son( ) ;

哪些方法不能被重写?

 1. static  方法,属于类,它不属于实例                          

   2. final  常量
  3. private 方法;

Java面向对象13:instanceo和类型转换

测试代码:

package com.Alacan.oop.demo06;

public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        Object object = new Student();

        System.out.println(object instanceof Student); //true
        System.out.println(object instanceof Person);  //true
        System.out.println(object instanceof Object);  //true
        System.out.println(object instanceof Teacher); //False
        System.out.println(object instanceof String);  //False

    }
}

执行结果:

true true true false false

Process finished with exit code 0

package com.Alacan.oop.demo06;

public class Application {

    public static void main(String[] args) {
        /*
        Object > String
        Object > Person > Teacher
        Object > Person > Student
        */

        //System.out.println(X  instanceof  Y); 能不能编译通过!
        //X Y 之间是否存在父子关系  存在父子关系编译通过  不存在父子关系编译不通过

        Object object = new Student();

        System.out.println(object instanceof Student); //true
        System.out.println(object instanceof Person);  //true
        System.out.println(object instanceof Object);  //true
        System.out.println(object instanceof Teacher); //False
        System.out.println(object instanceof String);  //False
        System.out.println("========================================");
        Person person = new Student();
        System.out.println(person instanceof Student); //true
        System.out.println(person instanceof Person);  //true
        System.out.println(person instanceof Object);  //true
        System.out.println(person instanceof Teacher); //False
        //System.out.println(person instanceof String);  //编译报错!
        System.out.println("========================================");
        Student student = new Student();
        System.out.println(student instanceof Student); //true
        System.out.println(student instanceof Person);  //true
        System.out.println(student instanceof Object);  //true
        //System.out.println(student instanceof Teacher); //编译报错!
        //System.out.println(student instanceof String);  //编译报错!
    }
}

执行结果:

true true true false false

true true true false

true true true

Process finished with exit code 0

类型转换:

高转低强制转换:

package com.Alacan.oop.demo06;

public class Student extends Person {
    public void go(){
        System.out.println("go");
    }
}
package com.Alacan.oop.demo06;

public class Application {

    public static void main(String[] args) {
        
        //类型之间的转换: 基本类型转换 高低64 32 16 8
        //父: 高类  子:低类

        //高 --------> 低    强制转换
        
        Person obj = new Student();

        //student将这个对象转换为student类型,我们就可以使用student类型的方法了!
        //Student student = (Student) obj;
        //student.go();
        
        ((Student)obj).go(); 
        //由上面两句话转换成一句话

    }
}
package com.Alacan.oop.demo09;

// interface 定义的关键字 接口都需要实现类
public interface UserService {
    //接口中所有定义都是抽象的 public abstract
    //void run(String name);
    void add(String name);    //增
    void delete(String name); //删
    void update(String name); //改
    void query(String name);  //查
    
}
package com.Alacan.oop.demo06;

public class Application {

    public static void main(String[] args) {
        //类型之间转换    父      子

        //子类转换为父类,可能丢失自己本来的一些方法
        Student student = new Student();
        student.go();
        Person person = student;   //student 转 person类型 低转高自动转过去
    }
}

总结:

1.父类引用指向子类的对象

2.把子类转换为父类,向上转型:自动转换

3.把父类转换为子类,向下转型:强制转换

4.方便方法调用,减少重复代码!简洁

抽象: 封装 继承 多态! 抽象类 接口

Java面向对象14:static 关键字详解

静态属性:

//static : 在类中使用修饰成员变量   在方法中使用修饰成员方法 方法上静态方法 属性中静态属性
public class Student { //修改类名 Alt + Enter

    private static int age; //静态的变量   多线程!
    private double score;   //非静态变量
    
    public static void main(String[] args) {
        Student s1 = new Student(); //Student student = new Student();=new student();  ctrl + alt + v

        System.out.println(Student.age);//Student.age sout + Enter
        System.out.println(s1.age);
        System.out.println(s1.score);

    }

}

静态方法:

package com.Alacan.oop.demo07;

//static : 在类中使用修饰成员变量   在方法中使用修饰成员方法 方法上静态方法 属性中静态属性
public class Student {

    private static int age; //静态的变量   多线程!
    private double score;   //非静态变量

    public void run(){
        //go();非静态方法可以调用静态方法里面的所有东西
        //静态方法可以调用静态方法的不能调用普通方法
    }

    public static void go(){

    }

    public static void main(String[] args) {
        //new Student().run();
        go();

    }

}

静态代码块:

package com.Alacan.oop.demo07;

public class Person {
    
    {
        //代码块(匿名代码块) 没有名字程序执行并不能主动调用
    }
    static{
        //静态代码块 永久只执行一次
    }
    
}
package com.Alacan.oop.demo07;

public class Person {
    //2.
    {
        System.out.println("匿名代码块");
    }
    //1.
    static {
        System.out.println("静态代码块");
    }
    //3.
    public Person() {
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Person person = new Person();
    }
}

执行结果:

静态代码块 匿名代码块 构造方法

Process finished with exit code 0

package com.Alacan.oop.demo07;

public class Person {
    //2. 赋初始值
    {
        System.out.println("匿名代码块");
    }
    //1. 只执行一次
    static {
        System.out.println("静态代码块");
    }
    //3.
    public Person() {
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Person person1 = new Person();
        System.out.println("========================================");
        Person person2 = new Person();
    }
}

执行结果:

静态代码块

匿名代码块

构造方法

匿名代码块

构造方法

Process finished with exit code 0

package com.Alacan.oop.demo07;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;

public class Test {

    public static void main(String[] args) {
        //System.out.println(Math.random()); //随机数 每次执行结果不同
        System.out.println(random());
        System.out.println(PI);
    }
}

Java面向对象15:抽象类

抽象类

abstract修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么方法就是抽象方法;如果修饰类,那么类就是抽象类。

抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明为抽象类。

抽象类,不能使用new关键字来创建对象,它是用来让子类继承的。

抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的。

子类继承抽象类,那么必须要实现抽象类没有实现的抽象方法,否则该子类也要声明为抽象类。

package com.Alacan.oop.demo08;

//abstract 抽象类
public abstract class Action {
    
    //约束 有人帮我们实现
    //abstract ,抽象方法,只有方法名字,没有方法的实现!
    public abstract void doSomething();
    
    //1.不能 new 这个抽象类,只能靠子类去实现它:约束!
    //2.抽象类中可以写普通方法
    //3.抽象方法必须在抽象类中
    //抽象的抽象:约束
    
    
}
package com.Alacan.oop.demo08;

//抽象类的所有方法,继承了它的子类,都必须实现它的方法,除非他的子类是 abstract那就不用实现本身是抽象的
public class A extends Action{

    @Override
    public void doSomething() {

    }
}

Java面向对象16:接口的定义与实现

接口:

普通类:只有具体实现

抽象类:具体实现和规范(抽象方法)都有!

接口:只有规范!

接口就是规范,定义的一组规则,体现了现实世界中“如果你是…则必须能…”的思想。如果你是天使则必须能飞。如果你是汽车,则必须能跑。如果你是好人,则必须干掉坏人;如果你是坏人,则必须欺负好人。

接口的本质是契约,就像法律一样,制定及遵守。

OO的精髓,是对对象的抽象,最能体现这一点的就是接口。为什么我们讨论设计 模式都只针对具备了抽象能力的语言(比如c++、Java、c#等),就是因为设计模式所研究的,实际上就是如何合理的去抽象。

声明类的关键字是class,声明接口的关键字是interface

写简单的接口:

package com.Alacan.oop.demo09;

// interface 定义的关键字 接口都需要实现类
public interface UserService {
    //接口中所有定义都是抽象的 public abstract
    //void run(String name);
    void add(String name);    //增
    void delete(String name); //删
    void update(String name); //改
    void query(String name);  //查
    
}
package com.Alacan.oop.demo09;

//抽象类 :extends
//类 可以实现接口   implements 接口
//实现接口的类,就需要重写接口中的方法
public class UserServiceImp1 implements UserService {
    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }
}

多个方法

package com.Alacan.oop.demo09;

public interface TimeService {
    void timer();
}
package com.Alacan.oop.demo09;

//抽象类 :extends
//类 可以实现接口   implements 接口
//实现接口的类,就需要重写接口中的方法

//多继承 利用接口实现多继承
public class UserServiceImp1 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.Alacan.oop.demo09;

//抽象的思维 Java 架构师
// interface 定义的关键字 接口都需要实现类
public interface UserService {
    //void run(String name);

    //常量 public static final
    int AGE = 99;

    //接口中所有定义都是抽象的 public abstract
    void add(String name);    //增
    void delete(String name); //删
    void update(String name); //改
    void query(String name);  //查

}

总结接口的作用:

1.约束

2.定义一些方法,让不同的人实现 10 ---> 1

3.public abstract

4.public static final

5.接口不能被实例化,接口中没有构造方法

6.inplements可以实现多个接口

7.必须要重写接口中的方法

Java面向对象17:N种内部类

内部类就是在一个类的内部在定义一个类,比如,A类中定义一个B类,那么B类相对A类来说就称为内部类,而A类相对B类来说就是外部类了。

1.成员内部类

2.静态内部类

3.局部内部类

4.匿名内部类

成员内部类:

package com.Alacan.oop.demo10;

public class Outer {

    private int id;
    public void out(){
        System.out.println("这是外部类的方法");
    }
    public class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }
    }
}
package com.Alacan.oop;

import com.Alacan.oop.demo10.Outer;

public class Application {
    public static void main(String[] args) {
        //new

        Outer outer = new Outer();
        //通过这个外部类来实例化内部类
        Outer.Inner inner = outer.new Inner();
        inner.in();
    }
}

执行结果:

这是内部类的方法

Process finished with exit code 0

外部类来实例化内部类:

package com.Alacan.oop.demo10;

public class Outer {

    private int id=18;
    public void out(){
        System.out.println("这是外部类的方法");
    }
    public class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }
        //获得外部类私有属性
        public void getID(){
            System.out.println(id);
        }
    }
}
package com.Alacan.oop;

import com.Alacan.oop.demo10.Outer;

public class Application {
    public static void main(String[] args) {
        //new

        Outer outer = new Outer();
        //通过这个外部类来实例化内部类
        Outer.Inner inner = outer.new Inner();
        inner.getID();
    }
}

执行结果:

18

Process finished with exit code 0

静态内部类:

package com.Alacan.oop.demo10;

public class Outer {

    private int id=18;
    public void out(){
        System.out.println("这是外部类的方法");
    }
    public static class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }
    }
}
package com.Alacan.oop;

import com.Alacan.oop.demo10.Outer;

public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();

    }
}
扩展:
package com.Alacan.oop.demo10;

public class Outer {
    //局部内部类
    public void method(){
        class Inner{
            public void in(){
                
            }
        }
    }

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

    }
}
package com.Alacan.oop.demo10;

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

异常

Java异常01:Error和Exception

实际工作中,遇到情况不可能是非常完美的。比如:你写的某个板块,用户输入不一定符合你的要求、你的程序要打开某个文件,这个文件可能不存在或者文件格式不对,你要读取数据库的数据,数据可能是空的等。我们的程序在跑着,内存或硬盘可能满了。等等

软件程序在运行过程中,非常可能遇到刚刚提到的这些异常问题,我们叫异常,英文是:Exception,意思是例外。这些,例外情况,或者叫异常,怎么让我们写的程序做出合理的处理。而不至于程序崩溃。

异常指程序运行中出现的不期而至的各种状况,如:文件找不到、网络连接失败、非法参数等。

异常发生在程序运行期间,它影响了 正常程序执行流程

异常出错:

package com.exception;

public class Demo01 {
    public static void main(String[] args) {
        new Demo01().a();
    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}
package com.exception;

public class Demo01 {
    public static void main(String[] args) {
        System.out.println(11/0);
    }
}

简单分类:

要理解Java异常处理是如何工作的,需要掌握三种类型的异常:

检查性异常:最具代表的检测性异常是用户错误或问题引起的异常,这是程序员无法预见的。例如要打开一个不存在的文件时,一个异常就发生了,这些异常在编译时不能被简单的忽略。

运行时异常:运行时异常是可能被程序员避免的异常。与检测性异常相反,运行时异常可以在编译时被忽略。

错误ERROR:错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略。例如,当栈溢出时,一个错误就发生了,它们在编译也检查不到。

异常体系结构

Java把异常当作对象来处理,并定义一个基类java.lang.Throwable作为所有异常的超类

在Java API中已经定义了许多的异常类,这些异常类分为两大类,错误Error和异常Exception。

Error

Error对象类由 Java 虚拟机生成并抛出,大多数错误与代码编写者所执行的操作无关。

Java虚拟机运行错误(Virtual MachineError),当 JVM 不再继续执行操作所需的内存资源时,将出现OutOfMemoryError。这些异常发生时,Java虚拟机(JVM)一般会选择线程终止;

还有发生在虚拟机试图执行应用时,如类定义错误(NoClassDefFoundError)、链接错误(LinkageError)。这些错误是不可查的,因为它们在应用程序的控制和处理能力之外,而且绝大多数是程序运行时不允许出现的状况。

Exception

在Exception分支中有一个重要的子类RuntimeException(运行时异常)

ArrayIndexOutOfBoundsException(数组下标越界)

NullPointerException(空指针异常)

ArithmeticException(算数异常)

MissingResourceException(丢失资源)

ClassNotFoundException(找不到类)等异常,这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理。

这些异常一般是由程序逻辑错误引起的,程序应该从逻辑角度尽可能避免这类异常的发生;

Error和exception的区别:Error通常是灾难性的致命的错误,是程序无法控制和处理的,当出现这些异常时,Java虚拟机(JVM)一般会选择终止线程;Exception通常情况下是可以被程序处理的,并且在程序中应该尽可能的去处理这些异常。

Java异常02:捕获和抛出异常

异常处理机制: 抛出异常

捕获异常

异常处理5个关键字

try、catch、finally、throw、throws

package com.exception;

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

        int a = 1;
        int b = 0;
        
            System.out.println(a/b);
       
    }
}

执行结果:

Exception in thread "main" java.lang.ArithmeticException: / by zero at com.exception.Test.main(Test.java:8)

Process finished with exit code 1

package com.exception;

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

执行结果:

程 序出现异常,变量b不能为0 finally

Process finished with exit code 0

package com.exception;

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

        int a = 1;
        int b = 0;
        try{    //try监控区域
            System.out.println(a/b);
        }catch(Error e){   //catch (想要捕获的异常类型!) 捕获异常
            System.out.println("Error");
        }catch (Exception e){
            System.out.println("Exception");
        }catch(Throwable t){
            System.out.println("Throwable");
        }finally {    //处理善后工作
            System.out.println("finally");
        }
        //finally 可以不要finally,假设IO,资源,关闭!
    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}

输出结果:

Exception finally

Process finished with exit code 0

快捷键:

package com.exception;


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

        // Ctrl + Alt + T    选中System.out.println(a/b);+ 快捷键
        try {
            System.out.println(a/b);
        } catch (Exception e) {
            System.exit(1);
            e.printStackTrace(); //打印错误的栈信息
        } finally {
        }
    }
}

执行结果:

Exception finally

Process finished with exit code 0

异常处理:

package com.exception;

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

        int a = 1;
        int b = 0;
        try{    //try监控区域
            if (b==0){  // throw  throws
                throw new ArithmeticException();  //主动抛出异常
            }
            System.out.println(a/b);
        }catch(Error e){   //catch (想要捕获的异常类型!) 捕获异常
            System.out.println("Error");
        }catch (Exception e){
            System.out.println("Exception");
        }catch(Throwable t){
            System.out.println("Throwable");
        }finally {    //处理善后工作
            System.out.println("finally");
        }
        //finally 可以不要finally,假设IO,资源,关闭!
    }
    public void a(){
        b();
    }
    public void b(){
        a();
    }
}

执行结果后发现并没有变化所以它是写在方法里的

Exception finally

Process finished with exit code 0

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

        try {
            new Test().test(1,0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        } finally {
        }

        //finally 可以不要finally,假设IO,资源,关闭!
    }
    //假设这方法中 ,处理不了这个异常 ,方法上抛出异常
    public void test(int a,int b) throws ArithmeticException{
        if (b==0){  // throw  throws
            throw new ArithmeticException();  //主动抛出异常 一般在方法中使用
        }
        System.out.println(a/b);
    }
}

Java异常03:自定义异常及小结

使用Java内置的异常类可以描述在编程时出现的大部分异常情况。除此之外,用户还可以自定义异常。用户自定义异常,只需继承Exception类即可。

在程序中使用自定义异常类,大体可分为几个步骤:

1.创建自定义异常类

2.在方法中通过throw关键字抛出异常对象。

3.如果在当前抛出异常的方法中处理异常,可以使用try-catch语句捕获并处理;

否则在方法的声明处通过throws关键字指明要抛出给方法调用者的异常,继续进行下一步操作。

4.在出现异常方法的调用者中捕获并处理异常。

异常类:

package com.exception.demo02;

//自定义的异常类
public class MyException extends Exception{
    
    //传递数字 > 10;
    private  int deltail;

    public MyException(int a) {
        this.deltail = a;
    }
    //toString : 异常的打印信息
    @Override
    public String toString() {
        return "MyException{" + deltail + '}';
    }
}

测试类:

package com.exception.demo02;

public class Test {
    //可能会存在异常的方法
    static void test(int a) throws MyException {

        System.out.println("传递的参数为:"+a);

        if (a>10){
            throw new MyException(a); //抛出
        }
        System.out.println("OK");
    }

    public static void main(String[] args) {
        try {
            test(1);
        } catch (MyException e) {
            System.out.println("MyException=>"+e);
        }
    }
}

执行结果:

传递的参数为:1 OK

Process finished with exit code 0

异常情况:

package com.exception.demo02;

public class Test {
    //可能会存在异常的方法
    static void test(int a) throws MyException {

        System.out.println("传递的参数为:"+a);

        if (a>10){
            throw new MyException(a); //抛出
        }
        System.out.println("OK");
    }

    public static void main(String[] args) {
        try {
            test(18);
        } catch (MyException e) {
            System.out.println("MyException=>"+e);
        }
    }
}

执行结果: 传递的参数为:18 MyException=>MyException{18}

Process finished with exit code 0

第二种方法:

package com.exception.demo02;

public class Test {
    //可能会存在异常的方法
    static void test(int a)  {

        System.out.println("传递的参数为:"+a);

        if (a>10){
            try {
                throw new MyException(a); //抛出
            } catch (MyException e) {
                e.printStackTrace();
            }
        }
        System.out.println("OK");
    }

    public static void main(String[] args) throws MyException {
        test(1);
    }
}

执行结果:

传递的参数为:1 OK

Process finished with exit code 0

实际应用中的总结:

处理运行异常时,采用逻辑去合理规避同时辅助 try-catch处理

在多重catch块后面,可以加一个catch(Exception)来处理可能会被遗漏的异常

对于不确定的代码,也可以加上try-catch,处理潜在异常

尽量去处理异常,切忌只是简单地调用printStackTrace()去打印输出

具体如何处理异常,要根据不同的业务需求和异常类型去决定

尽量添加finally语句块去释放占用资源

  • 35
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值