字符串相关的类
String类
public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
说明:1.String是一个final类,不可被继承,代表不可变的字符序列。
2.String实现了Serializable接口,表示字符串是支持序列化的。
3.String实现了Comparable接口,表示字符串是可以比较大小的。
4.String内部定义了final value[]用于存储字符串数据。
String 对象的创建
String str = "hello";
// 本质上this.value = new char[0]
String s1 = new String();
//this.value = original.value
String s2 = new String(String original);
//this.value = Arrays.copyOf(value, value.length)
String s3 = new String(char[] a);
String s4 = new String(char[] a,int startIndex,int count);
- String s1 = “abc”; String s2 = “abc”;s1 = “hello”;内存状态解析:
- String str1 = “abc”; 与String str2 = new String(“abc”);的区别?
说明:1.字符串常量存储在字符串常量池,目的是共享。
2.字符串非常量对象存储在堆中。
- Person p1 = new Person(“张三”,12); Person p2 = new Person(“张三”,12); 问System.out.println(p1.name == p2.name);结果是什么?true
String s1 = "你好,";
String s2 = "Tom";
String s3 = "你好," + "Tom";
String s4 = s1 + "Tom";
String s5 = s1 + s2;
String s6 = (s1 + s2).intern();
System.out.println(s3 == s4);//false
System.out.println(s3 == s5);//false
System.out.println(s4 == s5);//false
System.out.println(s3 == s6);//true
说明:1.常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量。
2.只要其中有一个是变量,结果就在堆中。
3.如果拼接的结果调用intern()方法,返回值就在常量池中。
- 面试题:下面程序运行的结果?
public class StringLen {
String str = new String("good");
char[] ch = { 't', 'e', 's', 't' };
public void change(String str, char ch[]) {
str = "test ok";
ch[0] = 'b';
}
public static void main(String[] args) {
StringLen ex = new StringLen();
ex.change(ex.str, ex.ch);
System.out.println(ex.str);//good
System.out.println(ex.ch);//best
}
}
常用方法
- int length() :返回字符串的长度: return value.length。
- char charAt(int index): 返回某索引处的字符return value[index]。
- boolean isEmpty() :判断是否是空字符串:return value.length == 0。
- String toLowerCase() :使用默认语言环境,将 String 中的所有字符转换为小写。
- String toUpperCase() :使用默认语言环境,将 String 中的所有字符转换为大写。
- String trim():返回字符串的副本,忽略前导空白和尾部空白。
- boolean equals(Object obj):比较字符串的内容是否相同。
- boolean equalsIgnoreCase(String anotherString) :与equals方法类似,忽略大小写。
- String concat(String str) :将指定字符串连接到此字符串的结尾。 等价于用“+”。
- int compareTo(String anotherString):比较两个字符串的大小。
- String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。
- String substring(int beginIndex, int endIndex):返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。
- boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束。
- boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始。
- boolean startsWith(String prefix, int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
- boolean contains(CharSequence s) :当且仅当此字符串包含指定的 char 值序列时,返回 true。
- int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引。
- int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
- int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引。
- int lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
- String replace(char oldChar, char newChar):返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
- String replace(CharSequence target, CharSequence replacement):使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
- String replaceAll(String regex, String replacement): 使用给定的replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
- String replaceFirst(String regex, String replacement) : 使用给定的replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
- boolean matches(String regex):告知此字符串是否匹配给定的正则表达式。
- String[] split(String regex):根据给定正则表达式的匹配拆分此字符串。
- String[] split(String regex, int limit):根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中。
String与字符数组的转换
- 字符数组转换成String:String 类的构造器String(char[]) 和 String(char[] ,int offset ,int length) 分别用字符数组中的全部字符和部分字符创建字符串对象。
- 字符串转换成字符数组:public char[] toCharArray()将字符串中的全部字符存放在一个字符数组中的方法;public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)提供了将指定索引范围内的字符串存放到数组中的方法。
String与字节数组的转换
- 字节数组转换成字符串:String(byte[])通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String;String(byte[] ,int offset ,int length) 用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一个字符串对象。
- 字符串转换成字节数组:public byte[] getBytes() 使用平台的默认字符集将此 String 编码为byte 序列,并将结果存储到一个新的 byte 数组中;public byte[] getBytes(String charsetName) 使用指定的字符集将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。
StringBuffer类
abstract class AbstractStringBuilder implements Appendable, CharSequence {
/**
* The value is used for character storage.
* value没有final,可以不断扩容;如果要添加的数据底层数组长度不够,那就需要扩容底层的数组,默认情况下,扩容为原来容量的两倍+2,同时将原有数组中的元素复制到新的数组中。
*/
char[] value;
/**
* The count is the number of characters used.
* count记录有效字符的个数
*/
int count;
说明:1. java.lang.StringBuffer代表可变的字符序列,JDK1.0中声明,可以对字符串内容进行增删,此时不会产生新的对象。
2.很多方法与String相同。
3.作为参数传递时,方法内部可以改变值。
4.线程安全,但效率低。
5.StringBuffer类不同于String,其对象必须使用构造器生成。有三个构造器:
1)StringBuffer():初始容量为16的字符串缓冲区。
//char[] value = new char[16]
StringBuffer sb1 = new StringBuffer();
System.out.println(sb1.length());//0
//value[0] = 'a'
sb1.append('a');
2)StringBuffer(int size):构造指定容量的字符串缓冲区。
3)StringBuffer(String str):将内容初始化为指定字符串内容。
//char[] value = new char["abc".length() + 16]
StringBuffer sb2 = new StringBuffer("abc");
System.out.println(sb2.length());//3
常用方法
- StringBuffer append(xxx):提供了很多的append()方法,用于进行字符串拼接。
- StringBuffer delete(int start,int end):删除指定位置的内容。
- StringBuffer replace(int start, int end, String str):把[start,end)位置替换为str。
- StringBuffer insert(int offset, xxx):在指定位置插入xxx。
- StringBuffer reverse() :把当前字符序列逆转。
StringBuilder类
StringBuilder 和 StringBuffer非常类似,均代表可变的字符序列,而且提供相关功能的方法也一样,但StringBuilder线程不安全,效率高。
面试题:String、StringBuffer和StringBuilder 三者的异同?
- String(JDK1.0):不可变字符序列。
- StringBuffer(JDK1.0):可变字符序列、效率低、线程安全。
- StringBuilder(JDK 5.0):可变字符序列、效率高、线程不安全。
- 作为参数传递的话,方法内部String不会改变其值,StringBuffer和StringBuilder会改变其值。
日期时间API
java.lang.System类
System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
java.util.Date类
- 两个构造器的使用
1)Date():创建一个对应当前时间的Date对象。
2)Date(long date):创建指定毫秒数的Date对象。 - 两个方法的使用
1)toString():显示当前的年、月、日、时、分、秒。
2)getTime():返回自1970年1月1日00:00:00 GMT以来此 Date 对象表示的毫秒数(也成时间戳)。
java.sql.Date类
对应着数据库中的日期类型的变量。
1)如何实例化?
java.sql.Date date = new java.sql.Date(2255688455L);
System.out.println(date);//1970-01-27
2)如何将java.util.Date对象转换成java.sql.Date对象?
Date date1 = new Date();
java.sql.Date date2 = new java.sql.Date(date1.getTime());
System.out.println(date2);
java.text.SimpleDateFormat类
是一个不与语言环境有关的方式来格式化和解析日期的具体类。
- 格式化
1)SimpleDateFormat() :默认的模式和语言环境创建对象。
2)public SimpleDateFormat(String pattern) :该构造方法可以用参数pattern指定的格式创建一个对象。
3) public String format(Date date) :方法格式化时间对象date。 - 解析
1) public Date parse(String source):从给定字符串的开始解析文本,以生成一个日期。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
String format = simpleDateFormat.format(new Date());
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format1 = simpleDateFormat1.format(new Date());
Date parse = simpleDateFormat.parse("21-2-1 上午9:18");
//java.text.ParseException: Unparseable date: "21-2-1 上午9:18"
//Date parse1 = simpleDateFormat1.parse("21-2-1 上午9:18");
Date parse2 = simpleDateFormat1.parse("2021-02-01 09:18:31");
System.out.println(format);//21-2-1 上午9:18
System.out.println(format1);//2021-02-01 09:18:31
System.out.println(parse);//Mon Feb 01 09:18:00 CST 2021
System.out.println(parse2);//Mon Feb 01 09:18:31 CST 2021
java.util.Calendar( 日历)类
Calendar是一个抽象基类,主用用于完成日期字段之间相互操作的功能。
- 实例化
1)使用Calendar.getInstance()方法。
2)调用它的子类GregorianCalendar的构造器。 - 方法
1)public int get(int field)。
2)public void set(int field,int value)。
3)public void add(int field,int amount)。
4)public final Date getTime()。
5)public final void setTime(Date date)。
Calendar calendar = Calendar.getInstance();
int i = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(i);//周一是2
calendar.set(Calendar.DAY_OF_MONTH,3);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//3
calendar.add(Calendar.DAY_OF_MONTH,1);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//4
Date time = calendar.getTime();
System.out.println(time);//Thu Feb 04 09:40:16 CST 2021
说明:1.获取月份时:一月是0,二月是1,...,12月是11。
2.获取星期时:周日是1,周一是2 ,...,周六是7。
以上日期时间API存在的问题
- 可变性:像日期和时间这样的类应该是不可变的。
- 偏移性:Date中的年份是从1900开始的,而月份都从0开始。
- 格式化:格式化只对Date有用,Calendar则不行。
- 它们也不是线程安全的,不能处理闰秒。
LocalDate、LocalTime、LocalDateTime 类
它们的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。
LocalDate now = LocalDate.now();
System.out.println(now);//2021-02-01
LocalTime now2 = LocalTime.now();
System.out.println(now2);//16:01:51.123
LocalDateTime now1 = LocalDateTime.now();
System.out.println(now1);//2021-02-01T16:00:45.818
int year = now.getYear();
Month month = now.getMonth();
int dayOfMonth = now.getDayOfMonth();
System.out.println(year + " " + month + " " + dayOfMonth);//2021 FEBRUARY 1
LocalDate withDayOfMonth = now.withDayOfMonth(2);
System.out.println(now + " " + withDayOfMonth);//2021-02-01 2021-02-02
//获取本月的第一天
LocalDate with = now.with(TemporalAdjusters.firstDayOfMonth());
System.out.println(with);//2021-02-01
//获取本月的最后一天
LocalDate with1 = now.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(with1);//2021-02-28
LocalDate localDate = now.plusDays(2);
System.out.println(localDate);//2021-02-03
LocalDate localDate1 = now.minusDays(3);
System.out.println(localDate1);//2021-01-29
格式化与解析日期或时间
java.time.format.DateTimeFormatter 类:该类提供了三种格式化方法:
- 预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;
ISO_LOCAL_TIME。 - 本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG)。
- 自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)。
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String format2 = localDateTime.format(dateTimeFormatter);
System.out.println(format2);//2021-02-01 04:12:06
瞬时:Instant
Instant:时间线上的一个瞬时点。 这可能被用来记录应用程序中的事件时间戳。且不需要任何上下文信息,例如,时区。概念上讲,它只是简单的表示自1970年1月1日0时0分0秒(UTC)开始的秒数。因为java.time包是基于纳秒计算的,所以Instant的精度可以达到纳秒级。
其他API
- ZoneId :该类中包含了所有的时区信息,一个时区的ID,如 Europe/Paris。
- ZonedDateTime :一个在ISO-8601日历系统时区的日期时间,如 2007-12-
03T10:15:30+01:00 Europe/Paris。 - Clock:使用时区提供对当前即时、日期和时间的访问的时钟。
- 持续时间:Duration,用于计算两个“时间”间隔。
- 日期间隔:Period,用于计算两个“日期”间隔。
- TemporalAdjuster:时间校正器。有时我们可能需要获取例如:将日期调整到“下一个工作日”等操作。
- TemporalAdjusters:该类通过静态方法(firstDayOfXxx()/lastDayOfXxx()/nextXxx())提供了大量的常用TemporalAdjuster 的实现。
Java比较器
实现对象排序的方式有两种:
- 自然排序:java.lang.Comparable
- 定制排序:java.util.Comparator
自然排序:java.lang.Comparable
- Comparable接口强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序。
- 实现 Comparable 的类必须实现 compareTo(Object obj) 方法,两个对象即通过 compareTo(Object obj) 方法的返回值来比较大小。如果当前对象this大于形参对象obj,则返回正整数,如果当前对象this小于形参对象obj,则返回负整数,如果当前对象this等于形参对象obj,则返回零。
- 实现Comparable接口的对象列表(和数组)可以通过 Collections.sort 或Arrays.sort进行自动排序。实现此接口的对象可以用作有序映射中的键或有序集合中的元素,无需指定比较器。
- Comparable 的典型实现:(默认都是从小到大排列的)
1)String:按照字符串中字符的Unicode值进行比较。
2)Character:按照字符的Unicode值来进行比较。
3)数值类型对应的包装类以及BigInteger、BigDecimal:按照它们对应的数值大小进行比较。
4)Boolean:true 对应的包装类实例大于 false 对应的包装类实例。
5)Date、Time等:后面的日期时间比前面的日期时间大。
Ps:(快速记忆法)当前对象与后一个对象进行比较,如果比较结果为1进行交换,其他不进行交换。
当后一个对象比当前对象大,返回结果值为1时,前后交换,说明是倒序排列。
当后一个对象比当前对象小,返回结果值为1时,前后交换,说明是升序排列。 - 实例:
class Goods implements Comparable {
private String name;
private double price;
public Goods(String s, int i) {
this.name = s;
this.price = i;
}
//按照价格,比较商品的大小
@Override
public int compareTo(Object o) {
if(o instanceof Goods) {
Goods other = (Goods) o;
if (this.price > other.price) {
return 1;
} else if (this.price < other.price) {
return -1;
}
return 0;
}
throw new RuntimeException("输入的数据类型不一致");
}
@Override
public String toString() {
return "Goods{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
public class ComparableLen {
public static void main(String[] args) {
Goods[] all = new Goods[4];
all[0] = new Goods("《红楼梦》", 100);
all[1] = new Goods("《西游记》", 80);
all[2] = new Goods("《三国演义》", 140);
all[3] = new Goods("《水浒传》", 120);
Arrays.sort(all);
System.out.println(Arrays.toString(all));
}
}
定制排序:java.util.Comparator
- 重写compare(Object o1,Object o2)方法,比较o1和o2的大小: 如果方法返回正整数,则表示o1 大于o2 ;如果返回0 ,表示相等;返回负整数,表示o1 小于o2。
- 可以将 Comparator 传递给 sort 方法(如 Collections.sort 或 Arrays.sort),从而允许在排序顺序上实现精确控制。
- 实例:
public class ComparableLen {
public static void main(String[] args) {
Goods[] all = new Goods[4];
all[0] = new Goods("《红楼梦》", 100);
all[1] = new Goods("《西游记》", 80);
all[2] = new Goods("《三国演义》", 140);
all[3] = new Goods("《水浒传》", 120);
Arrays.sort(all, new Comparator<Goods>() {
@Override
public int compare(Goods o1, Goods o2) {
//自定义排序逻辑...
return 0;
}
});
System.out.println(Arrays.toString(all));
}
}
System类
-
System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。该类位于java.lang包。
-
由于该类的构造器是private的,所以无法创建该类的对象,也就是无法实例化该类。其内部的成员变量和成员方法都是static的,所以也可以很方便的进行调用。
-
成员变量:
1)System类内部包含in、out和err三个成员变量,分别代表标准输入流(键盘输入),标准输出流(显示器)和标准错误输出流(显示器)。 -
成员方法:
1)native long currentTimeMillis():作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。
2)void exit(int status):作用是退出程序。其中status的值为0代表正常退出,非零代表异常退出。
3)void gc():作用是请求系统进行垃圾回收。至于系统是否立刻回收,则取决于系统中垃圾回收算法的实现以及系统执行时的情况。
4)String getProperty(String key):作用是获得系统中属性名为key的属性对应的值。
Math类
java.lang.Math 提供了一系列静态方法用于科学计算。其方法的参数和返回值类型一般为double型。
BigInteger与BigDecimal
BigInteger类
- java.math包的BigInteger 可以表示不可变的任意精度的整数。BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。
- 构造器:BigInteger(String val):根据字符串构建BigInteger对象。
BigDecimal类
- BigDecimal类支持不可变的、任意精度的有符号十进制定点数。
- 构造器:
1) public BigDecimal(double val)
2)public BigDecimal(String val)
BigInteger bi = new BigInteger("1243324112355896852142635");
BigDecimal bd = new BigDecimal("12435.351");
BigDecimal bd2 = new BigDecimal("11");
System.out.println(bi);//1243324112355896852142635
System.out.println(bd.divide(bd2, BigDecimal.ROUND_HALF_UP));//1130.486
System.out.println(bd.divide(bd2, 15, BigDecimal.ROUND_HALF_UP));//1130.486454545454545