目录
string 类
字符串常量池
Exception异常
1、string类
String 类代表字符串。
Java程序中所有字符串字面值(如:“abc”)都作为此类的实现。也就是说,"abc"都是String 类的对象。
字符串的特点:
1.字符串内容永远不可变(重点)
2.字符串可以共享使用。
3.字符串效果上是相当于一个char[],但是实际底层存储的是byte[]。
常用的三种构造方法:
1.public String() 创建一个空白的字符串,不包含任何内容。
2.public String(char[] array) 根据字符数组来创建字符串。
3.public String(byte[] array) 根据字节数组来创建字符串。
最直接的方式:String str = “class5”;
例一:
运行代码如下:
package demo01;
public class Demo01String {
public static void main(String[] args) {
String str1 = new String();//小括号留空,没有内容。
System.out.println(“第一个字符串:”+str1);
System.out.println("====================");
//字符数组
char[] chararray = new char[10];
System.out.println(chararray);
String str2 = new String(chararray);
System.out.println(str2);
System.out.println("====================");
//字节数组
byte[] bytearray = {10,20,30};
System.out.println(chararray);
String str3 = new String(chararray);
System.out.println(str3);
//直接创建字符串
//String str4 = new
String str4 = "class5";
System.out.println(str4);
}
}
2、字符串常量池
字符串常量池
int a = 5
int b = 6
对于基本类型 == 比较的是值
对于引用类型 == 比较的是地址值
例二:
运行代码如下:
package demo01;
public class Demo02StringPool {
public static void main(String[] args) {
String str1 = “abc”;
String str2 = “abc”;
System.out.println(str2);
char[] chararray = {‘a’,‘b’,‘c’};
String str3 = new String(chararray);
System.out.println(str1 == str2);//第一个和第二个的地址是一样的
System.out.println(str1 == str3);
System.out.println(str2 == str3);
str2 = "zxc";
System.out.println(str2);
}
}
package demo3;
public class Math {
public static void main(String[] args) {
//取绝对值
System.out.println(Math.abs(3.14));
System.out.println(Math.abs(0));
System.out.println(Math.abs(-3.14));
//向上取整
System.out.println(Math.ceil(3.91));
System.out.println(Math.ceil(3.5));
System.out.println(Math.ceil(3.01));
//向下取整
System.out.println(Math.floor(3.91));
System.out.println(Math.floor(3.5));
System.out.println(Math.floor(3.01));
//四舍五入
System.out.println(Math.round(20.4));
System.out.println(Math.round(20.5));
}
}
3.Exception异常
异常的处理
抛出异常:throw,throws
捕获异常:try,catch,finally
抛出异常throw
throw用在方法内,用来抛出一个异常对象,将这个异常对象传递到调用者处,并结 束当前方法的执行。
使用的格式:
throw new 异常类名(参数);
1
package demo2;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class exception {
public static void main(String[] args) {
//编译期异常
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”);
Date date=null;
try {
date=sdf.parse("2021-05-11");
}catch(ParseException e) {
e.printStackTrace();
}
System.out.println(date);
//runtimeException 运行期异常
int[] arr= {1,2,3};
System.out.println(arr[0]);
//有问题,但是不会提示,因为能够编译通过,在执行的时候才会有异常
try {
System.out.println(arr[2]);
}catch(Exception e) {
System.out.println("第一句测试");
System.out.println(e);
}
//error 错误
int[] arr2=new int[1024*1024*1024*1024*1024*1024];
System.out.println("后续代码");
}
}
抛出异常
package demo2;
public class Throw {
public static void main(String[] args) {
int[] arr=null;
int e=getElement(arr,3);
System.out.println(e);
}
private static int getElement(int[] arr, int index) {
if(arr==null) {
throw new NullPointerException(“传递的数组值是NULL”);
}
if(index<0||index>arr.length-1) {
throw new ArrayIndexOutOfBoundsException(“传递的索引超出了数组的正常使范围”);
}
int ele=arr[index];
return 0;
}
}
package demo2;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Throws {
public static void main(String[] args)throws Exception {
readFile(“c:\a”);
System.out.println(“后续代码”);
}
private static void readFile(String fileName) throws FileNotFoundException,IOException {
if(!fileName.equals(“c:\a.txt”)) {
throw new FileNotFoundException(“传递的文件不是c:\a.txt”);
}
if(!fileName.endsWith(".txt")) {
throw new IOException("文件后缀名有无");
}
System.out.println("文件正确");
}
}