文章目录
第一章 java语言概论
一、基础概念:
应用程序 =算法 +数据结构
- 计算机语言的发展迭代史
第一代:机器语言
第二代:汇编语言
第三代:高级语言
> 面向过程:C,Pascal、Fortran
> 面向对象:Java,JS,Python,Scala,…
二、常见的DOS命令
1. dir 列出当前的文件及文件夹
2. md 创建目录
3. re 删除目录
4. cd 进入指定的目录
5. cd.. 退回上一级目录
6. cd\ 退回到根目录
7. exit 退出dos命令行
第二章 基本数据类型
关键字
基本数据类型
一、基本的数据类型:
1. 数值型:
整数类型: byte(1 byte) 、 short(2 byte) 、 int(4 byte 默认)、 long(8 byte 需要加 : l or L)
浮点型 : float(4 byte 需要加 f or F) 、 double(8 byte 默认)
字符型: char(2 byte)
布尔型: boolean(1 byte)
2. 引用类型:
类 : class
接口: interface
数组: []
字符串: String
Lambda
常量
一、概念:
在程序中不会发生该遍的量 称之为常量
变量
一、变量的定义格式
//第一种声明格式
int demo =10;
//第二种声明格式
int demo_01; //未赋值不能使用
demo_01=10;
二、变量使用的注意点:
① 变量必须先声明,后使用
② 变量都定义在其作用域内。在作用域内,它是有效的。换句话说,出了作用域,就失效了
③ 同一个作用域内,不可以声明两个同名的变量
三.基本数据类型变量间运算规则
1 boolean不参与运算 之外的7种可以运算
2 自动类型转换(只涉及7种基本数据类型)
结论:当容量小的数据类型的变量与容量大的数据类型的变量做运算时,结果自动提升为容量大的数据类型。
byte 、char 、short --> int --> long --> float --> double
特别的:当byte、char、short三种类型的变量做运算时,结果为int型
说明:此时的容量大小指的是,表示数的范围的大和小。比如:float容量要大于long的容量
3 强制类型转换(只涉及7种基本数据类型):自动类型提升运算的逆运算。
1.需要使用强转符:()
2.注意点:强制类型转换,可能导致精度损失。
4 String与8种基本数据类型间的运算
- String属于引用数据类型,翻译为:字符串
- 声明String类型变量时,使用一对""
- String可以和8种基本数据类型变量做运算,且运算只能是连接运算:+
- 运算的结果仍然是String类型
避免:
String s = 123;//编译错误
String s1 = "123";
int i = (int)s1;//编译错误
第三章 运算符和表达式
1. 算数运算符
算数运算符
+ - * / % (前)++ (后)++ (前)- - (后) - -
//除号: /
int num1=10,num2=3;
//整数相除会精度丢失
int result=num1/num2;
System.out.println(result); // 3
//取余: %
System.out.println(10%3); // 1
System.out.println(3%10); // 3
System.out.println(-10%3); //-1
System.out.println(-10%-3); //-1
System.out.println(10%-3); //1
System.out.println(3%-10); //3
System.out.println(-3%-10); //-3
【特别说明的】 ++ 之间的区别
//前++ 和 后++ 的区别
int num4 = 20;
System.out.println(++num4); //【先加后用】 21
System.out.println(num4); //21
int num5 = 20;
System.out.println(num5++); //【先用后加】 20
System.out.println(num5); // 21
连接符:+:只能使用在String与其他数据类型变量之间使用。
2. 赋值运算符
赋值运算符 :
= += -= *= /= %=
1、连续赋值:
int num1,num2;
//连续赋值
num1=num2=10;
System.out.println(num1); //10
System.out.println(num2); //10
2、+=的使用:
num1+=5; //num1=num1+5
System.out.println(num1); //15
3. 比较运算符(关系运算符)
关系运算符:
== != > < >= <=
== 和 = 的区别:
int num1=10;
int num2=20;
// == 是比较 = 是赋值
System.out.println(num1==num2);//false
System.out.println(num1=num2); //20
【注意事项】
1. 比较运算符的结果是 boolean 类型
4. 逻辑运算符
逻辑运算符:
& && | || ! ^
一、案列演示:
// && 逻辑与 同真则真 一假则假
System.out.println(true&&true); //true
System.out.println(true&&false);//false
// || 逻辑或 一真则真 同假则假
System.out.println(true||false); //true
System.out.println(false||false); //false
System.out.println(true||true); //true
// ! 逻辑非 非真即假 非假即真
System.out.println(!(true)); //false
System.out.println(!(false)); //true
【特别说明】:
一、区分& 与 &&:
相同点:
1. & 与 && 的运算结果相同
2. 当符号左边是true时,二者都会执行符号右边的运算
不同点:
当符号左边是false时,&继续执行符号右边的运算,&&不再执行符号右边的运算。
推荐使用&&
System.out.println(true&true); //true
System.out.println(true&false);//false
二、区分 | 与 || 的区别:
相同点:
| 与 || 的运算结果相同
当符号左边是false时,二者都会执行符号右边的运算
不同点3:
当符号左边是true时,|继续执行符号右边的运算,而||不再执行符号右边的运算
推荐使用||
System.out.println(true|false); //true
System.out.println(false|false); //false
System.out.println(true|true); //true
三、逻辑运算符的两边都必须是 boolean 类型;
5. 位运算
位运算 : << >> >>> & | ^ ~
位运算符:
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0 >>2 就相当于往后面移了 2倍
int num =10;
//向左移: <<
int result =num<<2; // 10*2的2次方 10*2*2 =40
int result1 =num<<3; // 10*2的3次方 10*2*2*2 =80
System.out.println(result); //40
System.out.println(result1); //80
//向右移: >>
int result2=num>>2; // 10的根号2又2
int result3=num>>3; // 10的根号2又2又2
int result4=num>>7; // 10的根号....
System.out.println(result2); //2
System.out.println(result3); //1
System.out.println(result4); //0
int m=12;
int n=5;
128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 //12
0 0 0 0 0 1 0 1 //5
//------------------------------------------
0 当作 false 1 当作 true
& 0 0 0 0 0 1 0 0 //12&5 =4
System.out.println(m&n); //4
System.out.println(m|n); //13
System.out.println(m^n); //9
6. 三元运算符
三元运算符
(条件表达式)? 表达式1 : 表达式2
一、演示:
int x = 10;
int y = 5;
int z=(7>4)? x:y;
7>4 成立 返回 x 否则返回 y
System.out.println(z); //10
//嵌套使用
int num=(x<y)? ((7>5)? 4:1) :((6>4)? 2:3);
System.out.println(num); //2
【特别说明】:
1. 凡是用到了 三元运算符都可以转为 if-else
2. 三元运算符的优先级 大于 if-else
【原因】:简洁 执行效率高
第四章 流程控制、数组
分支
if -else 演示:
1. 结构1
if(5>4){
System.out.println("我爱编程..."); //为 true 所以执行
}
2. 结构2
if(false){
System.out.println("嘿嘿嘿"); //为 false 所以执行下面的
}else{
System.out.println("我爱编程..."); // 执行
}
3. 结构3
if(false){
System.out.println(); //不执行
}else if (true){
System.out.println(); //执行
}else{
System.out.println(); // 不执行
}
switch 演示
switch 演示:
int x=10;
switch (x+20){
case 20:
System.out.println(20);
break; //可选
case 30:
System.out.println(30);
break; //不加的话 会默认穿透下面的 直到遇见 break
case 10:
System.out.println(10);
break;
default: //可选
System.out.println(50);
break;
}
循环
一、循环的三种结构:
1. for结构:
for (int i = 0; i < 10; i++) {
System.out.println("我爱编程"+i); //执行10遍
}
【细节说明】:
循环四要素
初始条件: int i=0; ①
循环条件:(boolean类型、false跳出) i<10; ②
循环体 :System.out.println("我爱编程"+i);③
迭代条件: i++ ④
执行顺序 ①-> ②->③->④-> ②->③->④->
//--------------------------------------------------------------
2.while结构:
int i=0;
while (i<10){
System.out.println("我爱编程..."+i); //执行10遍
i++;
}
//--------------------------------------------------------------
3.do-while结构:
int i=0;
do {
//不管怎么样都会执行一次
System.out.println("我爱编程..."+i); //执行10次
i++;
}while(i<10);
}
【区别】:
for i的作用域在里面
while i的作用域在外面
do—while 无论都会执行一次
//--------------------------------------------------------------
二、循环可以嵌套:
1.当外层要执行m次,内层要执行n次。此时内层循环的循环体一共执行了m * n次
2.外层循环控制行数,内层循环控制列数
3.内层循环结构遍历一遍,只相当于外层循环循环体执行了一次
三、break 和 continue 关键字使用
break : 结束当前循环 【嵌套循环可以加标签使用 tag:】
continue: 结束当次循环 继续执行下次循环;
【补充】:return 结束所在的 方法中;
Scanner 类的使用 【补充】
一、全在代码里:
//1.导包:import java.util.Scanner;
Scanner scan =new Scanner(System.in);
System.out.println("请输入你的姓名..");
//获取键盘输入的 String 类型
String name=scan.next();
System.out.println(name);
System.out.println("请输入你的芳龄..");
//获取键盘输入的 int 类型
System.out.println(scan.nextInt());
System.out.println("是否很帅..");
//使用匿名的方法 获取键盘输入的 Boolean 值
System.out.println(new Scanner(System.in).nextBoolean());
数组
一维数组
1.数组的定义格式:
//方式一
int[] array = new int[5];
System.out.println(array); //是一个地址
//方式二
int[] array1 = new int[]{1, 2, 3, 4, 5};
array1[0] = 5;
//方式三
int[] array2 = {1, 2, 3, 4, 5};
System.out.println(array2[0]);
2. 获取数组的长度: array.length
System.out.println(array.length); //5
3. 遍历数组:
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
二维数组
1.二维数组的声明格式:
//方式一
int[][] array = new int[3][4];
//方式二
int[][] array1;
array1 = new int[][]{{1, 2}, {2, 3}, {4, 5}};
//方式三
int[][] array2 = {{1, 2}, {5, 3}, {6, 4}};
array2[2][1] = 100;
3.二维数组的遍历:
for (int i = 0; i < array2.length; i++) {
for (int j = 0; j < array2[i].length; j++) {
System.out.print(array2[i][j]);
}
System.out.println();
}
}
附加:数组常见的算法:
一、杨辉三角:
//杨辉三角
int[][] arr = new int[10][];
for (int i = 0; i < arr.length; i++) {
arr[i] = new int[i + 1];
arr[i][0] = arr[i][i] = 1;
if (i > 1) {
for (int j = 1; j < arr[i].length - 1; j++) {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(" " + arr[i][j]);
}
System.out.println();
}
二、求数组中元素的 max、min 、avg、sum
int[] array=new int[]{1,5,6,-1,10};
int max,min;
int sum =0;
max=min=array[0];
for (int i = 0; i < array.length; i++) {
if (max<array[i]){
max=array[i];
}
if (min>array[i]){
min=array[i];
}
sum+=array[i];
}
System.out.println("最大值:"+max);
System.out.println("最小值"+min);
System.out.println("平均值:"+(float)sum/array.length);
System.out.println("总和:"+sum);
}
三、数组反转:
//方法一
int[] array ={1,5,6,-1,10};
for (int i = 0; i < array.length/2; i++) {
int temp;
temp=array[i];
array[i]=array[array.length-1-i];
array[array.length-1-i]=temp;
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
}
//方法二
int[] array ={1,5,6,-1,10};
for (int i = 0,j=array.length-1; i <j ; i++,j--) {
int temp;
temp=array[i];
array[i]=array[array.length-1-i];
array[array.length-1-i]=temp;
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+" ");
}
}
四、查找
//线性查找(效率低)
int[] array ={1,5,6,-1,10};
int tag=-1;
for (int i = 0; i < array.length; i++) {
if (tag==array[i]){
System.out.println("找到了... 是:"+tag);
break;
}else if (i+1==array.length){
System.out.println("没找到...");
}
}
}
//二分查找
二分查找
注意 使用二分查找的时候 数组必须是有序的
public class text_01{
public static void main(String[] args) {
int[] array = new int[]{-4, -1, 5, 8, 9,10};
int head=0; //头
int tail =array.length-1; //尾巴
int tag =-1; //要寻找的值
boolean ifflag=true; //判断有没有找到值...
while (head<=tail){
int middle=(head+tail)/2;
if (array[middle]==tag){
System.out.println("找到了"+array[middle]+" 下标是"+middle);
ifflag=false;
break;
}else if(tag<array[middle]){
tail=middle-1;
}else if (tag>array[middle]){
head=middle+1;
}
}
if (ifflag){
System.out.println("没有找到...");
}
}
}
五、排序
//冒泡排序:
冒泡排序:
public static void main(String[] ages){
int[] array =new int[]{1,2,5,7,8,3,2,10};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length-i-1; j++) {
if(array[j]>array[j+1]){
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
//打印排好的值
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
}
}
Arrays工具包的使用
一、boolean equals(int[] a,int[] b)
boolean equals(int[] a,int[] b)
//判断两个数是否相等
int[] arr1 =new int[]{1,5,8};
int[] arr2 =new int[]{5,8,1};
int[] arr3 =new int[]{1,5,8};
boolean tag = Arrays.equals(arr1,arr2);
boolean tag1 = Arrays.equals(arr1,arr3);
System.out.println(tag); //false
System.out.println(tag1); //true
二、String toString(int[] a)
String toString(int[] a)
//输出数组的信息 返回[1,2]的格式
int[] arr1 =new int[]{1,5,8};
System.out.println(Arrays.toString(arr1)); //[1,5,8]
三、void fill(int[] a,int b)
void fill(int[] a,int b)
//将指定的数填充到数组中
int[] arr1 =new int[]{1,5,8,1,8,20,30,40};
Arrays.fill(arr1,10); //全部都会变成 10
System.out.println(Arrays.toString(arr1));
四、void sort(int[] a)
void sort(int[] a)
//将指定的数组进行排序
int[] arr1 =new int[]{1,5,8,1,8,20,30,40};
Arrays.sort(arr1); //排好序 asc升序
System.out.println(Arrays.toString(arr1));
五、int binarySearch(int[] a,int key)
int binarySearch(int[] a,int key)
//对排序后的数组进行查找
int[] arr1 =new int[]{1,5,8,1,8,20,30,40};
Arrays.sort(arr1); //排好序 asc升序
System.out.println(Arrays.toString(arr1));
int index =Arrays.binarySearch(arr1,5);
//index 比0 大 说明找到了
if(index>=0){
System.out.println("索引为:"+index);
}else{
System.out.println("没有这个数...");
}
第五章 java面向对象的核心逻辑
OPP
面对对象思想
public class oop_03 {
public static void main(String[] args){
创建一个 student 对象:
student stu =new student();
给 成员变量赋值:
stu.name="tom";
stu.age=18;
System.out.println(stu.name+":"+stu.age);
调用 成员方法:
stu.eat();
stu.sleep(10);
System.out.println(stu.talk(stu.name));
}
}
创建一个类:
class student{
创建 成员变量
String name;
int age;
创建 成员方法:
public void eat(){
System.out.println("吃...");
}
public void sleep(int number){
System.out.println("睡 "+number);
}
public String talk(String who){
return "和"+who+"说话";
}
}
方法的重载
一、方法重载的定义:
在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可。
总结: "两同一不同":
同一个类、相同方法名
参数列表不同:参数个数不同,参数类型不同
二、如何判断是否构成方法的重载?
跟方法的权限修饰符、返回值类型、形参变量名、方法体都没关系!
可变形参 args
args 的基本格式
public void args(String... str){
System.out.println(1);
}
注意事项
1、可变参数不能与 数组产生重载 两者不能共存
/* public void args(String[] num){ //错误写法
System.out.println(2);
}*/
2.可变形参要写到形参的最后
public void args(String a,String...b){ //正确
}
/* public void args(String...b,String a){ //错误写法
}*/
递归的调用
定义:一个方法内自己调用自己本身 称为递归
public class OOP_04 {
public static void main(String[] args) {
//求1到100之间的和
方式1 循环实现
int sum=0;
for (int i = 0; i <=100; i++) {
sum+=i;
}
System.out.println(sum);
OOP_04 text =new OOP_04();
System.out.println(text.getsum(100));
}
方式2 递归实现
public int getsum(int a ){
if (a==1){
return 1;
}else{
return a+getsum(a-1);
}
}
}
封装
封装总结:
//四种权限修饰符 (小到大排序) private 缺省 protected public
// 修饰符 类内部 同一个包 不同包的子类 同一个工程
// private yes
// 缺省 yes yes
// protected yes yes yes
// public yes yes yes yes
public class OOP_04 {
public static void main(String[] args) {
person per = new person();
per.name="tom";
加了 private 关键字 不能直接调用
per.age 错误写法
正确写法
per.setAge(18); //不能直接调用 通过 setAge调用
System.out.println(per.getAge()); //获取
}
}
封装 private
class person{
成员变量
String name;
private int age; //加了 private 不能直接调用
String sex;
成员变量的封装和体现: public 提供了 getXxx 和 setXxx 的方法
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
//成员方法
public void eat(){
System.out.println("吃...");
}
}
构造器
构造器的作用:
1、创建对象
2、创建对象完成初始化
//说明:
//当显示的定义的构造器,系统就不会提供给我们默认的了;
public class text {
public static void main(String[] args){
person1 per =new person1(); //会执行构造器里面的东西
//有钟初始化的意思;
person1 per1 =new person1("tom",18);
person1 per2 =new person1("jak");
}
}
class person1{
//创建成员变量
private String name;
int age;
public boolean sex;
//创建构造器
public person1(){
System.out.println("构造器....");
}
public person1(String name){
//调用 persong1 构造器
this();
this.name=name;
System.out.println("构造器1..."+this.name+","+this.age);
}
public person1(String name,int age ){
// this(name);
this(); //必须声明在当前构造器的首行;且只能声明一个
}
//创建方法
public void eat(){
System.out.println("吃...");
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
}
package 和 import 作用
------------------------------------
关键字的使用
1、每一个 ”.“ 代表一层文件的目录
2、包,属于标识符,遵循标识符的命名规则
补充:JDK中的主要包介绍:
-----------------------------------
import:导入
* 1. 在源文件中显式的使用import结构导入指定包下的类、接口
* 2. 声明在包的声明和类的声明之间
* 3. 如果需要导入多个结构,则并列写出即可
* 4. 可以使用"xxx.*"的方式,表示可以导入xxx包下的所结构
* 5. 如果使用的类或接口是java.lang包下定义的,则可以省略import结构
* 6. 如果使用的类或接口是本包下定义的,则可以省略import结构
* 7. 如果在源文件中,使用了不同包下的同名的类,则必须至少一个类需要以全类名的方式显示。
* 8. 使用"xxx.*"方式表明可以调用xxx包下的所结构。但是如果使用的是xxx子包下的结构,则仍需要显式导入
*
* 9. import static:导入指定类或接口中的静态结构:属性或方法。
import java.util.*; //等价 import java.util.Scanner;
import static java.lang.System.*; // 输出可以写省 System
public class package_import_text_05 {
public static void main(String[] args){
new Scanner(System.in).next();
out.println("tom...."); //导入了包之后可以省略写 System
}
}
常用的快捷键
常用快捷键:
alt + / alt + enter 补全代码的声明
ctrl +1 快速修复
ctrl + shift +o 批量导包
Ctrl+] 诸如{}围起来的代码块,使用该快捷键可以快速跳转至代码块的结尾处
Ctrl+[ 同上,快速跳至代码块的开始出
Ctrl+Shift+Enter 将输入的if、for、函数等等补上{}或者;使代码语句完整
Ctrl+Delete 删除光标所在至单词结尾处的所有字符
Ctrl+BackSpace 删除光标所在至单词开头的所有字符
Ctrl+向左箭头 将光标移至前一个单词
Ctrl+向右箭头 将光标移至后一个单词
Ctrl+向上箭头 向上滚动一行
Ctrl+向下箭头 向下滚动一行
Ctrl+W 选中整个单词
Ctrl+Shift+U 切换大小写
继承
子类和父类的体现
一、定义一个父类
//定义一个父类
public class person {
String name;
int age;
public void eat(){
System.out.println("人要吃....");
}
}
二、定义一个子类来继承父类
student 为子类、超类、基类、superclass
person 为父类 、派生类、subclass
public class student extends person {
String sex;
public void sleep(){
System.out.println("人要睡觉....");
}
}
三、创建一个对象来调用
public class extend_01 {
public static void main(String[] args) {
student stu =new student();
通过子类可以调用父类里面的方法 属性等...
stu.eat();
stu.sleep();
}
}
方法的重写
一、创建一个父类person
public class person {
public void eat(){
System.out.println("人也要多吃饭...");
}
public void eat(String name){
System.out.println(name +"人也要多吃饭...");
}
}
二、创建一个子类student 来继承父类的person 并进行重写
public class student extends person{
子类继承了父类后 对方法的重写 子类对象实例化 会调用子类中的方法...
public void eat(){
System.out.println("学生要吃饭...");
}
public void eat(String name){
System.out.println(name+"学生要吃饭...");
}
}
三、创建一个子类对象实例化
方法的重写:
定义:子类继承了父类以后,可以对父类中同名同参数的方法进行覆盖操作
重写的规定:
1、子类中的方法名、形参列表和父类中的方法名、形参列表 相同
2、子类方法中的权限修饰符不小于父类方法中的权限修饰符
>特殊情况:子类不能重写父类中 peivate中的方法...
3、返回值类型:
>父类中方法返回值的类型是 void,则子类子类重写的方法的返回值类型也只能是 void
>父类中方法返回值的类型是 A类型,则子类子类重写的方法的返回值类型可以是A类或A类的子类
>父类中方法返回值的类型是 基本数据类型,则子类子类重写的方法的返回值类型必须是相同的基本数据类型
4、补充:子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常
public class overwrite {
public static void main(String[] args) {
student p =new student();
p.eat(); //调用子类中的方法
p.eat("tom.."); //调用子类中的方法
}
}
super关键字的使用
一、声明一个父类 person
public class person{
int id=100;
String name;
int age;
public person(){
}
public person(String name,int age){
System.out.println("父类构造器....");
}
public void eat(){
System.out.println("人:要吃饭...");
}
public void sleep(){
System.out.println("人:要睡觉...");
}
}
二、声明一个子类 student
public class student extends person{
String sex;
int id=111;
public student(){
会默认的调用 父类的空参
super();
}
public student(String sex){
会默认的调用 父类的空参
super();
调用父类的构造器 调用构造器 必须放在首行
super("tom",18);
}
public void eat(){
System.out.println("学生:要吃饭...");
}
public void show(){
this 会在子类中找 没找到在去父类中找
super 会直接去夫父类中找
System.out.println(this.id+"、" + super.id);
eat(); 方法重写默认使用本类中的方法
super.eat(); 直接去调用父类中的方法
}
}
三、super关键字的对象
super关键字的使用
super.属性 super.方法 super(形参列表) //调用构造器
1、super可以用来调用:父类的属性 、父类的方法、父类的构造器
2、方法被重写时、属性相同时 我们可以显示的使用
this 、 super 灵活的调用父类和子类中的方法 和属性、构造器
public class supertest {
public static void main(String[] args) {
student stu =new student(); 会默认的调用super();
stu.show();
调用父类的构造器
student stu1 =new student("dd");
}
}
多态
上下转型
package 多态;
public class 向上转型 {
private static Object animal;
public static void main(String[] args) {
// 向上转型
animal aa=new cat();
// 猫猫在叫唤
aa.cry();
// 可以调用父类中所有的成员(需要遵守访问权限)
// 不可以调用子类特有的成员
// 错误: aa.sleep();
// 向下转型:
cat bb=(cat)aa;
bb.sleep();
}
}
多态属性问题
package 多态;
public class 多态属性的问题 {
public static void main(String[] args) {
// 向上转型
A a=new B();
// 属性看编译类型
System.out.println(a.count); //10
// 向下转型
B b=(B)a;
// 看编译类型
System.out.println(b.count); //20
}
}
class A{
int count=10;
}
class B extends A {
int count=20;
}
判断是否是他的运行类型
package 多态;
public class 判断是不是它的运行类型 {
public static void main(String[] args) {
// 判断是不是它的运行类型
BB bb = new BB();
AA aa=new BB();
System.out.println(bb instanceof AA); //true
System.out.println(aa instanceof AA ); //true
System.out.println(bb instanceof BB); //true
System.out.println(aa instanceof BB); //true
Object obj =new Object();
System.out.println(obj instanceof BB ); //false
}
}
class AA{
}
class BB extends AA{
}
多态动态绑定机制
package 多态.动态绑定机制;
public class text{
public static void main(String[] args) {
// java 的动态的绑定机制
// 当调用对象的绑定机制的时候,该方法会和该对象的内存地址/运行地址绑定
// 当调用对象的属性的时候 ,没有动态绑定机制 ,哪里调用,哪里使用
// 编译类型 AA 运行类型 BB
// 向上转型
AA a=new BB();
System.out.println(a.sum()); //30
System.out.println(a.sum1()); //
}
}
class AA {
public int i=10;
public int sum(){
return geti() +10;
}
public int sum1(){
return i+10;
}
public int geti(){
return i;
}
}
class BB extends AA {
// public int i = 20;
// public int sum(){
// return i+20;
// }
public int geti(){
return i;
}
public int sum1(){
return i+10;
}
}
类变量
package 类变量_01;
public class text_class {
// detail:
// 1. 类变量,是该类所有对象共享的变量
// 2. 定义语法: 访问修饰符 static 数据类型 变量名;
public static void main(String[] args) {
A a = new A();
System.out.println(a.name);
System.out.println(A.name);
System.out.println(A.aa);
}
}
class A{
// 类变量
public static String name="周浩好帅";
// 可以反过来写
static public int aa=12;
public int bb=12;
public static void zh(){
// 3. 静态方法中不能使用 和对象有关的关键字 比如 this 和 super
// System.out.println(super);
// 4. 静态方法中只能访问 静态变量 和 静态方法(在静态里面声明的可以使用)
// System.out.println(bb); 报错
System.out.println(aa);
}
}
代码块
代码块执行的顺序
package 代码块_02;
public class 代码块执行的顺序 {
public static void main(String[] args) {
// zz zz = new zz();
// 一个类中 代码执行的顺序:
// 静态属性 --> 普通属性 --> 构造器 注:静态代码块和静态属性 优先级一样 按顺序调用
hh hh = new hh();
// 子类中 代码执行的顺序:
// 1、 父类的静态代码和静态属性先执行 (优先级一样 按定义顺序执行)
// 2、 子类的静态代码和静态属性先执行 (优先级一样 按定义顺序执行)
// 3、 父类的普通代码和普通属性先执行
// 4、 父类的构造器
// 5、 子类的普通代码和普通属性先执行
// 6、 子类的构造器
// 静态的代码块只能调用静态成员
// 普通的代码快可以调用任何成员
}
}
class zz {
private static int aa = getaaa();
static {
System.out.println("静态代码块被执行..."); // 2
}
private int bb = getbbb();
{
System.out.println("普通代码快被执行..."); //6
}
public zz() {
System.out.println("构造器被执行"); } //7
public static int getaaa() {
System.out.println("getaa 方法被执行..."); // 1
return 20;
}
public int getbbb() {
System.out.println("getbb被执行"); //5
return 20;
}
}
class hh extends zz {
private static int aa = getaa();
static {
System.out.println("hh静态代码块被执行..."); //4
}
private int bb = getbb();
{
System.out.println("hh普通代码快被执行..."); //9
}
public hh() {
System.out.println("hh构造器被执行"); //10
}
public static int getaa() {
System.out.println("gethhhaa方法被执行..."); //3
return 20;
}
public int getbb() {
System.out.println("gethhhbb 被执行"); //8
return 20;
}
}
代码块的细节
package 代码块_02;
public class 代码块的细节 {
// 1、静态代码块 随着类的加载而执行,并且执行一次,如果是普通代码块的话,每创建一个对象就执行一次
// 2、类加载的三种情况:
// ①:创建对象的实例时
// ②:创建子类对象时,父类的也会被加载
// ③:调用静态成员时
public static void main(String[] args) {
// 创建对象时
A a = new A();
// 调用静态成员时
System.out.println(A.age);
}
}
class B{
static {
System.out.println("B 类的静态代码块被执行...");
}
}
class A extends B{
public static int age=10;
static {
System.out.println("A 类的静态代码块被执行...");
}
}
单列模式
懒汉式
package 单列模式_03;
public class 懒汉式 {
public static void main(String[] args) {
AA geta = AA.geta();
System.out.println(geta.name);
}
}
class AA{
public String name;
private static AA a;
public AA(String name) {
this.name = name;
}
public static AA geta(){
System.out.println("构造器被调用...");
if (a==null){
a=new AA("周浩");
}
return a;
}
}
饿汉式
package 单列模式_03;
public class 饿汉式 {
public static void main(String[] args) {
A aa= A.getaaa();
System.out.println(aa.name);
}
}
class A{
// 构造器私有化
// 在类的内部直接创建
// 提供一个公共的static方法 返回new 对象
public String name;
private static A a=new A("周浩");
private A(String name) {
this.name = name;
}
public static A getaaa(){
return a;
}
}
final
package final_04;
public class final_detail {
public static void main(String[] args) {
// 1. final 修饰的叫常量 初始化必须给值 一、定义的时候给值 二、构造器中给值 三、代码块给值
// 2. final 是static(静态的话)就不能在构造器中给值了
// 3. final 类是不可以继承的,但是可以实例化对象
// 4. final 不能修饰构造器
// 5. final 和 static 往往搭配使用 效率高 不会导致类加载
}
}
class AA {
// 定义的时候给值
private final String name="周浩";
private final String name1;
private final String name2;
// 构造器中给值
public AA(){
name1="周浩";
}
// 方法体中给值
{
name2="周浩";
}
}
抽象类
package 抽象类_05;
/**
abstract detail:
一、 抽象类不能被实列化
二、 抽象类可以没有抽象方法,一旦有抽象方法 一定要定义为抽象类
三、 只能修饰类和方法 不能修饰其他的属性;
四、 抽象方法不能有方法体;
五、 如果一个类继承了抽象类,则它必须实现抽象类的所有方法,除非它本身也声明为抽象类
六、 抽象方法不能使用 private final static 来修饰 因为这些关键字都是和重写相违背的;
*/
class animal_text{
public static void main(String[] args) {
// 一、 抽象类不能被实列化
// new animal(); error
}
}
public abstract class animal {
private String name;
// 三、 只能修饰类和方法 不能修饰其他的属性;
// private sbstrect int age; error
public animal(){
}
// 父类方法不确定性的问题时 可以定义为抽象类 abstract
// 抽象类就是没有实现的方法 ,就是没有方法体;
public abstract void eat();
}
// 五、 如果一个类继承了抽象类,则它必须实现抽象类的所有方法,除非它本身也声明为抽象类
abstract class A{
public abstract void sleep();
}
class B extends A{
public void sleep(){
System.out.println("周浩好帅...");
};
}
接口
接口入门
package 接口_06;
public class interface1 {
public static void main(String[] args) {
phone p=new phone();
camera cam=new camera();
computer com=new computer();
com.work(p);
com.work(cam);
}
}
//创建了一个接口 方法不能有方法体
interface usbinterface{
public void start();
public void stop();
}
//创建一个手机 相当于A 程序员写的
class phone implements usbinterface{
// 实现了接口中所有的方法
public void start(){
System.out.println("手机开始工作....");
}
public void stop(){
System.out.println("手机停止工作....");
}
}
//创建一个相机 相当于B 程序员写的
class camera implements usbinterface{
public void start(){
System.out.println("相机开始工作....");
}
public void stop(){
System.out.println("相机停止工作....");
}
}
//创建了一个电脑 相当于项目经理写的 可以实现统一调度
class computer{
public void work(usbinterface usb){
// 通过接口来调用方法
usb.start();
usb.stop();
}
}
接口细节
package 接口_06;
public class 接口细节 {
public static void main(String[] args) {
// 三、接口不能被实列化
// detail aa=new detail(); error
}
}
//创建一个接口
interface detail {
// 可以有方法 也可以有属性
public int num=10;
// 七、接口中的属性都是 public static final 来修饰的 且必须初始化;
public static final int num2=10;
// 在接口内抽象方法可以省略
public abstract void hi();
// 一、在jdk8 以后可以有默认的方法,需要使用default关键字来修饰
default public void ok(){
System.out.println("ok.....");
}
// 二、在jdk8 可以有静态方法
public static void ok1(){
System.out.println(num);
}
// 四、接口中所有的方法都是public
void aa();
}
class dome implements detail{
// 五、接口中所有普通的方法都要实现 可以使用 alt+enter 快速实现
public void aa(){
}
public void hi(){
}
}
// 六、一个类可以实现多个接口
interface a{
void ok1();
}
interface b{
void ok2();
}
class dome1 implements a,b{
// 同时实现连个接口 的两个方法
@Override
public void ok1() {
}
@Override
public void ok2() {
}
}
// 八、接口不能继承其他类 ,但可以继承别的接口
interface c{
void ok3();
}
interface d extends a,b,
c{
}
多态参数
package 接口_06;
public class 接口细节 {
public static void main(String[] args) {
// 三、接口不能被实列化
// detail aa=new detail(); error
}
}
//创建一个接口
interface detail {
// 可以有方法 也可以有属性
public int num=10;
// 七、接口中的属性都是 public static final 来修饰的 且必须初始化;
public static final int num2=10;
// 在接口内抽象方法可以省略
public abstract void hi();
// 一、在jdk8 以后可以有默认的方法,需要使用default关键字来修饰
default public void ok(){
System.out.println("ok.....");
}
// 二、在jdk8 可以有静态方法
public static void ok1(){
System.out.println(num);
}
// 四、接口中所有的方法都是public
void aa();
}
class dome implements detail{
// 五、接口中所有普通的方法都要实现 可以使用 alt+enter 快速实现
public void aa(){
}
public void hi(){
}
}
// 六、一个类可以实现多个接口
interface a{
void ok1();
}
interface b{
void ok2();
}
class dome1 implements a,b{
// 同时实现连个接口 的两个方法
@Override
public void ok1() {
}
@Override
public void ok2() {
}
}
// 八、接口不能继承其他类 ,但可以继承别的接口
interface c{
void ok3();
}
interface d extends a,b,
c{
}
多态数组
package 接口_06.接口的多态特性;
public class 多态数组 {
public static void main(String[] args) {
// 多态数组
usb[] us=new usb[2];
us[0]=new phone();
us[1]=new camera();
for (int i = 0; i < us.length; i++) {
us[i].work();
// 判断运行类型 instanceof
if (us[i] instanceof phone){
// 向下转型
((phone) us[i]).call();
}
}
}
}
interface usb{
void work();
}
class phone implements usb{
public void call(){
System.out.println("手机可以打电话....");
}
@Override
public void work() {
System.out.println("手机开始工作...");
}
}
class camera implements usb{
@Override
public void work() {
System.out.println("相机开始工作...");
}
}
多态传递现象
package 接口_06.接口的多态特性;
public class 多态转递现象 {
public static void main(String[] args) {
AA a=new DOME();
BB b=new DOME();
}
}
//多态的传递现象
interface AA{
void work();
}
// BB 继承了 AA 这个接口
interface BB extends AA{
}
// DOME 实现了 BB 这个接口
class DOME implements BB{
@Override
public void work() {
}
}
枚举
一、快速入门
package 枚举_01;
/**
* @author ZhouHao
* @version 1.0
*/
public class enum_01 {
public static void main(String[] args) {
System.out.println(enum_.DT);
}
}
//创建一个枚举
enum enum_{
// 一、枚举必须放在第一行
// 二、多个枚举使用 , 号隔开
// 三、ZH 等价于 ZH() 调用了无参构造器
ZH,CT("春天","温暖"),
XT("夏天","炎热"),
QT("秋天","凉爽"),
DT("冬天","寒冷");
private String name;
private String zz;
private enum_(){
}
private enum_(String name, String zz) {
this.name = name;
this.zz = zz;
}
@Override
public String toString() {
return "enum_{" +
"name='" + name + '\'' +
", zz='" + zz + '\'' +
'}';
}
public String getName() {
return name;
}
public String getZz() {
return zz;
}
}
二、枚举的常用方法
package 枚举_01;
/**
* @author ZhouHao
* @version 1.0
*/
public class enum常用的方法_02 {
public static void main(String[] args) {
// 一、 toString() 不重写的话输出 返回当前对象名(常量名)
System.out.println(AAEnum.DT.toString());
// 二、 name() 返回当前对象名(常量名)
System.out.println(AAEnum.DT.name());
// 三、ordinal() 返回当前位置号 默认从0开始
System.out.println(AAEnum.DT.ordinal());
// 四、values() 返回类名加地址
System.out.println(AAEnum.values());
for (AAEnum aa:AAEnum.values()){
// 返回所有的对象名(常量名)
System.out.println(aa);
}
// 五、valueOf 把已有的转换成一个对象
AAEnum aa=AAEnum.valueOf("DT");
System.out.println("----都是一样的返回 true-----");
System.out.println(aa==AAEnum.DT);
// 六、compareTQ 连个枚举常量相减 AAEnum.DT - AAEnum.CT
System.out.println(AAEnum.DT.compareTo(AAEnum.CT));
}
}
//创建一个枚举
enum AAEnum{
// 一、枚举必须放在第一行
// 二、多个枚举使用 , 号隔开
// 三、ZH 等价于 ZH() 调用了无参构造器
ZH,
CT("春天","温暖"),
XT("夏天","炎热"),
QT("秋天","凉爽"),
DT("冬天","寒冷");
private String name;
private String zz;
private AAEnum(){
}
private AAEnum(String name, String zz) {
this.name = name;
this.zz = zz;
}
/*
@Override
public String toString() {
return "enum_{" +
"name='" + name + '\'' +
", zz='" + zz + '\'' +
'}';
}*/
public String getName() {
return name;
}
public String getZz() {
return zz;
}
}
注解
一、预制警告
package 注解_02;
import java.util.Arrays;
import java.util.List;
/**
* @author ZhouHao
* @version 1.0
*/
//所有的警告都没了
@SuppressWarnings("all")
public class 预制警告 {
public static void main(String[] args) {
List a= Arrays.asList(args);
List b= Arrays.asList(args);
List c= Arrays.asList(args);
}
}
补充: 常用类的方法介绍
包装类的拆箱和装箱子
package 包装类_01;
/**
* @author ZhouHao
* @version 1.0
*/
public class 包装类 {
public static void main(String[] args) {
// jdk5 以前需要手动的拆箱和手动装箱
// 装箱: int ---> Integer
int n1=10;
Integer integer01=new Integer(n1);
Integer integer02=Integer.valueOf(n1);
// 拆箱 Integer ---> int
int i= integer01.intValue();
// 自动装箱
int n2=200;
// 底层依然用的是 Integer.valueOf(n2);
Integer integer03=n2;
// 自动拆箱
// 底层依然是 integer03.intValue();
int n3=integer03;
Double a=100d;
System.out.println(a);
Object obj=true? 1:2;
System.out.println(obj);
}
}
包装类常用的方法
package 包装类_01;
/**
* @author ZhouHao
* @version 1.0
*/
public class 包装类的方法 {
public static void main(String[] args) {
// String and Integer 转换的方法
// Integer ---> String
Integer i=10;
String str1=i+"";
String str2=i.toString();
String str3=String.valueOf(i);
// String ---> Integer
String str="123456";
Integer n1=Integer.parseInt(str);
Integer n2=new Integer(str);
// 返回最小值 -2147483648
System.out.println(Integer.MIN_VALUE);
// 返回最大值 2147483647
System.out.println(Integer.MAX_VALUE);
// 判断是不是数字
System.out.println(Character.isDigit('a'));
// 判断是不是字母
System.out.println(Character.isLetter('a'));
// 判断是不是大写
System.out.println(Character.isUpperCase('a'));
// 判断是不是小写
System.out.println(Character.isLowerCase('a'));
// 判断是不是空格
System.out.println(Character.isWhitespace('a'));
// 转成大写
System.out.println(Character.toUpperCase('a'));
// 转成小写
System.out.println(Character.toLowerCase('a'));
}
}
String的比较
package String_02;
/**
* @author ZhouHao
* @version 1.0
*/
public class String_01 {
public static void main(String[] args) {
String str="abc";
String str1="abc";
String str2=new String("abc");
// System.identityHashCode 打印String 的地址值
// 可以看出来 str和str1的地址是一样的
System.out.println(System.identityHashCode(str));
System.out.println(System.identityHashCode(str1)); //356573597
System.out.println(System.identityHashCode(str2));
System.out.println(str.equals(str1));
System.out.println(str==str1);
System.out.println(str.equals(str2));
System.out.println(str==str2);
// intern 返回字符串的规范表示 返回在常量池中的地址
System.out.println(str1==str2.intern());
System.out.println(str2==str2.intern());
// 常量相加
//
// 常量看的是池 变量相加 实在堆中
}
}
String常用的方法
package String_02;
import java.util.Locale;
/**
* @author ZhouHao
* @version 1.0
*/
public class String的常用方法 {
public static void main(String[] args) {
String str1="hello";
String str2=new String("HELLO");
// 一、equals 区分大小写,判断内容是否相等
System.out.println(str1.equals(str2));
// 二、equalsIgnoreCase 不区分大小写 判断内容是否相等
System.out.println(str1.equalsIgnoreCase("HELLO"));
// 三、length 获取字符的长度
System.out.println(str1);
// 四、indexOf 获取子串在字符串第一次出现的位置, 没找到返回-1
System.out.println(str1.indexOf("ll"));
// 五、lastIndexOf 获取子串在字符串最后一次出现的位置 没找到返回-1
System.out.println("ZhouHao".lastIndexOf("ao"));
// 六、 substring 截取指定范围的子串 注:0-2 不包括2
System.out.println(str1.substring(0,2));
// 七、 trim 去掉前后空格
System.out.println(" aaa ".trim());
// 八、 charAt 获取某索引处指定的字符
System.out.println(str1.charAt(3));
// 九、 toUpperCase 全部转成大写
System.out.println(str1.toUpperCase());
// 十、 toLowerCase 全部转成小写
System.out.println(str2.toLowerCase());
// 十一、 concat 连接
System.out.println("aa".concat("bb"));
// 十二、 replace 替换字符中的字符
System.out.println("ZhouHao".replace('Z','z'));
// 十三、 split 按某个字符来分割 返回一个字符串数组
String temp="123a456a789a";
String[] temp1=temp.split("a");
for (int i = 0; i < temp1.length; i++) {
System.out.println(temp1[i]); //123 456 789
}
// 十四、compareTo 比较字符相减
System.out.println(str1.compareTo(str2));
// 十五、 toCharArray 转换成字符串数组
// System.out.println(str1.toCharArray());
char[] c=str1.toCharArray();
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]);
}
// 十六、 format
String ss=String.format("姓名: %s",str1);
System.out.println(ss);
// 十七、 获取一个最大的数字
System.out.println(Math.max(4,5));
}
}
StringBuffer 和String的转换
package StringBuffer_03;
/**
* @author ZhouHao
* @version 1.0
*/
public class 快速入门 {
public static void main(String[] args) {
// String 和 StringBuffer 的相互转换
// String --->StringBuffer
String str="hello";
// 方式一、 构造器
StringBuffer stringBuffer=new StringBuffer(str);
System.out.println(stringBuffer);
// 方式二、 append追加
StringBuffer stringBuffer1=new StringBuffer();
stringBuffer1=stringBuffer1.append(str);
System.out.println(stringBuffer1);
// StringBuffer ---> String
StringBuffer stringBuffer2=new StringBuffer("ZhouHao");
// 方式一、 toString
String str1=stringBuffer2.toString();
System.out.println(str1);
// 方式二、 构造器来解决
String str2=new String(stringBuffer2);
System.out.println(str2);
}
}
StringBuffer常用的方法
package StringBuffer_03;
/**
* @author ZhouHao
* @version 1.0
*/
public class StringBuffer_常用的方法 {
public static void main(String[] args) {
StringBuffer str=new StringBuffer("ZhouHao");
// 增
str=str.append(",");
str=str.append("Hao").append("Shuai");
System.out.println("增加后:"+str);
// 删
str= str.delete(7,str.length());
System.out.println("删除后:"+str);
// 改 包含4 不包含7
str=str.replace(4,7,"HaoHao");
System.out.println("修改后:"+str);
// 查
int num=str.indexOf("Hao");
System.out.println("找到的索引是:"+num);
// 插
str=str.insert(4,"aa");
System.out.println("插入后:"+str);
// 获取长度
System.out.println("长度是:"+str.length());
}
}
StringBuilder和StringBuffer 和String的比较
package StringBuilder_04;
/**
* @author ZhouHao
* @version 1.0
*/
public class 快速入门 {
public static void main(String[] args) {
StringBuilder ab=new StringBuilder("zhouhao");
/**
* 一、StringBuilder 和 StringBuffer 的方法是一样的
* 二、如果字符串存在大量的修改操作 ,并在单线程的情况下 使用 StringBuilder
* 三、如果字符串存在大量的修改操作 ,并在多线程的情况下 使用 StringBuffer
* 四、如果我们字符串很少修改的,被多个对象引用的话 使用String 比如配置信息
* */
}
}
Math常用的方法
package Math_05;
/**
* @author ZhouHao
* @version 1.0
*/
public class Math_常用的方法 {
public static void main(String[] args) {
// 一、abs 绝对值
System.out.println(Math.abs(-1));
// 二、pow 求幂 2*2*2
System.out.println(Math.pow(2,3));
// 三、ceil 向上取整
System.out.println(Math.ceil(1.1));
// 四、floor 向下取整
System.out.println(Math.floor(1.9));
// 五、round 四舍五入
System.out.println(Math.round(2.4));
System.out.println(Math.round(2.5));
// 六、sqrt 开方
System.out.println(Math.sqrt(9));
// 七、random 求随机数 返回 0<= rand
// om <1 的数
System.out.println(Math.random());
System.out.println(Math.random()*10);
// 八、max 求两个数的最小值
System.out.println(Math.max(7,9));
// 九、min 求两个数的最大值
System.out.println(Math.min(7,9));
}
}
Arrays常用的方法
package Arrays_06;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
/**
* @author ZhouHao
* @version 1.0
*/
public class Arrays_常用的方法 {
public static void main(String[] args) {
int[] arr1={10,5,8,30};
// 一、 Arrays.toString() 遍历数组
System.out.println(Arrays.toString(arr1));
//------------------------------------------------------------------
// 二、 sort 排序
Arrays.sort(arr1); //默认排序的方法从小到大
System.out.println(Arrays.toString(arr1));
//------------------------------------------------------------------
int[] arr={4,4,5,6,0,1,9};
bbb(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int i1=(int) o1;
int i2=(int) o2;
// i1-i2 小到大排序 i2-i1 大到小排序
return i1-i2;
}
});
System.out.println(Arrays.toString(arr));
//------------------------------------------------------------------
// 三、 Arrays.binarySearch 二叉查找
// 一、数据必须是有序的 返回索引index 没找到就返回应该在的位置加1 -(low+1)
int[] aa={1,4,8,6,30,42};
System.out.println(Arrays.binarySearch(aa,4));
//------------------------------------------------------------------
// 四、 copyOf 数组元素负值
// 拷贝的length可以改边 根据自己需要的值来拷贝
int[] bb=Arrays.copyOf(aa,aa.length-2);
System.out.println(Arrays.toString(bb));
//------------------------------------------------------------------
// 五、 fill 数组的填充 7来替换 cc数组中的所有元素
int[] cc={1,4,5,7};
Arrays.fill(cc,7);
System.out.println(Arrays.toString(cc));
//------------------------------------------------------------------
// 六、 equals 来比较两个数组是否完全一样
// 比较两个元素的值是否一样
int[] dd={1,4,5,7};
int[] ee={1,4,5,7};
System.out.println(Arrays.equals(ee,dd));
//------------------------------------------------------------------
//------------------------------------------------------------------
}
public static void bbb(int[] arr ,Comparator c){
int temp=0;
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if (c.compare(arr[j],arr[j+1])>0){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
}
System类的使用
package System_07;
import java.util.Arrays;
/**
* @author ZhouHao
* @version 1.0
*/
public class System_way {
public static void main(String[] args) {
// 一、exit 退出程序
System.exit(0);
// 二、arraycopy 复制数组的元素 ,比较适合底层的调用,一般使用 Arrays.copyOf完成复制
int[] src ={1,2,3};
int[] arr=new int[3];
/**
* 第一个参数 : 表示原数组
* 第二个参数 : 从那个索引开始拷贝
* 第三个参数 : 目标数组 拷贝到那个数组
* 第四个参数 : 拷贝到目标数组的那个位置开始
* 第五个参数 : 从原数组拷贝多少个数据到新的数组
* */
System.arraycopy(src,0,arr,0,3);
System.out.println(Arrays.toString(arr));
// 三、currentTimeMillis 返回1970 1-1 到现在的毫秒数
System.out.println(System.currentTimeMillis());
// 四、gc() 运行的垃圾回收机制
System.gc();
}
}
Big_Num
一、BigInteger
package Bignum_08;
import java.math.BigInteger;
/**
* @author ZhouHao
* @version 1.0
*/
public class BigInteger_ {
public static void main(String[] args) {
// 对比long大的数字进行处理 BigInteger
BigInteger bigInteger = new BigInteger("45129999999999999999999999999999999");
// 在对 BigInteger 进行加减乘除的话 不能使用 + - * / 而是使用对应的方法
// add --> + 加法
BigInteger bigInteger1 = new BigInteger("100");
System.out.println(bigInteger.add(bigInteger1));
// subtract ---> - 减法
System.out.println(bigInteger.subtract(bigInteger1));
// multiply 乘法 divide 除法。
}
}
二、BigDecimal
package Bignum_08;
import java.math.BigDecimal;
/**
* @author ZhouHao
* @version 1.0
*/
public class BigDecimal_ {
public static void main(String[] args) {
// 当 double 的精度不够用的话 可以使用 BigDecimal
BigDecimal bigDecimal=new BigDecimal("1.4564555454876565656567888888888999");
System.out.println(bigDecimal);
BigDecimal bigDecimal1=new BigDecimal("1.1");
// 加 add
System.out.println(bigDecimal.add(bigDecimal1));
// 减 subtract
System.out.println(bigDecimal.subtract(bigDecimal1));
// 乘 multiply
System.out.println(bigDecimal.multiply(bigDecimal1));
// 除 divide 如果是一个除不尽的数的话就会抛出一个异常
// 解决异常 在后面指定一个精度 会保留一个和分子一样长度的精度
System.out.println(bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING));
System.out.println(bigDecimal.divide(bigDecimal1)); // 除不尽异常
}
}
DATE 时间
一、第一代 Date
package Date_09.date_;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author ZhouHao
* @version 1.0
*/
public class Date_way {
public static void main(String[] args) {
// 获取当前系统时间
Date d1=new Date();
System.out.println(d1);
// 格式转换
SimpleDateFormat adf=new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
String str=adf.format(d1);
System.out.println(str);
// 字符串转成date 这String 要和上面的格式一样
String s="2002年05月06日 12:00:00 星期一";
Date date= null;
try {
date = adf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
// 是按照国外的格式输出 adf.format转换成
System.out.println(adf.format(date));
}
}
二、第二代Calender
package Date_09.Calender_;
import javax.crypto.spec.PSource;
import java.util.Calendar;
/**
* @author ZhouHao
* @version 1.0
*/
public class Calender_way {
public static void main(String[] args) {
/**
* 一、Calender 是一个抽象类 并且构造器是私有的 我们只能通过 get来获取
* 二、
* */
// 通过 getInstance() 方法来获取时间
Calendar c=Calendar.getInstance();
// 都存放在字段里 获取日历对象的某个日历字段
// 获取年
System.out.println(c.get(Calendar.YEAR));
// 月 默认是从 0 开始编号
System.out.println(c.get(Calendar.MONDAY)+1);
// 日
System.out.println(c.get(Calendar.DAY_OF_MONTH));
// 时
System.out.println(c.get(Calendar.HOUR));
// 分
System.out.println(c.get(Calendar.MINUTE));
// 秒
System.out.println(c.get(Calendar.SECOND));
// 没有专门的格式 需要自定义
// 年、月、日
System.out.println(c.get(Calendar.YEAR)+"年" +(c.get(Calendar.MONDAY)+1)+"月" + c.get(Calendar.DAY_OF_MONTH)+"日");
// 时、分、秒
System.out.println(c.get(Calendar.HOUR_OF_DAY)+"点"+c.get(Calendar.MINUTE)+"分"+c.get(Calendar.SECOND)+"秒");
}
}
三、第三代LocalDate
一、Instant
package Date_09.LocaIDate_;
import java.time.Instant;
import java.util.Date;
/**
* @author ZhouHao
* @version 1.0
*/
public class Instant_ {
public static void main(String[] args) {
// 时间戳
// 一、 通过静态方法获取当前的时间戳
Instant now =Instant.now();
System.out.println(now);
// 二、通过 from 可以把Instant 转成 Date
Date date =Date.from(now);
// System.out.println(date);
// 三、通过 date的toInstant() 可以把date 转成 Instant 对象
Instant instant=date.toInstant();
// System.out.println(instant);
}
}
二、LocalDate常用的方法
package Date_09.LocaIDate_;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
/**
* @author ZhouHao
* @version 1.0
*/
public class LocalDate_way {
public static void main(String[] args) {
LocalDateTime l=LocalDateTime.now(); //LocalDate.now() LocalTime.now()
System.out.println(l);
// 年
System.out.println(l.getYear()+"年");
// 月
System.out.println(l.getMonth()+"月"); //英文的
System.out.println(l.getMonthValue());
// 日
System.out.println(l.getDayOfMonth()+"日");
// 时
System.out.println(l.getHour()+"时");
// 分
System.out.println(l.getMinute()+"分");
// 秒
System.out.println(l.getSecond()+"秒");
// 二、 使用 DateTimeFormatter 格式化日期
DateTimeFormatter dtf=DateTimeFormatter.ofPattern("G yyyy年MM月dd日 HH:mm:ss");
System.out.println(dtf.format(l));
String str= dtf.format(l);
System.out.println(str);
// 三、提供了大量的 plus 加 minus 减 可以对当前的时间加减
LocalDateTime p=l.plusDays(888);
System.out.println(dtf.format(p));
}
}
第六章 开发java SE高级应用程序
异常
一、五大运行时异常
package 异常_03.五大运行异常;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
/**
* @author ZhouHao
* @version 1.0
*/
public class text {
public static void main(String[] args) {
text.NullPointerException();
ArithmeticException();
ArrayIndexOutOfBoundsException();
NumberFormatException();
}
// 空指针异常
public static void NullPointerException(){
try {
// 空指针异常..
String name=null;
System.out.println(name.length());
} catch (Exception e) {
// e.printStackTrace();
System.out.println("空指针异常..");
}
}
// 算术异常
public static void ArithmeticException(){
try {
int res=10/0;
} catch (Exception e) {
System.out.println("算术异常");
// e.printStackTrace();
}
}
// 数组越界异常
public static void ArrayIndexOutOfBoundsException(){
try {
int[] arr={1,2,3};
System.out.println(arr[3]);
} catch (Exception e) {
System.out.println("数组越界");
}
}
//
public static void NumberFormatException(){
try {
String str="ZhouHao";
int num=Integer.parseInt(str);
System.out.println(num);
} catch (NumberFormatException e) {
System.out.println("类型转换异常");
}
}
}
class 数字格式异常{
}
二、try_catch_ finally
package 异常_03;
/**
* @author ZhouHao
* @version 1.0
*/
/**
* 小结 :
* 如果有异常 try异常后面的语句就不执行了 会执行catch 有finally 会执行
* 如果没有异常 try 会全部执行 不会执行catch 有finally 会执行
* */
public class try_catch_finally {
public static void main(String[] args) {
// try_catch_finally 使用方法
try {
int res = 10 / 0;
System.out.println(res);
// 子类在前面父类在后面
} catch (ArithmeticException a) {
// 捕获到异常捕获完了会直接到 finally;
System.out.println("传入的必须是他的子类 Exception ");
} catch (Exception e) {
System.out.println(e.getMessage());
}finally{
System.out.println("finally 不管怎么样都会执行...");
}
// try_finally
try {
int res=10/0;
// 当上面程序异常的话 会执行完了finally后在跳出程序
} finally {
// 不管try 有没有异常 这finally 都要执行
System.out.println("继续执行 finally");
}
// try 有异常的话 下面代码就不会执行
System.out.println("程序继续执行?");
}
}
三、throw 自定义异常
package 异常_03.throws_;
/**
* @author ZhouHao
* @version 1.0
*/
public class 自定义异常 {
public static void main(String[] args) {
int age =-12;
// age 的年龄必须在 0-120之间 否则就抛出一个异常
if (age>=0&&age<=120){
System.out.println("正常");
}else{
// 不正常的话抛出一个异常
throw new zzz("不正常...");
}
}
}
//自定义一个类 /异常 继承父类
class zzz extends RuntimeException{
// 创建一个构造器
public zzz(String message) {
super(message);
}
}
四、throws 和 throw 的比较
package 异常_03.throws_;
/**
* @author ZhouHao
* @version 1.0
*/
public class throw_and_throws {
/**
* throws 的时使用细节:
* 一、对于编译异常 程序必须用 try-catch 或 throws处理
二、对于运行异常 ,程序没有处理 默认就是throws方法处理
三、子类重写父类的话 ,子类不能缩小父类抛出的异常
四、throws 和 try-catch 两者二选一*/
/**
* 意义 位置 后面跟的东西
* throws 异常处理的一种方式· 方法声明处 异常类型
* throw 手动生成异常对象的关键字 方法体中 异常对象
* */
}