java-初识
****JDK和JRE的区别
JDK是开发环境,JRE是运行环境
command: javac 编译打包
java 执行运行java包
java官方文档https://docs.oracle.com/en/java/javase/18/docs/api
字符是不可变类型(同一个内存地址值不可变)
容器概述:
能装对象的对象
1. list 线性结构 arrayList列表/linkList(链表)
泛型:
规范容器内的数据类型 ex:List
IO流概念:
File file = new File("文件名")
file.createNewFile //创建文件,在当前文件目录下
file.getParentile() //获取文件夹的对象
file.getParent() //获取文件路径、
file.mkdir // 创建文件(仅限一个层级)
file.mkdir // 递归创建文件目录
file.delete() //删除文件
InputStream 文件输入流
OutputStream 输出流
Reader
Writer
JAVA架构图: https://www.edrawsoft.cn/viewer/public/s/d3304014062252
读取文件
FileInputStream file = new FileInputStream(new File("a.txt"));
byte[] bytes = new byte[1024]; //生成1024长度的空字符数组
int len = 0;
while((len=file.read(bytes))!=-1){
String s = new String(bytes);
System.out.print(s);
}
file.close();
缓冲读取文件(BufferReader/BufferWriter)
BufferedReader br = new BufferedReader(new FileReader(new File("a.txt")));
String str = "";
while((str=br.readLine())!=null) {
System.out.println(str);
}
br.close()
转换流 InputStreamReader/OutputStreamWriter
- BufferReader br = new BufferReader(new InputStreamReader(System.in)) #捕捉输入字节流转换成字符流
String s = br.readLine(); - OutputStreamWriter bw = new OutputStreamWriter(System.out);
br.writer("输出流")
br.flush()
对象流 ObjectInputStream/ObjectOutputStream
序列化:把一个对象转化成字节的过程
反序列化:把字节转换成对象(在java的对象中添加implment Serizlizable)
private 仅限于类内部条用
序列化
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
Person p = new Person(1, "蔡徐坤", 18);
oos.writeObject(p);
oos.flush();
oos.close();
反序列化
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
Object obj = ois.readObject();
Person p = (Person) obj;
System.out.print(p.getAge());
线程:(先生成我的线程)
public class Mythread extends Thread {
public Mythread(String name) {
super.setName(name);
super.setpriority(1~10)//设置优先级,目前window有效
}
@Override
public void run() {
super.run(); //执行自己线程的方法或者是父类Thread run方法
}
public static void main(String[] args) {
Mythread mt1 = new Mythread("thread1");
mt1.start(); //mt1.join() 从start主从cpu调度执行,join等待从线程执行完,主线程继续往下执行
}
}
public synchronized void setAge(int age) {
this.age = age; //synchronized 加锁,同一时刻只能调用一次(串行),针对整个函数
synchronized(this){};局部demo上锁
}
static:声明是类方法,不是类实例方法.(咋类内部直接调用)
Static vs. Public: In the example above, we created a static method, which means that it can be accessed without creating an object of the class, unlike
public, which can only be accessed by objects:
继承inheritance: use keyword EXTEND after subClass ex: `class Car extends Vehicle {}
KEYWORD final:修饰变量,则声明是常量,修饰类,则该类无法被继承
创建线程的两种方式(继承Thread和实现接口Runnable)
public class Main extends Thread {
public static void main(String[] args) {
Main thread = new Main();
thread.start();
System.out.println("This code is outside of the thread");
}
public void run() {
System.out.println("This code is running in a thread");
}
}
```plaintext第二种
public class Main implements Runnable {
public static void main(String[] args) {
Main obj = new Main();
Thread thread = new Thread(obj);
thread.start();
System.out.println("This code is outside of the thread");
}
public void run() {
System.out.println("This code is running in a thread");
}
}第二种相比第一,还可以继承其他类,实现功能扩展(java只有一个父类)