![c83092b32a20e3b857f5c3bd0fb7722d.png](https://img-blog.csdnimg.cn/img_convert/c83092b32a20e3b857f5c3bd0fb7722d.png)
异常处理、String类、基本数据类型的包装类Date 日期类
一、异常
异常:程序生病了
* 异常的分类
* 异常的处理 ***
* 异常的定义
*
* Throwable
* Error Exception
* / /
* UnChecked Checked Runtime
Exception Exception Exception
*
* Error:错误,一般由虚拟机生成并脱出,程序员无法控制,不需要关注
*
* Exception:异常
* CheckedException检查时异常|编译时异常:是在编译时出现的,如果不处理,程序无法执行。
* RuntimeException运行时异常:是在程序运行的时候出现,简单的可以通过增强程序的健壮性来处理(if判断)。
*
* 常见的运行时异常:
* 1.空指针 NullPointerException
* 2.数组索引越界 ArrayIndexOutOfBoundsException
* 3.类型转换异常 ClassCastException (引用数据类型父类|父接口引用强转为实现类|子类类型)
* 4.数学异常 ArithmeticException
* 5.负数异常 NegativeArraySizeException
* 6.数字格式异常 NumberFormatException
*
* throw : 制造异常
遇到异常,不同的异常见名知意,非常直观,方便调试。
* 如果遇到异常不处理,程序无法继续向下执行。
*
* throw : 制造异常
* 异常处理方式:
- throws : 抛出异常
- try..catch 捕获异常
* try{
* 可能会出现异常的代码;
* }catch(StringIndexOutOfBoundsException e){
* e....
* 如果出现这个类型的异常,就执行的代码
* }catch(NullPointerException e){
* ...
* }catch(FileNotFoundException e){
* ..
* }catch(Exception e){
* ....
* }finally{
* 无论是否出现异常,肯定会执行的代码
* }
*
*
* 1.异常都可以使用异常处理的2种方式进行处理
* 编译时异常必须使用这两种方式处理,运行时异常可以使用这两种方式也可以使用增强程序的健壮性
* 2.抛出异常,当前不处理,异常抛出到上一层,方法的内部把异常抛出到方法上面,谁调用这个方法,谁处理
* 3.异常捕获,在遇到异常的时候当前位置就处理,捕获某个异常对象,使用异常的功能
* 4.如果try中遇到异常了,try中下面的代码不会执行,直接去执行对应catch中的内容,如果没有遇到异常,正常执行所有的try中代码
* 5.一个try后可以跟一到多个catch,捕获小范围的异常catch写在上面,否则下面的catch执行不到
* 6.finally中的内容无论当前trycatch结构是否遇到异常都会执行其中的代码,与前面的try..catch是一个结构。
自定义异常:除了java提供的这些异常类以外,自定义异常类。
要求的异常类要继承Exception|RuntimeException |等子类。
* 常用类: String,基本数据类型的包装类,Date日期类...
*
* String : 不可变长的字符序列 *****
* 多字符换没有大小修改或拼接,灵活使用当前字符串对象String
* StringBuilder : 可变长的字符序列,线程不安全,效率较高
* 单线程环境下,如果大量的对字符串做修改,推荐使用StringBuilder
* StringBuffer : 可变长的字符序列,线程安全,效率较低
* 多线程环境下,需要保证线程安全的是哦户,如果大量的对字符串做修改,推荐使用StringBuffer
*
* 执行效率: StringBuilder > StringBuffer > String
*
二、String
* String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
* 字符串常量:字符串常量池中出现,使用时会先检查常量池中是否存在这个字符串常量对象,如果直接使用,不存在创建新的字符串常量对象
* 通过构造器创建对象:new-->对象在堆中,每执行new都是新的对象
常用用法:
//String(String original) 初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。
//String(char[] value) 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
//String(char[] value, int offset, int count) 分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
//String(byte[] bytes) 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
//String(byte[] bytes, String charsetName) 自定字符编码格式。
//String(byte[] bytes, int offset, int length) 通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。
//String(byte[] bytes, int offset, int length, String charsetName) 通过使用指定的字符集解码指定的 byte 子数组,构造一个新的 String。
String中常用的方法: 学会看API。
package com.shsxt.str02;
import java.util.Arrays;
public class StringDemo02 {
public static void main(String[] args) {
String str="shsxtverygood";
String str2="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 compareTo(String anotherString) 按字典顺序比较两个字符串
System.out.println("compareTo()-->"+str.compareTo(str2));
// int compareToIgnoreCase(String str) 按字典顺序比较两个字符串,不考虑大小写。
System.out.println("compareToIgnoreCase()-->"+str.compareToIgnoreCase(str2));
//String concat(String str) 将指定字符串连接到此字符串的结尾。 ***
System.out.println("concat()-->"+str.concat(str2));
// boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。 ***
System.out.println("contains()-->"+str.contains("sxt"));
/*
* static String copyValueOf(char[] data)
返回指定数组中表示该字符序列的 String。
static String copyValueOf(char[] data, int offset, int count)
返回指定数组中表示该字符序列的 String。
*/
System.out.println("copyValueOf()-->"+String.copyValueOf(new char[]{'a','b','c'}));
/*
* boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始。
* boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束
*/
System.out.println("startsWith()-->"+str.startsWith("shsxt"));
/*
* byte[] getBytes() 字符串转字节数组 ****
* byte[] getBytes(String charsetName)
*/
System.out.println("getBytes()-->"+Arrays.toString(str.getBytes()));
/*
* int indexOf(int ch) 根据字符找索引,第一次出现
* int indexOf(String str) ***
返回指定子字符串在此字符串中第一次出现处的索引。
int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
int lastIndexOf(String str) 返回指定子字符串在此字符串中最右边出现处的索引。
*/
System.out.println("indexOf()-->"+str.indexOf("sxt",3));
System.out.println("indexOf()-->"+str.lastIndexOf("s"));
// int length() 返回此字符串的长度。
System.out.println("length()-->"+str.length());
//String replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
System.out.println("replace()-->"+str.replace("s","S"));
//String[] split(String regex) 根据给定正则表达式的匹配拆分此字符串。 ****
String s="abc1fjs1kdjflds1dsf";
System.out.println("split()-->"+Arrays.toString(s.split("1")));
String value="name=zhangsan";
String name="";
/*
* String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。 ****
* String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。
* 结束索引endIndex不包含
*/
System.out.println("substring()-->"+str.substring(2));
System.out.println("substring()-->"+str.substring(2,5));
//char[] toCharArray() 将此字符串转换为一个新的字符数组。 ***
System.out.println("toCharArray()-->"+Arrays.toString(str.toCharArray()));
/*
* String toUpperCase() 字符串转大写
* String toLowerCase() 字符串转小写
*/
System.out.println("toUpperCase()-->"+str.toUpperCase());
String s2=" 3 2432 ";
// String trim() 去前后空格 ***
System.out.println("trim()-->"+s2.trim());
//static String valueOf(int i) 返回 int 参数的字符串表示形式。
System.out.println("valueOf()-->"+String.valueOf(true));
}
}
三、基本数据类型的包装类
* byte --- Byte
* short --- Short
* int --- Integer
* long --- Long
* float --- Float
* double--- Double
* char --- Character
* boolean --- Boolean
*
* 自动拆装箱:
* 自动装箱: 基本-->引用
* 自动拆箱: 引用-->基本
package com.shsxt.data03;
public class DataDemo01 extends Object{
public static void main(String[] args) {
int a=5;
Integer i1=a; //自动装箱 Integer.valueOf(a)
System.out.println(i1+1);
int i=i1; //自动拆箱 i1.intValue() -->返回int类型的值
Integer i2=new Integer("123435");
System.out.println(i2);
int i5=127; //整数常量池
Integer i3=127; //Integer.valueOf(127)
Integer i7=127; //如果缓冲区对象所能表示的范围之间[-128,127],返回缓冲区对象,否则返回new Integer
Integer i4=new Integer(127); //堆中
Integer i6=new Integer(127);
System.out.println(i3==i4); //false
System.out.println(i4==i6); //false
//Integer类型无论是否是new的,和基本数据类型比较,会自动拆箱,只要值相同肯定相同
System.out.println(i4==i5); //true
System.out.println(i3==i5); //true
Integer i8=128;
Integer i9=128;
System.out.println(i8==i9); //flase
}
}
四、Date 日期类
* 构造器:
* Date() 当前本机时间
* Date(long date) 分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数。
*
* 在java.util.Date日期类
package com.shsxt.date04;
import java.util.Date;
public class DateDemo01 {
public static void main(String[] args) {
//Date() 分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。
Date date=new Date();
System.out.println(date);
Date date1=new Date(9283472174982L);
System.out.println(date1);
//获取对象从1970年开始到现在的毫秒数
long l=date.getTime();
System.out.println(l);
Date date2=new Date(l);
System.out.println(date2);
System.out.println("--"+date2.toString());
/*
* boolean after(Date when)
测试此日期是否在指定日期之后。
boolean before(Date when)
测试此日期是否在指定日期之前。
int compareTo(Date anotherDate)
比较两个日期的顺序。
boolean equals(Object obj)
比较两个日期的相等性。
*/
System.out.println(date.after(date1));
System.out.println(date.before(date1));
System.out.println(date.equals(date2));
}
}