StringBuilder
StringBuffer的特点:
可变字符串、高效拼接字符串;
支持链式操作;
线程不安全;
任何对它指向的字符串的操作都不会产生新的对象。每个StringBuffer对象都有一定的缓冲区容量,当字符串大小没有超过容量时,不会分配新的容量,当字符串大小超过容量时,会自动增加容量。
String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且浪费大量优先的内存空间。
例如,在多次拼接字符串时,String的效率都很低。
StringBuilder的方法
StringJoiner
StringJoiner善于处理 以某 分隔符 拼接字符串
包装类
JAVA中,很多类和方法中的参数都需通过对象(例如集合),但基本数据类型却不是面向对象的,因此我们就需要用到包装类。
基本数据类型:
byte, short, int, long, float, double, char, boolean,
对应的包装类分别是
Byte, Short, Integer, Long, Float, Double, Character, Boolean。
自动拆箱与装箱
将基本数据类型转化为对应类型的包装类的过程叫“装箱”;将包装类转为对应类型的基本数据类型的过程叫“拆箱”
对于Integer, 拆箱的过程就是通过Integer 实体调用intValue()方法;装箱的过程就是调用了 Integer.valueOf(int i) 方法,直接new了一个Integer对象
建议使用valueOf() 方法创建一个包装类实例而不是直接使用构造方法,因为该方法可以走缓存提高性能
——转载自:不二人生
转侵删。
//装箱
int num = 123;
Integer i = Integer.valueOf(num);
Integer i = 123;
//自动拆箱
Integer i = new Integer(10);
int num = i;
Float f = new Float(52.31);
float fNum = f;
BigInteger
BigDecimal
枚举类
定义:
public enum WeekDay {
SUN, MON , STU , WED , THU, FRI , STA
}
public enum SexType {
/**
* M : 男
* F : 女
* S : 保密
*/
M("男"),F("女"),S("保密");
public final String value;
SexType(String value){
this.value = value;
}
}
枚举类的测试
public class SexTypeTest {
public static void main(String[] args) {
//构建一个人类对象
Person person = new Person();
person.setName("张三");
person.setSex(SexType.M);
person.setAge(20);
System.out.println(person);
//定义一个性别
SexType sex = SexType.M;
System.out.println(sex +"对应的性别为" + sex.value);
}
}
Math类
这个只需要记住每个方法怎么用就行
Math.random()生成0,1之间的随机数
Math.random()*20 - 5 范围是 -5 ~ 15
时间和日期
Date
// 创建一个 日期对象
Date date = new Date();
// 创建一个 格式化日期的 对象
// yyyy 来标识年份
// MM 标识 月份
// dd 标识 日
// HH 24进制标识小时 hh : 12进制
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS E(u) a");
// 将日期 以指定格式 转换成字符串
String format = df.format(date);
System.out.println(format);
String str = "1999/12/11" ;
// 重新设置 模式匹配串
//df.applyPattern("yyyy/MM/dd");
SimpleDateFormat df2 = new SimpleDateFormat("yyyy/MM/dd");
// 将 日期格式的字符串 转成日期对象
Date parse = df2.parse(str);
System.out.println(parse);
Instant
// 创建一个 Instant 高精度时间戳对象
Instant instant = Instant.now();
System.out.println(instant);
// 转成 毫秒
System.out.println(instant.toEpochMilli());
Instant with = instant.with(ChronoField.INSTANT_SECONDS, 1000);
System.out.println(with);
时区
// 创建一个系统自带的默认时区
ZoneId zoneId = ZoneId.systemDefault();
//中国
ZoneId of = ZoneId.of("Asia/Shanghai");
Set<String> sets = ZoneId.getAvailableZoneIds();
System.out.println(sets.size());
System.out.println(of);
System.out.println(zoneId);
LocalDateTime
test1
Date date = new Date();
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
.format(date));
//Date 年月日
LocalDate now = LocalDate.now();
System.out.println(now);
//Time 时分秒(精准到微秒)
LocalTime time = LocalTime.now();
System.out.println(time);
// 获取当前系统时间(年月日时分秒)
System.out.println(LocalDateTime.now());
//设置返回指定年月日
LocalDate of = LocalDate.of(1990, 5, 5);
System.out.println(of);
//这么多纳秒 是 这一天的哪一时刻(一天内86400000000000纳秒)
LocalTime s = LocalTime.ofNanoOfDay(24L * 60 * 60 * 1000 * 1000 * 1000 -1);
//LocalDateTime from = LocalDateTime.from(LocalDate.now());
//System.out.println(from);
System.out.println(s);
//DateTime可以转Date Date不能转DateTime
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
String format = df.format(LocalDateTime.now());
System.out.println(format);
test2
// 获取当前系统时间
LocalDateTime date = LocalDateTime.now();
// 格式化该时间
// .ofPattern 用于将构造方法私有化的类 创建对象
String text = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println(text);
// 将日期设置为 三天后的时间
date = date.plusDays(3);
System.out.println(date);
// 加30毫秒
LocalDateTime plus = date.plus(30, ChronoUnit.MILLIS);
// 加 三天 (区别于上面的设置日期)
LocalDateTime plus1 = date.plus(Duration.ofDays(3));
System.out.println(plus);
System.out.println(plus1);
test3
// 创建一个 LocalDateTIME 对象
LocalDateTime now = LocalDateTime.now();
LocalTime time = LocalTime.of(10, 22, 33);
// 将时间设置为 2020 年 7月 5号
// 时间 设置为 11:22:33.444
LocalDateTime localDateTime = now.with(ChronoField.YEAR, 2020)
.with(ChronoField.MONTH_OF_YEAR, 7)
.with(ChronoField.DAY_OF_MONTH, 5)
.withHour(11)
.withMinute(22)
.withSecond(33)
.with(ChronoField.MILLI_OF_SECOND, 444)
.with(time);
// 获取 对应的 时间
System.out.println(localDateTime.get(ChronoField.MILLI_OF_DAY));
System.out.println(localDateTime);
//false
System.out.println(time.isSupported(ChronoField.YEAR));