一 异常
1.异常的体系:
Throwable
/ \
Error Exception
(1)Error:
不需要管,一般指虚拟机问题,你想管也管不了
UnCheckedException:不可检查时异常
(2)Exception:
1)CheckedException:检查时异常|编译时异常
如果出现检查时异常,如果不处理程序无法继续执行
2)RuntimeException:运行时异常
可以简单的通过增强程序的健壮性处理 if..
所有的运行时异常都会直接或者间接的继承自RuntimeException
(3)几种运行时常见的异常:
a.NullPointerException 空指针
b.ArrayIndexOutOfBoundsException 数组索引越界异常
c.NegativeArraySizeException 数组长度负数异常
d.ClassCastException 类型转换异常
e.ArithmeticException 数学异常
f.NumberFormatException 数字转换异常
2.异常处理的两种方式
throw : 制造异常
(1)throws : 抛出异常
(2)包异常 : 捕获异常
try {
会出现异常的代码;
}catch (Exception e) { //Exception e=new NullPointerException();
e.printStackTrace();
//出现这个异常后会执行的代码;
}catch (NullPointerException e) {
e.printStackTrace();
//出现这个异常后会执行的代码;
}finally{
无论以上是否出现异常,都会执行finally中的代码
}
1.catch可以多个,至少有一个
2.接受小范围异常的catch要写在上面,大范围写在下面
3.try中的代码,一旦遇到异常就不会继续执行,会找对应的catch执行其中的代码
方法的重写:
子类方法上抛出异常大小<=父类中方法上抛出的异常
3.自定义异常类
要直接或者间接继承自Exception或者它的子类
二 常用类
1.String:字符串
""字面常量值都作为String类型的实例对象
底层是字符数组 char[] 而且被final修饰,所以长度不可变
String 不可变长字符串 构造器 方法
StringBuilder :可变长字符串,线程不安全的,效率较高
StringBuffer:可变长字符串,线程安全的,效率较低
默认初始容量为16的字符数组
效率排序:StringBuilder>StringBuffer>String
(1)String构造代码
public class StringDemo01 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str="abc"; //"abc"是一个String对象
//String() 初始化一个新创建的 String 对象,使其表示一个空字符序列。
String str1=new String();
System.out.println(str1);
//String(String original) 初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。
String str2=new String("abc");
String str3=new String("abc");
String str4="abc";
System.out.println(str2);
System.out.println(str2==str); //false
System.out.println(str2.equals(str)); //true
System.out.println(str2==str3); //false
System.out.println(str==str4); //true
//String(char[] value) 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
String str5=new String(new char[]{'a','b','c'});
System.out.println(str5); //abc
//String(char[] value, int offset, int count) 分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
String str6=new String(new char[]{'你','好','中','国'},2,2);
System.out.println(str6); //你好
//String(byte[] bytes) 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
byte[] arr="因为".getBytes();
byte[] arr2="因为".getBytes("gbk");
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
String str7=new String(new byte[]{67,68,69});
//String(byte[] bytes, String charsetName) 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
String str8=new String(arr,"gbk");
System.out.println(str7);
System.out.println(str8);
//String(byte[] bytes, int offset, int length) 通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。
String str9=new String(arr,3,3);
System.out.println(str9);
/*
* String(StringBuffer buffer)
分配一个新的字符串,它包含字符串缓冲区参数中当前包含的字符序列。
String(StringBuilder builder)
分配一个新的字符串,它包含字符串生成器参数中当前包含的字符序列。
*/
}
}
(2)String方法代码
public class StringDemo02 {
public static void main(String[] args) {
String str="shsxtverygood";
String str1="Shsxtverygood";
//char charAt(int index) 返回指定索引处的 char 值。
System.out.println("charAt():"+str.charAt(3));
//int codePointAt(int index) 返回指定索引处的字符(Unicode 代码点)。
System.out.println("codePointAt():"+str.codePointAt(3));
//int codePointBefore(int index) 返回指定索引之前的字符(Unicode 代码点)。
System.out.println("codePointBefore():"+str.codePointBefore(3));
//int compareTo(String anotherString) 按字典顺序比较两个字符串。
System.out.println("compareTo():"+str.compareTo(str1));
//int compareToIgnoreCase(String str) 按字典顺序比较两个字符串,不考虑大小写。
System.out.println("compareToIgnoreCase():"+str.compareToIgnoreCase(str1)); //0
//concat(String str) 将指定字符串连接到此字符串的结尾。
System.out.println("concat():"+str.concat(str1)); //0
// boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。
System.out.println("contains():"+str.contains("sssssxt")); //0
//static String copyValueOf(char[] data) 返回指定数组中表示该字符序列的 String。
System.out.println("copyValueOf():"+(String.copyValueOf(new char[]{'1','3','5'}).length())); //135
//boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。
System.out.println("endsWith():"+str.endsWith("good")); //true
//void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将字符从此字符串复制到目标字符数组。
char[] ch=new char[10];
str.getChars(5, 9, ch, 2);
System.out.println(Arrays.toString(ch));
//int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引。
System.out.println("indexOf():"+str.indexOf("s")); //0
//int lastIndexOf(String str) 返回指定子字符串在此字符串中最右边出现处的索引。
System.out.println("lastIndexOf():"+str.lastIndexOf("s")); //true
//String replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
System.out.println("replace():"+str.replace('s','S')); //true
System.out.println(str);
//String[] split(String "") 根据给定分隔符的匹配拆分此字符串。
String user="uanme=张三";
String[] arr=user.split("=");
System.out.println(Arrays.toString(arr));
System.out.println(arr[1]);
//String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。
System.out.println("substring():"+str.substring(2,5)); //true
//char[] toCharArray() 将此字符串转换为一个新的字符数组。
char[] arrChar=str.toCharArray();
System.out.println(arrChar);
//String trim() 返回字符串的副本,忽略前导空白和尾部空白。
String str10=" zhangsan ";
System.out.println(str10.trim());
//static String valueOf(boolean b) 返回 boolean 参数的字符串表示形式。
System.out.println(String.valueOf(false).length()); //"false"
}
}
(3)StringBuilder代码
public class StringDemo03 {
public static void main(String[] args) {
String str="abc";
// System.out.println(str.hashCode());
// str.replace('a', 'A');
//StringBuilder() 构造一个其中不带字符的字符串生成器,初始容量为 16 个字符。
StringBuilder sb=new StringBuilder();
System.out.println(sb);
System.out.println(sb.capacity());
//StringBuilder(int capacity) 构造一个字符串生成器没有指定的字符和一个初始容量 capacity论点。
sb=new StringBuilder(20);
System.out.println(sb.capacity());
//StringBuilder(String str) 构造一个字符串生成器初始化为指定的字符串的内容。
sb=new StringBuilder("abc");
System.out.println(sb.capacity()); //19 16+str.length()
//追加 append(double d) 附加 double参数的字符串表示这个序列。
System.out.println(sb.length());
System.out.println(sb.append(false));
System.out.println(sb.capacity());
System.out.println(sb.length());
//删除 StringBuilder delete(int start, int end) int end结束索引取不到
System.out.println(sb.delete(2,4));
System.out.println(sb);
/*//指定位置插入 StringBuilder insert(int offset, String str)
sb.insert(2, "ffff");
System.out.println(sb);
//反转 StringBuilder reverse()
System.out.println(sb.reverse());
String s=new String(sb);
System.out.println(s);
StringBuffer sb2=new StringBuffer("哈哈");
sb2.reverse();
}
2.基本数据类型的包装类
基本数据类型 包装类
byte ---- Byte
short --- Short
int ---- Integer
long ---- Long
float ---- Float
double ---- Double
char ---- Character
int --> Integer
自动装箱:基本数据类型-->包装类型
Integer --> int
自动拆箱:包装类型--->基本数据类型
public class Data04 {
public static void main(String[] args) {
int a=5;
Integer i1=a; //自动装箱 Integer i1= Integer.valueOf(a);
int int1=i1; //自动拆箱 int int1=i1.intvalue()
test(1.1,2.2);
Integer in1=127;
Integer in2=127;
Integer in3=new Integer(127);
Integer in4=new Integer(127);
int in5=127;
Integer in6=128;
Integer in7=128;
System.out.println(in1==in2); //true
System.out.println(in2==in3); //false
System.out.println(in3==in4); //false
System.out.println(in5==in4); //true 自动拆箱
System.out.println(in5==in2); //true 自动拆箱
System.out.println(in6==in7); //false
Integer.valueOf(128);
//缓冲区对象所表示的范围: [-128,127],在其范围之内是同一个Integer对象,在其范围之外,返回new Integer()
}
static void test(Double d1,Double d2){ //自动装箱 Double d1=1.1;
System.out.println(d1+d2); //自动拆箱运算
}
}
3.Math 数学类
public class Math04 {
public static void main(String[] args) {
//static double ceil(double a) 向上取整
System.out.println(Math.ceil(-3.9));//-3.0
//static double floor(double a) 向下取整
System.out.println(Math.floor(-3.3));//-4.0
/*
比较
* static long max(long a, long b)
返回两个 long 值中较大的一个。
static double min(double a, double b)
返回两个 double 值中较小的一个。
*/
System.out.println(Math.max(1.2,2.2));
System.out.println(Math.min(1.2,2.2));
//static long round(double a) 返回最接近参数的 long。
//static double random() 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
}
}
4.Date 日期类
构造器:
Date() 以本地时间构建日期对象
Date(long time) 以参数毫秒数从1970 年 1 月 1 日 00:00:00开始计算
1.导包java.util.Date
2.毫秒数都是以1970 年 1 月 1 日 00:00:00 GMT)为准
(1)Date日期类代码
public class Date01 {
public static void main(String[] args) {
Date date=new Date();
System.out.println(date);
//System.out.println(date.toString().length());
//long getTime() 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
System.out.println(date.getTime());
Date date2=new Date(1551428482079L);
System.out.println(date2);
System.out.println(date==date2);
// boolean equals(Object obj) 比较两个日期的相等性。
System.out.println(date.equals(date2)); //true
/*
* boolean after(Date when)
测试此日期是否在指定日期之后。
boolean before(Date when)
测试此日期是否在指定日期之前。
*/
System.out.println(date.after(date2));
System.out.println(date.before(date2));
/*
* int compareTo(Date anotherDate) 比较两个日期的顺序。
*/
System.out.println(date.compareTo(date2)); //1
//static long currentTimeMillis() 返回以毫秒为单位的当前时间。
System.out.println(System.currentTimeMillis());
System.out.println(date.getTime());
}
}
(2)SimpleDateFormat 格式类
y 年
M 月
d 日
H 24小时
h 12小时
m 分
s 秒
1.format 日期对象转字符串,指定格式转换
2.parse 字符串转日期对象
1s=1000ms
SimpleDateFormat 格式类代码
public class SimpleDateFormat02 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat(); //根据空构造创建对象,默认转换格式
SimpleDateFormat simpleDateFormat2=new SimpleDateFormat("yyyy/MM/dd E a hh:mm:ss"); //根据空构造创建对象,默认转换格式
//日期对象转为字符串
System.out.println(simpleDateFormat2.format(new Date()));;
//字符串转为日期对象
String str="2019/03/28 星期四 16:39:06";
SimpleDateFormat simpleDateFormat3=new SimpleDateFormat("yyyy/MM/dd E HH:mm:ss");
System.out.println(simpleDateFormat3.parse(str).getTime());
}
}
5.枚举类 enum
列举所有可能
1.枚举类也是类,类中的字段|属性都是该类的一个实例,默认相当于使用public static final修饰
2.枚举类隐式的继承了java.lang.Enum
public class EnumDemo01 {
public static void main(String[] args) {
Week sun=Week.Mon;
System.out.println(sun.name()); //获取实例的名称
System.out.println(sun.ordinal()); //2 字段索引值
System.out.println(Arrays.toString(sun.values())); //[Mon, Thes, Sun]返回这个枚举类实例的数组
sun.setName("星期天");
sun.test();
System.out.println(sun.getName());
//switch (枚举)
switch(sun){
case Sun:
System.out.println(sun.getName()+"Sun");
break;
case Mon:
System.out.println(sun.getName()+"Mon");
break;
}
}
}
//定义枚举类 星期
enum Week{
Mon,
Thes,
Sun;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//可以定义成员方法
void test(){
System.out.println("hahahha"+name);
}
}
class WeekDay{
public static final WeekDay Mon=new WeekDay();
}