3.4常用类
3.41String类
1, String : 不可变长的字符序列 String类表示字符串。 Java程序中的所有字符串文字(例如"abc" )都实现为此类的实例。 2, StringBuilder : 可变长的字符序列, 字符串缓冲区支持可变字符串 3, StringBuffer : 可变长的字符序列;
public class Class001_String { public static void main(String[] args) throws UnsupportedEncodingException { //String() 初始化新创建的 String对象,使其表示空字符序列。 String str1 = new String(); System.out.println(str1); //String(String original) String s = "nihao"; //1个对象 "nihao" ->字符串常量池中 String str2 = new String("nihao"); ///1个对象 new ->堆中 System.out.println(str2); //String(char[] value) String str3 = new String(new char[]{'q','w','e','r','d','f'}); System.out.println(str3); //String(char[] value, int offset, int count) String str4 = new String(new char[]{'q','w','e','r','d','f'},2,3); System.out.println(str4); //String(byte[] bytes) 通过使用平台的默认字符集解码指定的字节数组构造新的 String 。 byte[] arr = "你好".getBytes("gbk"); //String str5 = new String(arr); //在utf-8一个中文3个字节 在gbk一个中文2个字节 //String(byte[] bytes, String charsetName) String str5 = new String(arr,"gbk"); System.out.println(str5); System.out.println(arr.length); //String(byte[] bytes, int offset, int length) //String(byte[] bytes, int offset, int length, String charsetName) String str6 = new String(arr,2,2,"gbk"); System.out.println(str6); } }
3.4.2String常用方法
public class Class002_String { public static void main(String[] args) { String str = "jintiantianqihenqinglang"; String str2 = "Jintiantianqihenqinglang"; //char charAt(int index) 返回指定索引处的 char值。 *** System.out.println(str.charAt(5)); //int codePointAt(int index) 返回指定索引处的字符(Unicode代码点)。 System.out.println(str.codePointAt(5)); /* int compareTo(String anotherString) 按字典顺序比较两个字符串。 返回值: this,anotherString 0 --> this == anotherString <0 ---> this < anotherString >0 ---> this > anotherString */ System.out.println(str.compareTo(str2)); //int compareToIgnoreCase(String str) 按字典顺序比较两个字符串,忽略大小写差异。 System.out.println(str.compareToIgnoreCase(str2)); //String concat(String str) 将指定的字符串连接到此字符串的末尾。 System.out.println(str.concat("haha")); System.out.println(str); //boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时,才返回true。 *** System.out.println(str.contains("tian")); //static String copyValueOf(char[] data) 相当于 valueOf(char[]) 。 //static String copyValueOf(char[] data, int offset, int count) 相当于 valueOf(char[], int, int) 。 String s = String.copyValueOf(new char[]{'a','b','c','d'}); System.out.println(s); //boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结尾。 *** //boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开头。 System.out.println(str.startsWith("j")); //boolean equals(Object anObject) 将此字符串与指定的对象进行比较。 ***** //boolean equalsIgnoreCase(String anotherString) 将此 String与另一个 String比较,忽略了大小写。 System.out.println(str.equals(str2)); System.out.println(str.equalsIgnoreCase(str2)); //byte[] getBytes() 使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中。 //byte[] getBytes(String charsetName) 使用命名的字符集将此 String编码为字节序列,将结果存储到新的字节数组中。 //void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将此字符串中的字符复制到目标字符数组中。 char[] arr = new char[10]; str.getChars(3,6,arr,1); System.out.println(Arrays.toString(arr)); //int indexOf(String str) 返回指定子字符串第一次出现的字符串中的索引。 **** //int indexOf(String str, int fromIndex) 从指定的索引处开始,返回指定子字符串第一次出现的字符串中的索引。 //int lastIndexOf(String str) 返回指定子字符串最后一次出现的字符串中的索引。 //int lastIndexOf(String str, int fromIndex) 返回指定子字符串最后一次出现的字符串中的索引,从指定索引开始向后搜索。 System.out.println(str.indexOf("a")); System.out.println(str.indexOf("a",6)); //String intern() 返回字符串对象的规范表示。 String s1 = new String("abc"); String s2 = new String("abc"); String s3 = "haha"; String s4 = "haha"; System.out.println(s1==s2); System.out.println(s3==s4); System.out.println(s1.intern() == s2.intern()); //boolean isBlank() 如果字符串为空或仅包含 white space代码点,则返回 true ,否则 false 。 System.out.println("".isBlank()); System.out.println(" ".isBlank()); System.out.println(" sf ".isBlank()); //int length() 返回此字符串的长度。 ***** //String repeat(int count) 返回一个字符串,其值为此字符串的串联重复 count次。 System.out.println("xixi".repeat(3)); //String replace(CharSequence target, CharSequence replacement) *** //String replaceFirst(String regex, String replacement) System.out.println(str.replace("a","A")); System.out.println(str.replaceFirst("q","Q")); //String[] split(String regex) 将此字符串拆分为给定 regular expression的匹配 项 。 ***** System.out.println(Arrays.toString(str.split("a"))); //String strip() 返回一个字符串,其值为此字符串,并删除了所有前导和尾随 white space 。 去除全角空格,去除半角空格 //String stripLeading() 返回一个字符串,其值为此字符串,并删除了所有前导 white space 。 //String stripTrailing() 返回一个字符串,其值为此字符串,并删除所有尾随 white space 。 //String trim() 返回一个字符串,其值为此字符串,删除了所有前导和尾随空格,不能去除全角空格,只能去除半角空格 *** System.out.println(" 123haha ".trim()); System.out.println(" 123haha ".strip()); System.out.println(" 123haha ".stripLeading()); System.out.println(" 123haha ".stripTrailing()); //String substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串。 ***** //String substring(int beginIndex, int endIndex) 返回一个字符串,该字符串是此字符串的子字符串。 System.out.println(str.substring(5)); System.out.println(str.substring(5,8)); //char[] toCharArray() 将此字符串转换为新的字符数组。 ***** System.out.println(Arrays.toString(str.toCharArray())); //String toLowerCase() 使用默认语言环境的规则将此 String所有字符转换为小写。 *** //String toUpperCase() 使用默认语言环境的规则将此 String所有字符转换为大写。 *** System.out.println(str.toUpperCase()); System.out.println(str2.toLowerCase()); //static String valueOf(boolean b) 返回 boolean参数的字符串表示形式。 System.out.println(String.valueOf(false)); } }
3.4.3StringBuilder与StringBuffer
1, String : 不可变的字符序列 private final byte[] value; StringBuilder : 可变的字符序列,线程不安全|不同步 建议使用StringBuilder代替StringBuffer StringBuffer : 可变的字符序列,线程安全的|同步 在多线程环境下保证数据的安全,建议使用StringBuffer
执行效率: StringBuilder > StringBuffer > String StringBuilder|StringBuffer : byte[] value; --> 默认初始容量16,每次扩容原容量的2倍+2,使用 Arrays.copyOf方法进行扩容 int newCapacity = (oldCapacity << 1) + 2;
public class Class003_StringBuilder { public static void main(String[] args) { //StringBuilder() 构造一个字符串构建器,其中不包含任何字符,初始容量为16个字符。 StringBuilder sb = new StringBuilder(); System.out.println(sb); //StringBuilder(String str) 容量为seq的长度+16 StringBuilder sb2 = new StringBuilder("ABC"); System.out.println(sb2); System.out.println(sb.capacity()); System.out.println(sb2.capacity()); //StringBuilder append(boolean b) 将 boolean参数的字符串表示形式追加到序列中。 sb.append(123); System.out.println(sb.append(false));; System.out.println(sb.append("nihaoaac"));; System.out.println(sb.append("a"));; System.out.println(sb); System.out.println(sb.capacity()); System.out.println(sb.length()); //StringBuilder delete(int start, int end) 删除此序列的子字符串中的字符。 System.out.println(sb.delete(0,3)); System.out.println(sb); //StringBuilder insert(int offset, double d) 将 double参数的字符串表示形式插入此序列中。 System.out.println(sb.insert(5,4.4)); System.out.println(sb); //StringBuilder reverse() 导致此字符序列被序列的反向替换。 System.out.println(sb.reverse()); //StringBuilder|StringBuffer --> String //1) toString() //2) new String(StringBuilder|StringBuffer) System.out.println(sb.toString()); System.out.println(new String(sb)); //String-->StringBuilder|StringBuffer //new StringBuilder|StringBuffer(String str) } }
3.4.4包装类
1,基本数据类型的包装类 基本 引用 byte Byte short Short int Integer long Long char Character boolean Boolean double Double float Float
自动拆装箱: jdk1.5 自动装箱 : 基本--->引用 自动拆箱 : 引用--->基本 问: 有了基本数据类型为什么还需要包装类? 1.包装类中提供了一些成员方法,更强大 2.类似容器中只能存储引用数据类型数据,需要包装类 3.基本数据类型与引用数据类型默认值不同 为什么有了引用数据类型还有基本数据类型? 有利于节省内存 使用更简单方便
总结 : int,Integer,new Integer数据比较问题: 要求数据值相等考虑类型,地址等问题 1)两个new 比较,肯定不相等,堆内存中地址不同 2)两个int 比较,值相等就相等 3)int与Integer比较,无论是否通过new,只要数据值相等就像等,因为会先自动拆箱成基本数据类型比较 4)一个Integer,一个new Integer肯定不相等,new 就是堆内存地址,地址肯定不同 5)两个Integer比较,看数值的范围是否在[-128,127]之间,在之间相等,不在之间返回new Integer不相等
public class Class001_Integer { public static void main(String[] args) { int i = 1; //基本 //自动装箱 Integer in = i; //Integer.valueOf(i) //自动拆箱 int i2 = in; //in.intValue() test(1.1,2.2); //自动装箱 int num1 = 127; int num2 = 127; Integer num3 = 127; Integer num4 = 127; Integer num5 = new Integer(127); Integer num6 = new Integer(127); Integer num7 = 128; Integer num8 = 128; System.out.println(num1==num2); //true System.out.println(num3==num4); //true System.out.println(num5==num6); //false System.out.println(num7==num8); //false System.out.println(num1==num3); //true System.out.println(num5==num3); //false Double.valueOf(127); //存在Integer缓存对象,能够表示的数值范围 [-128,127],在其范围内返回缓冲区对象,不在范围内返回new Integer } public static void test(Double d1,Double d2){ System.out.println(d1+d2); //自动拆箱 } }
3.4.5枚举类型
1,枚举类型 : 一种事物的所有可能,一种类型的所有实例 1.enum定义枚举 2.枚举类中的字段都作为当前枚举类型的实例存在,默认public static final修饰 3.枚举类中可以根据需求定义成员,成员变量,成员方法,构造器... 4.枚举类型中的构造器默认私有的 5.所有的枚举类型默认隐式的继承自java.lang.Enum类型
public class Class01_Enum { public static void main(String[] args) { WDay day1 = WDay.day1; WeekDay sun = WeekDay.SUN; WeekDay thus = WeekDay.THUS; sun.setName("周六"); System.out.println(sun.getName()); System.out.println(WeekDay.MON.getName()); //String name() 返回此枚举常量的名称,与其枚举声明中声明的完全相同。 System.out.println(sun.name()); //int ordinal() 返回此枚举常量的序数(它在枚举声明中的位置,其中初始常量的序数为零)。 System.out.println(thus.ordinal()); //values() 获取枚举类型的所有实例 System.out.println(Arrays.toString(sun.values())); //switch() -> byte short int char String(jdk1.7) 枚举(jdk1.5) switch (thus){ case MON: System.out.println("今天星期一,心情还不错"); break; case SUN: System.out.println("星期二也还可以"); break; case THUS: System.out.println("星期三自习课挺好"); break; } } } //创建一个枚举类型 enum WeekDay{ MON("星期一"),THUS("星期二"),SUN("星期三"); private String name; WeekDay() { } WeekDay(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class WDay{ public static final WDay day1 = new WDay(); public static final WDay day2 = new WDay(); public WDay() { } }
3.4.6Math类
1,Math Math包含用于执行基本数字运算的方法,例如基本指数,对数,平方根和三角函数。 静态工厂
public class Class01_Math { public static void main(String[] args) { //static double random() 返回带有正号的 double值,大于或等于 0.0且小于 1.0 。 //[0.0,1.0) 随机小数 //随机整数 : [min,max) (int)(Math.random()*(max-min)+min) //随机整数 : [min,max] (int)(Math.random()*(max-min+1)+min) //[3,9] System.out.println((int)(Math.random()*(9-3+1)+3)); System.out.println((int)(Math.random()*(9-3+1)+3)); System.out.println((int)(Math.random()*(9-3+1)+3)); System.out.println((int)(Math.random()*(9-3+1)+3)); //static double abs(double a) 返回 double值的绝对值。 System.out.println(Math.abs(1.55)); //static double ceil(double a) 返回大于或等于参数且等于数学整数的最小值(最接近负无穷大) double 。 System.out.println(Math.ceil(1.69)); System.out.println(Math.ceil(-1.53)); //static double floor(double a) 返回小于或等于参数且等于数学整数的最大值(最接近正无穷大) double 。 System.out.println(Math.floor(1.12)); System.out.println(Math.floor(-1.43)); //static int max(int a, int b) 返回两个 int值中较大的 int 。 System.out.println(Math.max(1.22,1.56)); //static int min(int a, int b) 返回两个 int值中较小的 int 。 System.out.println(Math.min(1.08,1.10)); //static double pow(double a, double b) 返回第一个参数的值,该值是第二个参数的幂。 System.out.println(Math.pow(3,3)); //static long round(double a) 返回与参数最接近的 long ,并将关系四舍五入为正无穷大。 System.out.println(Math.round(1.89)); System.out.println(Math.round(2.10)); System.out.println(Math.round(3.14)); //static double sqrt(double a) 返回 double值的正确舍入正平方根。 System.out.println(Math.sqrt(16)); } }
3.4.7Date日期类
1,Date : Date类表示特定的时刻,精度为毫秒,在java.util包下的date,表示一个时间日期精确到ms数
public class Class01_Date { public static void main(String[] args) { //1.Date() //创建一个Date日期对象 Date date1 = new Date(); System.out.println(date1); //获得时间 System.out.println(date1.getTime()); //2.Date(long date) 参数为ms数 标准基准时间(称为“纪元”)以来的指定毫秒数,即1970年1月1日00:00:00 GMT。 System.out.println(new Date(1637062443778l)); Date date2 = new Date(); //boolean after(Date when) 测试此日期是否在指定日期之后。 System.out.println(date1.after(date2)); //boolean before(Date when) 测试此日期是否在指定日期之前。 System.out.println(date1.before(date2)); } }
3.4.8日期格式转换类
1,SimpleDateFormat 日期格式转换类
java8新增: LocalDate 日期
LocalTime 时间
LocalDateTime 年月日时分秒
public class Class01_Data1 { public static void main(String[] args) throws ParseException { //创建对象 Date date = new Date(); //转换器-->默认模板 SimpleDateFormat simple = new SimpleDateFormat(); //Date--->String System.out.println(simple.format(date));//2021/11/16 下午8:37 //转换器--> 指定模板 SimpleDateFormat simple1 = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ms"); System.out.println(simple1.format(date)); //2021-11-16 17:39:24 816 //String--->Date System.out.println(simple1.parse("2021-11-17-08-24-2411")); } }
3.4.9File类
1,File 文件或者路径抽象表现形式
public class Class01_File { public static void main(String[] args) throws IOException { //创建一个file对象 File file1 = new File("D://test.txt"); File file2 = new File("D://haha.txt"); File file3 = new File("test1.txt");//相对路径,默认相对于当前项目下 System.out.println(file1); System.out.println(file2); System.out.println(file3); //File(File parent, String child) File file4 = new File(file1,"src.txt"); System.out.println(file4); //boolean setReadOnly() 标记此抽象路径名指定的文件或目录,以便仅允许读取操作。 //System.out.println(file1.setReadOnly());; //boolean canWrite() 测试应用程序是否可以修改此抽象路径名表示的文件。 System.out.println(file1.canWrite()); //File(String parent, String child) File file5 = new File("D://CCC","test1.txt"); System.out.println(file5); //boolean createNewFile() 当且仅当具有此名称的文件尚不存在时,以原子方式创建由此抽象路径名命名的新空文件。 System.out.println(file1.createNewFile()); //boolean delete() 删除此抽象路径名表示的文件或目录。 System.out.println(file1.delete()); //boolean exists() 测试此抽象路径名表示的文件或目录是否存在。 System.out.println(file1.exists()); System.out.println(file2.exists()); //File getAbsoluteFile() 返回此抽象路径名的绝对形式。 System.out.println(file1.getAbsoluteFile()); System.out.println(file3.getAbsoluteFile()); //long getFreeSpace() 通过此抽象路径名返回分区 named中未分配的字节数。 System.out.println(new File("D:").getFreeSpace()); //long getTotalSpace() 通过此抽象路径名返回分区 named的大小。 System.out.println(new File("D:").getTotalSpace()); //String getName() 返回此抽象路径名表示的文件或目录的名称。 System.out.println(file1.getName()); //String getParent() 返回此抽象路径名父项的路径名字符串,如果此路径名未指定父目录,则返回 null 。 System.out.println(file1.getParent()); //File getParentFile() 返回此抽象路径名父项的抽象路径名,如果此路径名未指定父目录,则返回 null 。 System.out.println(file1.getParentFile()); //boolean isAbsolute() 测试此抽象路径名是否为绝对路径。 //boolean isDirectory() 测试此抽象路径名表示的文件是否为目录。 //boolean isFile() 测试此抽象路径名表示的文件是否为普通文件。 //boolean isHidden() 测试此抽象路径名指定的文件是否为隐藏文件。 System.out.println(file1.isFile()); System.out.println(file1.isDirectory()); //long lastModified() 返回上次修改此抽象路径名表示的文件的时间。 System.out.println(new SimpleDateFormat().format(new Date(file2.lastModified()))); } }