Java基础
标识符
命名规则:
1.标识符只能由数字,字母,下划线,美元符号组成,不能含有其他符号。
2.标识符不能以数字开头
3.关键字不能做标识符。如public static等
4.标识符是严格区分大小写的。
5.对于类名来说,如果一个Java源文件中同时出现了A类和a类,那么谁在前就生成谁,最好不要让类名相同。
命名规范:
1.见明知意
2.类名,接口名首字母大写,后面每个单词首字母大写。
3.变量名,方法名首字母小写,后面每个单词首字母大写。
4.所有常量名,全部大写,并且单词和单词之间采用下划线衔接。
数据类型
作用:
不同的数据类型,在内存中分配的空间大小不同,Java虚拟机到底给这个数据分配多大的内存空间,主要还是看这个变量的数据类型,根据不同的类型分配不同的空间。
对于int这种整数类型,jvm会自动给interesting分配4个字节大小的空间。
变量的作用域:
1.作用域就是变量的有效范围,出了大括号就无效了。
数据类型的分类:
一:基本数据类型:
第一类:整数型
第二类:浮点型
第三类:布尔型
第四类:字符型
8小种:byte,short,int,long分别为1 2 4 8个字节
float,double分别为4 8个字节
boolean为1个字节
char为2个字节,java中规定字符型字面量必须使用单引号括起来,属于文字。
二:引用数据类型:
string
计算机存储单位:
计算机只能识别二进制。1字节=8bit(8比特)
1bit就是一个1或0
1kb=1024byte
1mb=1024kb
1gb=1024mb
1tb=1024gb
byte b = 2;在计算机中是这样表示的:00000010
short s = 2;在计算机中是这样表示的:00000000 00000010
int i = 2;在计算机中是这样表示的:00000000 00000000 00000000 00000010
byte类型的取值范围:[-128-127]共,可以表示256个不同的数字。
byte类型的最大值是怎么计算出来的?
01111111=127 注意:在计算机中,一个二进制位最左边的是符号位,当为0时表示整数,当为1时表示负数。
字符编码:
1.字符编码是人为的定义的一套转换表,在字符编码中规定了一系列的文字对应的二进制。
2.字符编码其实本质上就是一本字典,该字段中描述了文字与二进制之间的对照关系。
3.字符编码是人为规定的。
字符编码涉及到编码和解码两个过程,编码和解码的时候必须采用一套字符编码方式,不然就会出现乱码。
转义字符:
Java语言中“\”负责转义,\t表示制表符tab,\n表示换行符,\u表示一个字符的unicode编码
自动类型转换:
小容量可以自动转换成大容量,称为自动类型转换。
大容量不能之间赋值给小容量,需要使用强制类型转换进行强转。
break语句:
终止离他最近的循环语句,不是if等等语句。
如果想终止哪个循环可与起名字。
a:for(int k=0;k<2;k++){
b:for(int i=1;i<=10;i++){
if(i==5){
break a;
}
System.out.println(i);
}
}
continue语句:
作用:终止本次循环,直接进入下一次循环执行。
for(){
if(){
continue;//当continue继续执行时,她下面的代码不执行,直接进入下一次循环
}
//一旦进行continue以下代码不执行
code1;
code2;
}
break和return的区别:
public class Method{
public static void main(String[] args){
for(int i=0;i<10;i++){
if(i==5){
//break; 终止for循环
//return; 终止当前的方法,和break不是一个级别。
}
System.out.println("i="+i);
}
System.out.println("Hello World!");
}
}
方法:
方法不调用是不会在栈中分配空间的。
方法只有在调用的时候才会在栈中分配空间,并且调用时就是压栈。
方法执行结束之后,该方法所需要的空间就会释放,此时发生弹栈操作。
方法调用叫做:压栈。分配空间
方法结束叫做:弹栈。释放空间
栈中存储什么?
方法运行过程中需要的内存,以及栈中存储方法的局部变量。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z90qy88E-1672803022884)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221213124644803.png)]
方法重载
方法名相同,但是数据类型和返回值不同。
参数的顺序不同,也算不同。
递归
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hbJrQSkh-1672803022885)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221213154428680.png)]
实例变量
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8Muw54nz-1672803022885)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221213184135307.png)]
JVM
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rMrqgLQ4-1672803022886)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221213201939197.png)]
构造方法
1.当一个类中没有提供任何构造方法,系统默认提供一个无参的构造方法,这个无参数的构造方法叫做缺省构造器。
2.当一个类中手动的提供了构造方法,那么系统将不在提供无参数构造方法。
Student x = new Student(); Student y = new Student(123);
静态方法
没有对象就没有name,所以如果定义静态的方法,输出的是类里面的实例变量,这个变量是谁的name不知道,所以会报错。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FhnrnXT4-1672803022886)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221214172200032.png)]
实例代码块
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lZxUoiLZ-1672803022887)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221214213934389.png)]
this
1.this是一个关键字,全部小写。
2.一个对象一个this。this是一个变量,一个引用,this保存当前对象的内存地址,指向自身。
3.this代表的就是当前对象,this存储在堆内存当中对象的内部。
4.this只能使用在实例方法中,谁调用这个实例方法,this就是谁,所以this代表的是:当前对象。
5.this在大部分情况下是可以省略的。
6.this不能使用在静态方法中。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BXIQ0Cct-1672803022887)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221214214737937.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e65XTI7l-1672803022887)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221214215633087.png)]
下面的this是可以省略的
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hFfBKdmw-1672803022888)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221214215836673.png)]
————————————————————
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g1DtUq9S-1672803022889)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221215083131085.png)]
this通过当前的构造方法去调用另一个本类的构造方法,可以使用this(实际参数列表)
this调用必须是构造器中第一个语句
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tDvWBeAB-1672803022890)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221215094735594.png)]
多态
向上转型:子转成父(自动类型转换)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aqQfuozQ-1672803022890)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221215152044141.png)]
向下转型:父转成子(强制类型转换,需要加强制类型转换符)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S6bqTjjP-1672803022891)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221215160741732.png)]
如果调用的方式是子类中特有的方法,需要做强制类型转换,就是向下转型
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GoWKu4lP-1672803022892)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221216095053424.png)]
instanceof
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MwLyzZEe-1672803022892)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221216105551865.png)]
super
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1M4DrSpt-1672803022892)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221216151324502.png)]
final
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MjS0qxfi-1672803022893)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221217111750342.png)]
抽象类
1.final和abstract不能联合使用,这两个关键字是对立的
2.抽象类的子类可以是抽象类
3.抽象类虽然无法实例化,但是抽象类有构造方法,这个构造方法可以供子类使用。
4.抽象类关联到一个概念:抽象方法。
抽象方法表示没有实现的方法,没有方法体的方法。
public abstract void do some();
5.抽象类中不一定有抽象方法,抽象方法必须出现在抽象类中。
6.如果子类是抽象类的话,那么从父类继承过来的抽象方法可以不用重写,否则必须重写。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JCug8sHs-1672803022893)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221217143420131.png)]
抽象类和接口的区别
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2mOlV9vg-1672803022893)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221217153425384.png)]
内部类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pqq1dsQp-1672803022893)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221218122923458.png)]
引用类型的数组
public class A {
public static void main(String[] args) {
int[] array={1,2,3};
for (int i = 0; i < array.length; i++) {
// int temp = array[i];
// System.out.println(temp);
//将上两行代码合并
System.out.println(array[i]);
}
//创建一个Animal类型的数组
Animal a1 = new Animal();
Animal a2 = new Animal();
Animal[] animals = {a1,a2};
//对Animal数组进行遍历、
for (int i = 0; i < animals.length; i++) {
// Animal a = animals[i];
// a.move();
//将上两行代码合并
animals[i].move();
}
//动态初始化一个长度为2的Animal类型数组
Animal[] ans = new Animal[2];
//创建一个animal对象,放到数组的第一个盒子中。
ans[0] = new Animal();
//Animal数组中只能存放Animal类型,不能存放product类型
// ans[1] = new Product();
//Animal数组中可以存放cat类型的数据,因为cat是一个Animal。cat是子类
ans[1] = new Cat();
}
}
class Animal{
public void move(){
System.out.println("Animal move....");
}
}
class Product{
public void move(){
System.out.println("Animal move....");
}
}
class Cat extends Animal{
public void move(){
System.out.println("zoumaobu");
}
}
数组拷贝
public static void main(String[] args) {
//拷贝源
int[] src = {1,11,22,3,4};
//拷贝目标
int[] dest = new int[20];
//第二个和第四个参数从第几个目标开始,拷贝长度为2
System.arraycopy(src,1,dest,3,2);
//遍历目标数组
for (int i = 0; i < dest.length; i++) {
System.out.println(dest[i]);
}
}
Stringbuffer和StringBuilder
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LKZ1IyU3-1672803022894)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220165414993.png)]
//创建一个初始化容量为16的数组
StringBuffer stringBuffer = new StringBuffer();
//拼接字符串,以后拼接字符出统一调用append();
stringBuffer.append("a");
stringBuffer.append("c");
stringBuffer.append("d");
stringBuffer.append("b");
stringBuffer.append(3.14);
stringBuffer.append(true);
System.out.println(stringBuffer.toString());
public static void main(String[] args) {
//StringBuilder也可以完成字符串的拼接
StringBuilder sb = new StringBuilder();
sb.append(100);
sb.append(true);
sb.append("hello");
sb.append("kitty");
System.out.println(sb);
}
包装类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TW9WW1EJ-1672803022894)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220170006297.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-imMOfNRl-1672803022894)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220170048544.png)]
装箱拆箱
public static void main(String[] args) {
//包装:基本数据类型向引用数据类型的转换
//基本数据类型--转化为--->引用数据类型(装箱)
Integer i = new Integer(123);
//将引用数据类型--转化为--->基本数据类型(拆箱)
float f = i.floatValue();
System.out.println(f);
int retValue = i.intValue();
System.out.println(retValue);
}
自动拆箱装箱
public static void main(String[] args) {
//基本数据类型--(自动转换)--->包装类型:自动装箱
Integer x = 900;
//x是包装类型
//y是基本数据类型
//包装类型--(自动转换)--->基本数据类型:自动拆箱
int y = x;
Integer z = 100;
//+两边要求的是基本类型的数字,z是包装类,不属于基本数据类型,这里会进行自动拆箱,将转换为基本数据类型
System.out.println(z + 1);
Integer a = 1000;//Integer a = new Integer(1000);a是一个引用,保存内存地址指向对象
Integer b = 1000;//Integer b = new Integer(1000);b是一个引用,保存内存地址指向对象
// == 比较的是对象的内存地址,a和b两个引用中保存的对象内存地址不同
// == 这个运算符不会触发自动拆箱机制(只有+-*%等运算的时候才会)
System.out.println(a==b);//false
}
在内存池中指向相同地址
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fiLwYbAU-1672803022895)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220184212710.png)]
parseint
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qipDBOcC-1672803022895)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220211910960.png)]
valueof
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HYn682VU-1672803022896)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220211930405.png)]
日期时间
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SrNANLE3-1672803022896)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220212837202.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nLJNHHV2-1672803022896)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220212940805.png)]
BigDecimal
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1UalJhQh-1672803022896)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220213101286.png)]
枚举
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eROFw6SN-1672803022897)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220214207907.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xWNbcCpw-1672803022897)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220214036985.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g6vK5Mdc-1672803022897)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221220214103483.png)]
集合
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uNccbqRR-1672803022898)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221221112041335.png)]
io流
通过io可以完成硬盘文件的读和写
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oibBBvnr-1672803022898)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221222083606959.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-quhPsA7F-1672803022898)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221222082306403.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gsKyCeCd-1672803022899)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221222111441745.png)]
public static void main(String[] args){
FileInputStream fis = null;
try {
//创建流对象获取地址
fis = new FileInputStream("E:\\Desktop\\project.txt");
FileInputStream fis1 = new FileInputStream("E:/Desktop/project.txt");
//开始读,读取到最后什么也没有返回-1
int readData = fis.read();
System.out.println(readData);
}catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
//在finally语句中确保流一定关闭,先确定不等于空
if (fis != null){
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
//对上一个程序进行改善
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("E:\\Desktop\\project.txt");
while (true){
int readData = fis.read();
if (readData == -1){
break;
}
System.out.println(readData);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if (fis != null){
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
outputstream
public static void main(String[] args) {
FileOutputStream fos = null;
try {
//文件不存在的时候会自动新建,这种会清空
fos = new FileOutputStream("E:\\Desktop\\project.txt");
//在源文件后面追加写入,不会清空
fos = new FileOutputStream("E:\\Desktop\\project.txt",true);
//开始写
byte[] bytes = {97,98,99,100};
//将bytes数组全部写出
fos.write(bytes);
//将bytes数组的一部分写出
fos.write(bytes,0,2);
String s = "我是一个中国人,我骄傲!!!";
byte[] bs = s.getBytes();//将字符串转换为数组
fos.write(bs);
//写完之后要刷新
fos.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if (fos != null){
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
文件拷贝
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qgpmPNzC-1672803022899)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221222210250081.png)]
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//创建一个输入流对象
fis = new FileInputStream("");
//创建一个输出流对象
fos = new FileOutputStream("");
//最核心,一边读,一边写
byte[] bytes = new byte[1024 * 1024];
int readCount = 0;
while ((readCount = fis.read(bytes)) != -1){
fos.write(bytes,0,readCount);
}
fos.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
//分开try不要一起try 一起的时候一个出现异常,可能会影响到另一个流的关闭
if (fos != null){
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (fis != null){
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
filereader
文件字符输入流,只能读取普通文本,读取文本内容时,比较方便,快捷。
public static void main(String[] args) {
FileReader reader = null;
try {
//创建文件字符输入流
reader = new FileReader("");
//准备一个char数组
char[] chars = new char[4];
//往char数组中读
reader.read(chars);
for (char c:
chars){
System.out.println(c);
}
//开始读
// char[] chars = new char[4]; //一次读取4个字符
// int readCount = 0;
// while ((readCount = reader.read(chars)) != -1){
// System.out.print(new String(chars,0,readCount));
// }
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
filewriter
文件字符输出流,写。只能输出普通文本。
public static void main(String[] args) {
FileWriter out = null;
try {
//创建文件输出流对象
out = new FileWriter("");
//开始写
char[] chars = {'我','是','中','国'};
out.write(chars);
out.write(chars,2,3);
out.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if (out != null){
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
使用filereader和filewriter进行拷贝的话,只能拷贝“普通文本”文件。
public static void main(String[] args) {
FileReader in = null;
FileWriter out = null;
try {
//读
in = new FileReader("");
//写
out = new FileWriter("");
//一边读一边写
char[] chars = new char[1024 * 512];
int readCount = 0;
while ((readCount = in.read(chars)) != -1){
out.write(chars,0,readCount);
}
//刷新
out.flush();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (in != null){
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (out != null){
try {
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
bufferedreader
带有缓冲区的字符输入流。使用这个流的时候不需要自定义char数组,或者说不需要自定义byte数组。
public static void main(String[] args) throws Exception {
FileReader reader = new FileReader("");
//当一个流的构造方法中需要一个流的时候,这个被传进来的流叫做:节点流。
//外部负责包装的这个流,叫做包装流,还有一个名字叫做:处理流。
//像当前这个程序来说:Filereader就是一个节点流。bufferedReader就是包装流/处理流。
BufferedReader br = new BufferedReader(reader);
//读一行
// String firstLine = br.readLine();
// System.out.println(firstLine);
String s = null;
while ((s = br.readLine()) != null){
System.out.println(s);
}
//关闭流 对于包装流来说,只需要关闭最外层流就行,里面的结点流会自动关闭。
br.close();
}
dataoutputStream
数据专属的流,这个流可以将数据连同数据的类型一并写入文件。注意:这个文件不是普通文本文档。
public static void main(String[] args) throws Exception {
//创建数据专属的字节输出流
DataOutputStream dos = new DataOutputStream(new FileOutputStream(""));
byte b = 100;
short s = 200;
int i = 300;
dos.write(b);
dos.write(s);
dos.write(i);
//刷新
dos.flush();
//关闭最外层
dos.close();
}
file
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HEbqWoGS-1672803022899)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221223210921494.png)]
public static void main(String[] args) throws Exception{
File f1 = new File("D:\\file");
//判断是否存在
System.out.println(f1.exists());
//如果文件不存在,则以文件的形式创建出来
if (!f1.exists()){
//以文件形式创建
f1.createNewFile();
}
//以目录形式创建
if (!f1.exists()){
f1.mkdir();
}
//创建多重目录
File f2 = new File("D:/a/b/c/d/e");
if (!f2.exists()){
f2.mkdirs();
}
File f3 = new File("");
//获取文件的父路径
String parentPath = f3.getParent();
System.out.println(parentPath);
File parentFile = f3.getParentFile();
System.out.println("获取绝对路径:"+ parentFile.getAbsolutePath());
File f4 = new File("");
System.out.println("获取绝对路径:"+ f4.getAbsolutePath());
}
文件拷贝
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bOM4EkQJ-1672803022900)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221225174016904.png)]
线程
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-995Qfg5o-1672803022900)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221226084821399.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-j9ljb7tV-1672803022900)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221226091736383.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Hv3jR7Xu-1672803022900)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221226092620443.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DQhRnCY4-1672803022901)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221226093844318.png)]
实现线程第一种方法:
直接继承Java.lang.thread,重写run方法。
public class A {
public static void main(String[] args) throws Exception {
//这里是main方法,这里的代码属于主线程,在主栈中运行
//新建一个分支线程对象
MyThread myThread = new MyThread();
//启动线程,启动成功的线程会自动调用run方法,main方法在主栈的栈底部,run和main是平级的
//start方法的作用是,启动线程分支线程后,在jvm中开辟一个新的栈空间,代码执行结束后,瞬间就消失了
myThread.start();
//这里代码还是运行在主线程中
for (int i = 0; i < 1000; i++) {
System.out.println("主线程--->"+i);
}
}
}
class MyThread extends Thread{
@Override
public void run() {
//编写程序,这段程序运行在分支栈中(分支线程).
for (int i = 0; i < 1000; i++) {
System.out.println("分支线程--->"+i);
}
}
}
实现线程的第二种方法,编写一个类实现Java.lang.runnable接口
public class A {
public static void main(String[] args) throws Exception {
//创建一个可运行对象
MyRunnable r = new MyRunnable();
//将可运行对象封装成一个线程对象
Thread t = new Thread();
//启动线程
t.start();
for (int i = 0; i < 100; i++) {
System.out.println("主线程--->"+i);
}
}
}
//这并不是一个线程类,是一个可运行类,它还不是一个线
class MyRunnable implements Runnable{
@Override
public void run() {
}
}
获取线程的名字
Thread.currentThread()获取当前线程
public class A {
public static void main(String[] args) throws Exception {
//创建线程对象
MyThread2 t = new MyThread2();
//设置线程的名字
t.setName("tttt");
//获取线程的名字
String tName = t.getName();
System.out.println(tName);
//启动线程
t.start();
}
}
class MyThread2 extends Thread{
public void run(){
for (int i = 0; i < 1000; i++) {
System.out.println("分支线程"+i);
}
}
}
线程睡眠
public static void main(String[] args) throws Exception {
//让当前线程进入休眠,睡眠5秒
//当前线程是主线程
Thread.sleep(1000 * 5);
//5秒后执行这个代码
System.out.println("hello world!");
}
interrupt中断线程睡眠,不想让线程继续睡眠了
public class A {
public static void main(String[] args) throws Exception {
Thread t = new Thread(new MyRunnable2());
t.setName("t");
t.start();
Thread.sleep(1000 * 5);
//终端t线程睡眠
t.interrupt();
}
}
class MyRunnable2 implements Runnable{
//run()方法中的异常不能throws,只能try catch
//因为run()方法在父类中没有抛出任何异常,子类不能比父类抛出更多的异常
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "---begin");
//睡眠一年
try {
Thread.sleep(1000*60*60*24*365);
} catch (InterruptedException e) {
// throw new RuntimeException(e);
}
System.out.println(Thread.currentThread().getName() + "---end");
}
}
合理的终止线程
public class A {
public static void main(String[] args) throws Exception {
MyRunable4 r=new MyRunable4();
Thread t = new Thread(r);
t.setName("t");
t.start();
Thread.sleep(5000);
r.run = false;
}
}
class MyRunable4 implements Runnable{
boolean run = true;
@Override
public void run() {
for (int i = 0; i < 10; i++) {
if (run){
System.out.println(Thread.currentThread().getName() + "---" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}else {
//终止当前线程
return;
}
}
}
}
线程让位
public class A {
public static void main(String[] args) throws Exception {
Thread t = new Thread(new MyRunable4());
t.setName("t");
t.start();
for (int i = 0; i < 10000; i++) {
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
class MyRunable4 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
//每100个让位一下
if (i % 100 == 0){
Thread.yield();//当前线程暂停一下,让给主线程
}
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
线程合并 join
public class A {
public static void main(String[] args) throws Exception {
Thread t = new Thread(new MyRunable4());
t.setName("t");
t.start();
t.join();
}
}
class MyRunable4 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
//每100个让位一下
if (i % 100 == 0){
Thread.yield();//当前线程暂停一下,让给主线程
}
System.out.println(Thread.currentThread().getName() + "--->" + i);
}
}
}
synchronized
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3mGkCWvd-1672803022901)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221230153300804.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H3mhrqbW-1672803022901)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221230154051823.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gde9mljZ-1672803022902)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221230153353260.png)]
死锁
public class A {
public static void main(String[] args) throws Exception {
Object o1 = new Object();
Object o2 = new Object();
//t1和t2两个线程共享o1和o2
Thread t1 = new MyThread1(o1,o2);
Thread t2 = new MyThread2(o1,o2);
t1.start();
t2.start();
}
}
class MyThread1 extends Thread{
Object o1;
Object o2;
public MyThread1(Object o1,Object o2){
this.o1 = o1;
this.o2 = o2;
}
public void run() {
synchronized (o1){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (o2){
}
}
}
}
class MyThread2 extends Thread{
Object o1;
Object o2;
public MyThread2(Object o1,Object o2){
this.o1 = o1;
this.o2 = o2;
}
public void run() {
synchronized (o2){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (o1){
}
}
}
}
守护线程
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6uSkcsJT-1672803022902)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221231105114468.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TLCwA29W-1672803022903)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221231105519524.png)]
定时器
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VCdmH0Rl-1672803022903)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221231105819606.png)]
wait和notify
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DCoB5E61-1672803022903)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221231122034753.png)]
反射
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ncE8Sxsx-1672803022904)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20221231123759723.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fXkXIXR3-1672803022904)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20230101194002661.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-km4ytcOk-1672803022905)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20230101205755524.png)]
普通与反射机制的对比
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L2SilS8H-1672803022905)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20230101210216268.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ek35wYgy-1672803022906)(C:\Users\14182\AppData\Roaming\Typora\typora-user-images\image-20230102192900867.png)]
//验证反射机制的灵活性
//java代码写一遍,再不改变Java源代码的基础上,可以做到不同对象的实例化,
//非常之灵活(符合ocp开放原则:对扩展开放,对修改关闭)
public class A {
public static void main(String[] args) throws Exception {
//这种方式代码就写死了,只能创建一个user类型的对象
//User user = new User();
//以下代码是灵活的,代码不需要改动,可以修改配置文件,配置文件修改后,可以创建出不同的实例对象
//通过io流读取classinfo.properties文件
FileReader reader = new FileReader("");
//创建属性类map
Properties pro = new Properties();
//加载
pro.load(reader);
//关闭流
reader.close();
//通过key获取value
String className = pro.getProperty("className");
//通过反射机制实例化对象
Class c = Class.forName(className);
Object obj = c.newInstance();
System.out.println(obj);
}
}
public static void main(String[] args) throws Exception {
//不使用反射机制获取对象
Student s = new Student();
//给属性赋值
s.no=111;
//读属性
System.out.println(s.no);
//使用反射机制
Class studentClass = Class.forName("test.Student");
Object obj = studentClass.newInstance();//student对象
//获取no属性,根据属性的名称来获取field
Field noField = studentClass.getDeclaredField("no");
//给obj对象的no属性赋值
noField.set(obj,2222);
}
到不同对象的实例化,
//非常之灵活(符合ocp开放原则:对扩展开放,对修改关闭)
public class A {
public static void main(String[] args) throws Exception {
//这种方式代码就写死了,只能创建一个user类型的对象
//User user = new User();
//以下代码是灵活的,代码不需要改动,可以修改配置文件,配置文件修改后,可以创建出不同的实例对象
//通过io流读取classinfo.properties文件
FileReader reader = new FileReader("");
//创建属性类map
Properties pro = new Properties();
//加载
pro.load(reader);
//关闭流
reader.close();
//通过key获取value
String className = pro.getProperty("className");
//通过反射机制实例化对象
Class c = Class.forName(className);
Object obj = c.newInstance();
System.out.println(obj);
}
}
```java
public static void main(String[] args) throws Exception {
//不使用反射机制获取对象
Student s = new Student();
//给属性赋值
s.no=111;
//读属性
System.out.println(s.no);
//使用反射机制
Class studentClass = Class.forName("test.Student");
Object obj = studentClass.newInstance();//student对象
//获取no属性,根据属性的名称来获取field
Field noField = studentClass.getDeclaredField("no");
//给obj对象的no属性赋值
noField.set(obj,2222);
}