原先我们知道,Java的String型使用的是char数组进行存储,然而在9以后变成了byte数组进行存储

节省字符串占用内存,减少GC次数
非中文byte使用 Latin-1编码,中文采用UTF16
而若使用char数组,则是UTF-8编码,单字节也需要占用双空间(一个字节占用了两个字节的空间)
当然也跟着使用了byte数组进行存储
该方法可以直接将输入流转为输出流
public class CopyFile {
public static void main(String[] args) throws IOException {
String pathIn = "C:\\Users\\Syf200208161018\\Desktop\\软件.pdf";
String pathOut = "C:\\Users\\Syf200208161018\\Desktop\\copy.pdf";
//输入
FileInputStream fileInputStream = new FileInputStream(pathIn);
//输出
FileOutputStream fileOutputStream = new FileOutputStream(pathOut);
//读取后写入
byte[] bytes = new byte[1024];
int count = 0;
while ((count = fileInputStream.read(bytes))!=-1){
//确保输出不会溢出
fileOutputStream.write(bytes,0,count);
}
fileInputStream.close();
fileOutputStream.close();
}
}
public class Demo1 {
public static void main(String[] args) {
String path = "C:\\Users\\Syf200208161018\\Desktop\\neww.txt";
try (FileInputStream fileInputStream = new FileInputStream(path)) {
final FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Syf200208161018\\Desktop\\copy.txt");
fileInputStream.transferTo(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
开发者经常抱怨Java中引用代码的程度。局部变量的显示类型声明,常常被认为是不必须的,给一个好听的名字经常可以很清楚的表达出下面应该怎样继续
https://dzone.com/articles/var-work-in-progress
减少了啰嗦和形式的代码,避免了信息冗余,而且对齐了变量名,更容易阅读!
StringBuilder stringBuilder1 = new StringBuilder();
final var stringBuilder = new StringBuilder();
如下可以看到instanceof无法使用了

原因是int无法匹配转化Integer,当然其他的基本数据类型也是这样

直接使用valueOf方法声明就行
