文章目录
一、字符串(String)
字符串被final修饰不能有子类
常用的构造方法
1.无参构造
2.Byte[]参数 构造
3.Char[] 参数 构造
4.StringBuffer 参数构造
5.StringBulider 参数构造
1.常用的方法
charAt()
copareTo()
concat()
contains()
equals()
endsWith()
startsWith()
indexOf()
intern()
replace()
split()
subString()
2.正则表达式 pattern
代码如下:
正则表达式可以判断输入的字符串是否是符合你正则表达式的约束标准的
System.out.println("请输入字符");
Scanner input = new Scanner(System.in);
String key = input.next();
if(key.matches("[abc]")){
System.out.println(key);
}else{
System.err.println(key);
}
注意:正则表达式在没有给定长度的时候匹配都是单个字符
预定义字符:
比较常用的是\d 和\w
边界器匹配器
^ -------正则表达式的开始
$ -------正则表达式的结束
通配符:*
3.StringBuffer 和 StringBuilder
面试题:String ,StringBuffer,StringBuilder的区别:
-
String:不可变字符序列
-
Stringbuffer:安全的可变字符序列
Append 表示从字符串的后面添加新的内容 Insert 表示从字符串中间位置进行插入 -
Stringbuider:不安全的可变字符序列
适用于单线程 运行速度比StringBuffer快 API用法和StringBuffer兼容。
package com.openlab.array;
public class StringTest {
public static void main(String[] args) {
String result = "123456";
StringBuffer sb = new StringBuffer(result);
StringBuilder sb1 = new StringBuilder();
sb.append(true)
.append("1")
.append(1)
.append(0.0);
}
}
二、常用类
Math 数学类 用于数学计算
Random 伪随机数
Date 时间
SimpleDateFormat
Caledar 日期
File 文件
1、Math中的常用方法
常量E ----科学计数法
常量PI ---- π 圆周率
测试 floor(double)
Pow()
random() 0.0-1.0
round()
sqrt()
2、random伪随机数
package com.openlab.array;
import java.util.Random;
public class StringTest {
public static void main(String[] args) {
Random rand = new Random(17);
String[] bRet = {"钢铁","蜘蛛","绿人","寡妇"};
for (int i = 0; i < bRet.length; i++) {
int b = rand.nextInt(bRet.length-1);
System.out.println(bRet[b]);
}
}
}
3、data时间
构造函数两个
- 无参的 获取当前的系统时间
- Long 封装long time 数据 用于定义某个时间点
4、Calender日期
抽象类 无法进行实例化
所以通过单列来实例化
package com.openlab.array;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringTest {
public static void main(String[] args) {
Date date = new Date(1622540032005L);
// long timer = date.getTime();
// //通过单例模式获取Calendar 对象
//
// Calendar c = Calendar.getInstance();
String s = DateFormat.getDateTimeInstance().format(date);
System.out.println(s);
}
}
//通过单例模式获取Calendar 对象
Calendar c = Calendar.getInstance();
5、SimpleDataFormat
主要负责的事情就是时间类型和字符串类型的转换
来自于java.text 父类是DateFormat ------Format ----Object
根据此表可以自定义输出的时间格式
SimpleDateFormat format = new SimpleDateFormat("yyyy年-MM月-dd日-HH小时:mm分:ss秒");
String datatext = format.format(date);
System.out.println(datatext);
三、异常 Exception
1、什么叫异常
Throwable 是所有异常或错误的父类 ,它的父类是Object 它具有两个子类 一个是Error另一个是Exception
-
Exception 是程序在运行时发生错误或异常
-
Error 错误 程序在运行时发生的非人为的难以避免,并且不好解决的特殊情况
比如 当过多的使用递归时 会出现栈的溢出 Error
服务器烧毁,内存不够 堆空间溢出
2、异常的分类
Exception —异常
为什么要有异常
1.可以让程序继续执行
2.用来修改bug
-
运行时异常 RunTimeException ----非检查异常
程序员人为性的代码上的错误
java.lang.ArithmeticException
NullPointerException
NumberFormatException
ClassCastException
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException -
非运行时异常 ----检查异常
是指程序编写的过程会出现一些安全的隐患,一些异常需要检查,而这些需要检查的异常,其实就是非运行时异常 –也被叫做检查异常
FileNotFoundException
IOException
都属于 非运行时异常,是在代码编写的过程中是一定要被处理的
3、异常的处理方式
3.1、捕获
Try —catch ----finally
try 块{
//可能会有异常发生的代码
}catch(Exception e){
// 当有异常发生的时候,catch块被执行
当没有异常发生的时候 catch块就不执行了
}finally{
不管异常是否发生都要执行
}
1.try块中有return关键字时 如果没有finally 则直接返回
2.try块中有return 关键字 catch块也有return 如有有异常时则作走catch块的return
如果没有异常则走try里的return
3.当try块中有return finally中也有return 如果没有异常发生走finally里的return
4.当try块中有return catch也有return finally还有return
没异常按照第3条走,如果有异常 则按照finally中的返回结果执行。
package com.openlab.exception;
public class Test {
public static void main(String[] args) {
Test t = new Test();
Test tt =t.get();
System.out.println(tt);
}
public Test get(){
int a = 10;
int b = 0;
try{
int c = 10/0;
System.out.println("444");
return null;
}catch(Exception e){
try{
System.out.println("123");
}catch(Exception ex){
}
return this;
}finally{
System.out.println("我是一定要执行的");
return null;
}
}
}
3.2、抛出
public static void main(String[] args) {
//抛出异常throw
String id;
System.out.println("请输入您的员工编号");
Scanner input = new Scanner(System.in);
id = input.next();
if(id.length() == 6){
System.out.println("你是一个合格的员工");
}else{
throw new IllegalArgumentException("你的id不符合要求,请联系上帝");
}
}
3.3、声明
throws 声明异常 一般情况下用在你的方法头
public boolean createNewFile() throws IOException {
SecurityManager security = System.getSecurityManager();
if (security != null) security.checkWrite(path);
if (isInvalid()) {
throw new IOException("Invalid file path");
}
return fs.createFileExclusively(path);
}
面试题 :throw和throws的区别? 从关键字和用法说区别? throw 抛出 用于抛出一些运行时异常 在代码中可以自定义逻辑
也可以抛出自定义的异常 throws 声明异常 用于方法异常声明 主要是声明非运行时异常,做检查使用
4、自定义异常
Exception RunTimeException
用于在自己的框架中描述一些自定义的异常类,展示和Java原有的异常信息不同
实现方案:
要借助 Exception 和 RunTimeException
package com.openlab.exception;
public class MyDaoException extends RuntimeException {
static final long serialVersionUID = -7034897190745766939L;
public MyDaoException() {
super();
}
public MyDaoException(String message) {
super(message);
}
public MyDaoException(Throwable cause) {
super(cause);
}
public MyDaoException(String message, Throwable cause) {
super(message, cause);
}
}
4、多个异常处理
在多个catch块中 异常处理的顺序 要遵循:
先小后大, 异常的抛出要相对准确,不建议捕获异常的父类级别的异常类例如想Exception或者Throwable 以及RuntimeException 如果能够进准到具体的哪个异常可以大大地增加代码的可读性和排错性。
package com.openlab.exception;
public class Test2 {
public static void main(String[] args) {
try{
String key = null;
char d = key.charAt(0);
System.out.println(d);
int a = 10;
int b = 0;
int c = 10/0;
}catch(NullPointerException e){
}catch(ArithmeticException e){
}catch(Exception e){
}
}
}
五、文件
目录和文件的抽象的表示形式
url
package com.openlab.exception;
import java.io.File;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) {
File file = new File("mydir1/mydir2/a.txt");
if(file.exists()){
System.out.println(file.length());
}else{
//获取目录
File parent = file.getParentFile();
parent.mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}