1、异常:
a. 分类:Throwable
Error:错误
Exception:异常
编译时期异常:一编译,就爆红(主要还是调用了某个方法,某个方法底层抛了一个编译时期异常)
Exceptiion以及子类(除了RuntimeException之外)
运行时期异常:一运行就报错
runtimeException以及子类
2、异常处理:
throws
try...catch
3、finally:不管是否有异常都会执行的代码,要配合try...catch使用
finally的使用场景:关闭资源使用
4、自定义异常:
a.定义一个类,继承Exception,变成了编译时异常
继承RuntimeException,变成了运行时期异常
b.提供构造方法,便于设置异常信息
5、Object:所有类的根类,任何类都会直接或者间接继承Object类
a.toString方法:
没有重写,直接输出对象名,调用的是Object中的toString方法,输出地址值
重写了,直接输出对象名,默认调用重写的toString方法,输出对象内容。
b.equals方法:
没有重写,比较对象地址值
重写了,比较对象内容
c.clone方法:需要实现Cloneable接口,重写clone方法
赋值一个地址值不一样,属性值一样的对象
6、经典接口:
Comparable
Comparator
API
String字符串
StringBuilder
都是用于字符串的。
String基础知识以及创建
String常用方法
String的介绍
1、概述:String类代表字符串
2、特点:
a.Java程序中的所有字符串字面值(如 “abc”)都作为此类的实例(说得很牛,但其实实例就是对象来着)实现。
凡是带引号的,都是String的对象。
String s = "abc"
"abc"就是对象;String就是对象的数据类型;s就是对象名。只不过我们定义字符串不用像之前定义对象那样,new一下子,这种就是简化的创建字符串的形式。
The String class represents character string. All string literals in Java programs, such as "abc", are implemented as instances of this class.
b.字符串是个常量,它们的值在创建后不能更改。应该底层有个final。有final修饰的就不能被更改。
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.
String s = "hello";
s+="World"; // 会产生新对象
c. String 对象是不可变的,所以可以共享
String s1 = "abc"
String s2 = "abc"
s1 == s2; // true
2、String的实现原理
其实字符串的原貌是数组。
1、jdk8的时候,String底层是一个被final修饰的char数组 -> private final char[] value;
2、jdk9开始到之后:底层是一个被final修饰的byte数组-> private final byte[] value;
我们jdk版更新,除了jdk8开始,可操作性的技术和新技术会多一些,所以除了拉满表达式,出了新的编程思想,
jdk更新之后除了8,往后其实主要是什么,就是对内存的优化。其实可操作性的技术其实没有特别多。
所以说,为什么从char变成了byte
一个char类型占2个字节
一个byte类型占1个字节 - 省内存空间
字符串定义完之后,数组就创建好了,被final一修饰,数组的地址值直接定死。
数组地址值变不了,导致我们字符串定义完之后也就更改不了。
String的创建
String 是怎么创建的,创建的无非就是对象或者直接赋值,new对象的话,肯定就需要new 构造。
1、String() -> 利用String的无参构造创建String对象
2、String(String original) -> 根据字符串创建String对象
3、String(char[]) -> 根据char数组去创建String对象
4、String(byte[] bytes) -> 通过使用平台的默认字符集解码指定的byte数组,构造一个新的String.
什么叫平台:平台其实指的就是咱们的操作系统。
操作系统默认字符集:GBK。讲jdk的时候提过,黑窗口编码就是gbk,
GBK:一个中文占2个字节
UTF-8:一个中文占3个字节
而且,中文对应的字节一般都是负数。
代码在idea中写的,idea启动的时候,会自动加一个启动参数,此启动参数为UTF-8
-Dfile.encoding=UTF-8
还有一个简化写法:
String 变量名 = ""
public class DemoString {
public static void main(String[] args) {
String s1 = new String();
String s2 = new String("abc");
char[] chars = {'a', 'b', 'c'};
String s3 = new String(chars);
}
}
byte[] bytes1 = {97,98,99};
String s4 = new String(bytes1);
System.out.println(s4); // abc
byte[] bytes1 = {-97,-98,-99};
String s4 = new String(bytes1);
System.out.println(s4); // abc
一旦是负数,这三个字节就要代表中文了。当在对应的编码表里面找不到对应的中文,就会乱码,
String s7 = "abc";
System.out.println(s7);
1、String(char[] value, int offset, int count) -> 将char数组的一部分转成String对象
value:要转String的char数组
offset:从数组的哪个索引开始转
count:转多少个
2、String(byte[] bytes, int offset, int length) -> 将byte数组的一部分转成String对象
bytes:要转String的byte数组
offset:从数组的哪个索引开始转
length:转多少个
char[] chars = {'a', 'b', 'c', 'd', 'e', 'f'};
String s1 = new String(chars, 1, 3);
System.out.println(s1);
从索引1开始,数3个。
byte[] bytes = {97, 98, 99, 100, 101};
String s2 = new String(bytes, 0.2);
System.out.println(s2);