String类
String概述
String声明为final的,不可被继承String实现了Serialzable接口,表示字符串是支持序列化的;实现了Comparable接口,表示String可以比较大小String内部定义了final char[] value用于存储字符串数据- 通过字面量的方式给一个字符串赋值,此时的字符串值声明在字符串常量池中
- 字符串常量池中是不会存储相同内容的字符串的
String的不可变性
- 当字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的
value进行赋值 - 当对现有的字符串进行连接操作时候,也需要重新指定内存区域赋值,不能使用原有的
value进行赋值 - 当调用
String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不饿能使用原有的value进行赋值
String实例化的不同方式
- 通过字面量定义的方式
- 通过
new + 构造器的方式
//通过字面量定义的方式:
//此时的s1和s2的数据javaEE声明在方法区中的字符串常量池中。
String s1 = "javaEE";
String s2 = "javaEE";
//通过new + 构造器的方式:
//此时的s3和s4保存的地址值,是数据在堆空间中开辟空间以后对应的地址值。
String s3 = new String("javaEE");
String s4 = new String("javaEE");
字符串拼接方式赋值对比
11. 常量与常量的拼接结果在常量池,且常量池中不会存在相同内容的常量
12. 只要其中一个是变量,结果就在堆中
13. 如果拼接的结果调用intern()方法,返回值就在常量池中
String s1 = "javaEE";;
String s3 = "javaEEhadoop";
String s4 = "javaEE" + "hadoop";
String s5 = s1 + "hadoop";
System.out.println(s3 == s4);//true
System.out.println(s3 == s5);//false
StringBuffer、StringBuilder
String、StringBuffer、StringBuilder三者的对比
String:不可变的字符序列;底层使用char[]存储StringBuffer:可变的字符序列;线程安全的,效率低;底层使用char[]存储StringBuilder:可变的字符序列;jdk 5.0新增;线程不安全的,效率高;底层使用char[]存储
日期时间API
获取当前时间:System类中的currentTimeMills()
SimpleDateFormat:对日期和Date类的格式化和解析
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);
Calendar类:日历类、抽象类
//1.实例化
//方式一:创建其子类(GregorianCalendar)的对象
//方式二:调用其静态方法getInstance()
Calendar calendar = Calendar.getInstance();
//2.常用方法
//get()
int days = calendar.get(Calendar.DAY_OF_MONTH);
//set()
//calendar可变性
calendar.set(Calendar.DAY_OF_MONTH,22);
//add()
calendar.add(Calendar.DAY_OF_MONTH,-3);
//getTime():日历类---> Date
Date date = calendar.getTime();
//setTime():Date ---> 日历类
Date date1 = new Date();
calendar.setTime(date1);
days = calendar.get(Calendar.DAY_OF_MONTH);;
LocalDate(本地日期)、LocalTime(本地时间)、LocalDateTime(本地日期时间)的使用:
//now():获取当前的日期、时间、日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
//of():设置指定的年、月、日、时、分、秒。没有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
//getXxx():获取相关的属性
System.out.println(localDateTime.getDayOfMonth());
//体现不可变性
//withXxx():设置相关的属性
LocalDate localDate1 = localDate.withDayOfMonth(22);
Instant的使用
Instant的使用,类似于 java.util.Date类
//now():获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);//2019-02-18T07:29:41.719Z
//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2019-02-18T15:32:50.611+08:00
//toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ---> Date类的getTime()
long milli = instant.toEpochMilli();
//ofEpochMilli():通过给定的毫秒数,获取Instant实例 -->Date(long millis)
Instant instant1 = Instant.ofEpochMilli(1550475314878L);
DateTimeFormatter:日期时间格式类
//方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//格式化:日期-->字符串
LocalDateTime localDateTime = LocalDateTime.now();
String str1 = formatter.format(localDateTime);
//解析:字符串 -->日期
TemporalAccessor parse = formatter.parse("2019-02-18T15:42:18.797");
System.out.println(parse);
//方式二:
//本地化相关的格式。如:ofLocalizedDateTime()
//FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
String str2 = formatter1.format(localDateTime);
//本地化相关的格式。如:ofLocalizedDate()
//FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :
//适用于LocalDate
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
String str3 = formatter2.format(LocalDate.now());
//方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String str4 = formatter3.format(LocalDateTime.now());
//解析
TemporalAccessor accessor = formatter3.parse("2019-02-18 03:52:09");
System.out.println(accessor);
Java比较器
自然排序:使用Compareble接口
- 像
String、包装类重写compareTo()方法以后,进行了从小到大的排列 - 重写
compareTo(obj)的规则:
如果当前对象this大于形参对象obj,则返回正整数,
如果当前对象this小于形参对象obj,则返回负整数,
如果当前对象this等于形参对象obj,则返回零。 - 对于自定义类来说,如果需要排序,我们可以让自定义类实现
Comparable接口,重写compareTo(obj)方法。
public class Goods implements Comparable{
private String name;
private double price;
//指明商品比较大小的方式:按照价格从低到高排序,再按照产品名称从高到低排序
@Override
public int compareTo(Object o) {
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 -this.name.compareTo(goods.name);
}
}
throw new RuntimeException("传入的数据类型不一致!");
}
}
定义排序:使用Comparator
public void test3(){
String[] arr = new String[]{"AA","CC","KK","MM","GG","JJ","DD"};
Arrays.sort(arr,new Comparator(){
//按照字符串从大到小的顺序排列
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof String && o2 instanceof String){
String s1 = (String) o1;
String s2 = (String) o2;
return -s1.compareTo(s2);
}
throw new RuntimeException("输入的数据类型不一致");
}
});
}
2615

被折叠的 条评论
为什么被折叠?



