StringBuilder类
StringBuilder() 无参构造
作用:构造一个不带任何字符串的字符串生成器,初始容量为16个字符,如果大于16会尝试将扩容,新数组大小原来的变成2倍+2,容量如果还不够,直接扩充到需要的容量大小。int newCapacity = value.length * 2 + 2;
常用方法:StringBuilder append(String str)
将指定字符串添加到字符序列
//1、原来用+拼接字符串效率低。因为字符串是常量,不能改,每次拼接时都会产生新字符串对象。
//2、现在用工具类拼接字符串高效。因为底层是一个可变长度的数组,始终用的都是一个对象,减少了内存的消耗。
public class Test1 {
public static void main(String[] args) {
// method();//先用+拼接字符串
method2();// 用工具类拼接
}
public static void method2() {
String str = "abcdefg";
// 1、创建工具类对象--
StringBuilder sb = new StringBuilder();
long start = System.currentTimeMillis();// 计时开始ms
for (int i = 0; i < 10000; i++) {
// result = result + str;//+拼接
sb.append(str); // 用工具类拼接
}
long end = System.currentTimeMillis();// 计时结束
System.out.println(end - start);// for循环的执行时间?1ms
}
public static void method() {
// 1、定义字符串
String str = "abcdefg";
// 2、拼接1000次并输出
String result = "";// 定义变量,记录拼接结果
long start = System.currentTimeMillis();// 计时开始ms
for (int i = 0; i < 10000; i++) {
result = result + str;// +拼接
}
long end = System.currentTimeMillis();// 计时结束
System.out.println(end - start);// for循环的执行时间524ms
}
}
包装类
对基本类型提供了对应的包装类型,包装类型提供了更多的功能
基本类型:byte short int long float double boolean char'
包装类型: Byte Short Integer Long Float Double Boolean Character
Integer–int的包装类型
创建对象:Integer in = new Integer(int value);
返回Integer实例:static Integer valueOf(int i);
public class Test2 {
public static void main(String[] args) {
//包装类 和 基本类型 可以互相转换
//基本类型 转 包装类:valueOf() 或者 构造方法 --- 目的是 使用包装类型身上的功能 -- 自动装箱
//包装类 转 基本类型:intValue() --- 目的是 拿到包装类 包着的值 做运算 -- 自动拆箱
//1、创建Integer对象
//就是拿到基本类型 的参数 5 ,经过包装,,形成了 包装类型 的
Integer in = new Integer(5);
//静态方法valueOf 高效(-128~127范围内),如果要存相同数据,只是第一次没存时会存一次,以后还要存相同数据时就不再存了。
Integer in2 = Integer.valueOf(5);
Integer in3 = Integer.valueOf(5);
System.out.println(in2==in3);//true,只存了一次,in3没有再存
System.out.println(in == in2);//false,两种方式存储了相同数据5
//2、调用常用方法
int value = in.intValue();
int sum = in.parseInt("100");//把字符串类型的数字,转成基本类型
System.out.println(sum + 1);
//double -- Double
//自动装箱:把基本类型 double 变成 包装类型 Double --valueOf()
Double dou = Double.valueOf(9.9);
//自动拆箱: 把 包装类型的值 变成 基本类型 --doubleValue()
double dou3 = dou.doubleValue();
//常用方法
double dou2 = dou.parseDouble("8.8");//把字符串类型的数字,转成基本类型
}
}
日期类Date
import java.util.Date;
public class Test3 {
public static void main(String[] args) {
//1、创建Date对象
Date date = new Date();
//2、常见方法
System.out.println( date.getYear() );//获取现在是多少年,从1900 开始
System.out.println( date.getDate() );//获取今天是每个月的第几天
System.out.println( date.getDay() );//获取今天是每周的第几天
System.out.println( date.getHours() );//获取现在是几点钟
System.out.println( date.getMinutes() );//获取现在是每小时的多少分钟
System.out.println( date.getMonth() );//获取当前是几月,= 当前月 - 1
System.out.println( date.getSeconds() );//获取现在是每分钟的多少秒
System.out.println( date.getTime() );//获取当前的毫秒值,从1970 年 1 月 1 日 00
System.out.println( date.hashCode() );//获取哈希码值
System.out.println( date.toLocaleString() );//获取当前时间 年月日 时分秒
}
}
日期工具SimpleDateFormat
用来把String类型的日期 和 Date类型的日期 互转
Date parse(String text) 解析字符串的文本,生成 Date。
String format(Date date) 将Date类型的日期,转成 字符串类型
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) throws ParseException {
//1、输入出生日期 -- String
String birthday = new Scanner(System.in).nextLine();
//2、把string类型的日期 转成 Date类型
//参数中,需要指定日期的格式 y-年 M-月 d-天
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date birth = sdf.parse(birthday);
//3、做个时间差ms--天
long bir = birth.getTime();//获取到出生日期对应的毫秒值
long now = System.currentTimeMillis();//获取现在的毫秒值
System.out.println( (now-bir)/1000/60/60/24 );
}
}
BigDecimal/BigInteger
BigDecimal用来解决java中浮点数运算不精确的现象
BigInteger用来解决java中大整数的运算
BigDecimal(String val)
将 BigDecimal 的字符串表示形式转换为 BigDecimal。
add(BigDecimal bd)
做加法运算
subtract(BigDecimal bd)
做减法运算
multiply(BigDecimal bd)
做乘法运算
divide(BigDecimal bd)
做除法运算
import java.math.BigDecimal;
import java.util.Scanner;
public class Test5{
public static void main(String[] args) {
// method();//暴露不精确的现象
method2();//解决方案
}
public static void method2() {
double a = new Scanner(System.in).nextDouble();
double b = new Scanner(System.in).nextDouble();
//1、把a和b变成BigDecimal类型
//!!!构造方法不建议使用double参数的(还是不精确),我们建议使用string参数的构造方法
BigDecimal bd = new BigDecimal(a+"");
BigDecimal bd2 = new BigDecimal(b+"");
BigDecimal bd3 ;//记录运算结果
bd3 = bd.add(bd2);//加法运算
System.out.println(bd3);
bd3 = bd.subtract(bd2);//减法运算
System.out.println(bd3);
bd3 = bd.multiply(bd2);//乘法运算
System.out.println(bd3);
// bd3 = bd.divide(bd2);//报错:如果除法运算不精确,会抛出异常Non-terminating decimal expansion; no exact representable decimal result.
//divide(m,n,o)-m是要做除法运算的对象名,-n是要保留几位小数,-o是舍入方式(四舍五入)
bd3 = bd.divide(bd2,3,BigDecimal.ROUND_HALF_UP);
System.out.println(bd3);
}
public static void method() {
//1、接收用户输入的两个小数
double a = new Scanner(System.in).nextDouble();
double b = new Scanner(System.in).nextDouble();
//2、做+-*/运算
System.out.println(a+b);//不精确
System.out.println(a-b);//不精确
System.out.println(a*b);//不精确
System.out.println(a/b);//不精确
}
}