目录
1.String、StringBuffer、StringBuilder三者的对比
2.StringBuffer与StringBuilder的内存解析
3.对比String、StringBuffer、 StringBuilder三者的执行效率
4.StringBuffer、StringBuilder中的常用方法
1.获取系统当前时间:System类中的currentTimeMillis()
2. java.util.Date类与java.sql.Date类
3. java.text.SimpleDataFormat类
4.本地日期、本地时间、本地日期时间的使用:LocalDate/LocalTime/LocalDateTime
一、String类
1.概述
2.String的不可变性
2.1 说明
2.2 代码举例
String s1 = "abc";//字面量的定义方式
String s2 = "abc";
s1 = "hello";
System.out.println(s1 == s2);//比较s1和s2的地址值
System.out.println(s1);//hello
System.out.println(s2);//abc
System.out.println("*****************");
String s3 = "abc";
s3 += "def";
System.out.println(s3);//abcdef
System.out.println(s2);
System.out.println("*****************");
String s4 = "abc";
String s5 = s4.replace('a', 'm');
System.out.println(s4);//abc
System.out.println(s5);//mbc
2.3 图示
3.String实例化的不同方式
3.1 方式说明
3.2 代码举例
/通过字面量定义的方式:此时的s1和s2的数据javaEE声明在方法区中的字符串常量池中。
String s1 = "javaEE";
String s2 = "javaEE";
//通过new + 构造器的方式:此时的s3和s4保存的地址值,是数据在堆空间中开辟空间以后对应的地址值。
String s3 = new String("javaEE");
String s4 = new String("javaEE");
System.out.println(s1 == s2);//true
System.out.println(s1 == s3);//false
System.out.println(s1 == s4);//false
System.out.println(s3 == s4);//false
3.3 面试题
3.4 图示
4. 字符串拼接方式赋值的对比
4.1 说明
4.2 代码举例
String s1 = "javaEE";
String s2 = "hadoop";
String s3 = "javaEEhadoop";
String s4 = "javaEE" + "hadoop";
String s5 = s1 + "hadoop";
String s6 = "javaEE" + s2;
String s7 = s1 + s2;
System.out.println(s3 == s4);//true
System.out.println(s3 == s5);//false
System.out.println(s3 == s6);//false
System.out.println(s3 == s7);//false
System.out.println(s5 == s6);//false
System.out.println(s5 == s7);//false
System.out.println(s6 == s7);//false
String s8 = s6.intern();//返回值得到的s8使用的常量池中已经存在的“javaEEhadoop”
System.out.println(s3 == s8);//true
****************************
String s1 = "javaEEhadoop";
String s2 = "javaEE";
String s3 = s2 + "hadoop";
System.out.println(s1 == s3);//false
final String s4 = "javaEE";//s4:常量
String s5 = s4 + "hadoop";
System.out.println(s1 == s5);//true
5.常用方法
6. String与其它结构的转换
6.1 与基本数据类型、包装类之间的转换
@Test
public void test1(){
String str1 = "123";
// int num = (int)str1;//错误的
int num = Integer.parseInt(str1);
String str2 = String.valueOf(num);//"123"
String str3 = num + "";
System.out.println(str1 == str3);
}
6.2 与字符数组之间的转换
@Test
public void test2(){
String str1 = "abc123"; //题目: a21cb3
char[] charArray = str1.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.println(charArray[i]);
}
char[] arr = new char[]{'h','e','l','l','o'};
String str2 = new String(arr);
System.out.println(str2);
}
6.3 与字节数组之间的转换
@Test
public void test3() throws UnsupportedEncodingException {
String str1 = "abc123中国";
byte[] bytes = str1.getBytes();//使用默认的字符集,进行编码。
System.out.println(Arrays.toString(bytes));
byte[] gbks = str1.getBytes("gbk");//使用gbk字符集进行编码。
System.out.println(Arrays.toString(gbks));
System.out.println("******************");
String str2 = new String(bytes);//使用默认的字符集,进行解码。
System.out.println(str2);
String str3 = new String(gbks);
System.out.println(str3);//出现乱码。原因:编码集和解码集不一致!
String str4 = new String(gbks, "gbk");
System.out.println(str4);//没出现乱码。原因:编码集和解码集一致!
}
6.4 与StringBuffer、StringBuilder之间的转换
7. JVM中字符串常量池存放位置说明:
8. 常见算法题目的考查:
二、StringBuffer、StringBuilder
1.String、StringBuffer、StringBuilder三者的对比
2.StringBuffer与StringBuilder的内存解析
以StringBuffer为例:
String str = new String();//char[] value = new char[0];
String str1 = new String("abc");//char[] value = new char[]{'a','b','c'};
StringBuffer sb1 = new StringBuffer();//char[] value = new char[16];底层创建了一个长度是16的数组。
System.out.println(sb1.length());//
sb1.append('a');//value[0] = 'a';
sb1.append('b');//value[1] = 'b';
StringBuffer sb2 = new StringBuffer("abc");//char[] value = new char["abc".length() + 16];
//问题1. System.out.println(sb2.length());//3
//问题2. 扩容问题:如果要添加的数据底层数组盛不下了,那就需要扩容底层的数组。
默认情况下,扩容为原来容量的2倍 + 2,同时将原数组中的元素复制到新的数组中。
指导意义:开发中建议大家使用:StringBuffer(int capacity) 或 StringBuilder(int capacity)
3.对比String、StringBuffer、 StringBuilder三者的执行效率
4.StringBuffer、StringBuilder中的常用方法
三、JDK 8之前日期时间API
1.获取系统当前时间:System类中的currentTimeMillis()
long time = System.currentTimeMillis();
//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。
//称为时间戳
System.out.println(time);
2. java.util.Date类与java.sql.Date类
/*
java.util.Date类
|---java.sql.Date类
1.两个构造器的使用
>构造器一:Date():创建一个对应当前时间的Date对象
>构造器二:创建指定毫秒数的Date对象
2.两个方法的使用
>toString():显示当前的年、月、日、时、分、秒
>getTime():获取当前Date对象对应的毫秒数。(时间戳)
3. java.sql.Date对应着数据库中的日期类型的变量
>如何实例化
>如何将java.util.Date对象转换为java.sql.Date对象
*/
@Test
public void test2(){
//构造器一:Date():创建一个对应当前时间的Date对象
Date date1 = new Date();
System.out.println(date1.toString());//Sat Feb 16 16:35:31 GMT+08:00 2019
System.out.println(date1.getTime());//1550306204104
//构造器二:创建指定毫秒数的Date对象
Date date2 = new Date(155030620410L);
System.out.println(date2.toString());
//创建java.sql.Date对象
java.sql.Date date3 = new java.sql.Date(35235325345L);
System.out.println(date3);//1971-02-13
//如何将java.util.Date对象转换为java.sql.Date对象
//情况一:
// Date date4 = new java.sql.Date(2343243242323L);
// java.sql.Date date5 = (java.sql.Date) date4;
//情况二:
Date date6 = new Date();
java.sql.Date date7 = new java.sql.Date(date6.getTime());
}
3. java.text.SimpleDataFormat类
- SimpleDateFormat对日期Date类的格式化和解析
3.1.两个操作:
3.2.SimpleDateFormat的实例化:new + 构造器
//*************照指定的方式格式化和解析:调用带参的构造器*****************
// SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//格式化
String format1 = sdf1.format(date);
System.out.println(format1);//2019-02-18 11:48:27
//解析:要求字符串必须是符合SimpleDateFormat识别的格式(通过构造器参数体现),
//否则,抛异常
Date date2 = sdf1.parse("2020-02-18 11:48:27");
System.out.println(date2);
小练习:
@Test
public void testExer() throws ParseException {
String birth = "2020-09-08";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf1.parse(birth);
// System.out.println(date);
java.sql.Date birthDate = new java.sql.Date(date.getTime());
System.out.println(birthDate);
}
4.Calendar类:日历类(抽象类)
//1.实例化
//方式一:创建其子类(GregorianCalendar的对象
//方式二:调用其静态方法getInstance()
Calendar calendar = Calendar.getInstance();
// System.out.println(calendar.getClass());
//2.常用方法
//get()
int days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
//set()
//calendar可变性
calendar.set(Calendar.DAY_OF_MONTH,22);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//add()
calendar.add(Calendar.DAY_OF_MONTH,-3);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//getTime():日历类---> Date
Date date = calendar.getTime();
System.out.println(date);
//setTime():Date ---> 日历类
Date date1 = new Date();
calendar.setTime(date1);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
四、JDK8中新日期时间API
1.日期时间API的迭代:
2.前两代存在的问题举例:
3.java 8 中新的日期时间API涉及到的包
4.本地日期、本地时间、本地日期时间的使用:LocalDate/LocalTime/LocalDateTime
4.1 说明:
4.2 常用方法:
//now():获取当前日期、时间、日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
//of():设置指定的年、月、日、时、分、秒。没有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2022, 3, 8, 16, 56,36);
System.out.println(localDateTime1);
//getXxx():获取属性
System.out.println(localDateTime.getDayOfMonth());//18
System.out.println(localDateTime.getDayOfWeek());//FRIDAY
System.out.println(localDateTime.getDayOfYear());//77
System.out.println(localDateTime.getMonth());//MARCH
System.out.println(localDateTime.getMonthValue());//3
//体现不可变性
//withXxxxx():设置属性
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);//2022-03-18
System.out.println(localDate1);//2022-03-22
LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.println(localDateTime);//2022-03-18T17:10:07.169
System.out.println(localDateTime2);//2022-03-18T04:10:07.169
//plusXxx():加操作
LocalDateTime localDateTime3 = localDateTime.plusMonths(4);
System.out.println(localDateTime);//2022-03-18T17:11:58.379
System.out.println(localDateTime3);//2022-07-18T17:11:58.379
//minusXxx():减操作
LocalDateTime localDateTime4 = localDateTime.minusDays(15);
System.out.println(localDateTime);//2022-03-18T17:13:46.603
System.out.println(localDateTime4);//2022-03-03T17:13:46.603
5.时间点:Instant
5.1 说明:
5.2 常用方法:
//now():获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);
//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
//返回1970-01-01 00:00:00到当前时间的毫秒数,即为时间戳
System.out.println(instant.toEpochMilli());
//静态方法,返回在1970-01-01 00:00:00基础上加上指定毫秒数之后的Instant类的对象
Instant instant1 = Instant.ofEpochMilli(272784546406L);
System.out.println(instant1);
6.日期时间格式化类:DateTimeFormatter
6.1 说明:
6.2 常用方法:
特别的:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
// 重点:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str4 = formatter3.format(LocalDateTime.now());
System.out.println(str4);//2019-02-18 03:52:09
//解析
TemporalAccessor accessor = formatter3.parse("2019-02-18 03:52:09");
System.out.println(accessor);
五、Java比较器
1.Java比较器的使用背景:
2.自然排序:使用Comparable接口
2.1 说明
2.2 自定义类代码举例:
public class Goods implements Comparable{
private String name;
private double price;
//指明商品比较大小的方式:照价格从低到高排序,再照产品名称从高到低排序
@Override
public int compareTo(Object o) {
// System.out.println("**************");
if(o instanceof Goods){
Goods goods = (Goods)o;
//方式一:
if(this.price > goods.price){
return 1;
}else if(this.price < goods.price){
return -1;
}else{
// return 0;
return -this.name.compareTo(goods.name);
}
//方式二:
// return Double.compare(this.price,goods.price);
}
// return 0;
throw new RuntimeException("传入的数据类型不一致!");
}
// getter、setter、toString()、构造器:省略
}
3.定制排序:使用Comparator接口
3.1 说明
3.2 代码举例:
Comparator com = new Comparator() {
//指明商品比较大小的方式:照产品名称从低到高排序,再照价格从高到低排序
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Goods && o2 instanceof Goods){
Goods g1 = (Goods)o1;
Goods g2 = (Goods)o2;
if(g1.getName().equals(g2.getName())){
return -Double.compare(g1.getPrice(),g2.getPrice());
}else{
return g1.getName().compareTo(g2.getName());
}
}
throw new RuntimeException("输入的数据类型不一致");
}
}
4. 两种排序方式对比
* Comparable接口的方式一旦一定,保证Comparable接口实现类的对象在任何位置都可以比较大小。
* Comparator接口属于临时性的比较。
六、其他类
1.System类
2.Math类
3.BigInteger类、BigDecimal类
代码举例: