1、StringBuffer类
StringBuffer是一个可变的字符串类,我们可以把它看成一个容器,这里边的可变指的是StringBuffer对象中的内容是可变的
StringBuffer类和String类的区别
String类:内容是不可变的
StringBuffer类:内容是可变的
public class StringBufferDemo01{
public static void main(String[]args){
//public StringBuffer():创建一个空白可变字符串对象,不含有任何内容
StringBuffer sb = new StringBuffer();
System.out.println("sb:"+sb);
System.out.println("sb.length():"+sb.length());
//public StringBuffer(Stringstr):根据字符串的内容,来创建可变字符串对象
StringBuffer sb2 = new StringBuffer("hello");
System.out.println("sb2:"+sb2);
System.out.println("sb2.length():"+sb2.length());
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
StringBuffer的添加和反转方法
public StringBuffer append(任意类型):添加数据,并返回对象本身
public StringBuffer reverse():返回相反的字符序列
*/
public class StringBufferDemo01 {
public static void main(String[] args) {
//创建对象
StringBuffer sb = new StringBuffer();
//public StringBuffer append(任意类型):添加数据,并返回对象本身
// StringBuffer sb2 = sb.append("hello");
// System.out.println("sb:" + sb);
// System.out.println("sb2:" + sb2);
// System.out.println(sb == sb2);
//
// sb.append("hello");
// sb.append("world");
// sb.append("java");
// sb.append(100);
//链式编程
sb.append("hello").append("world").append("java").append(100);
System.out.println("sb:" + sb);
//public StringBuffer reverse():返回相反的字符序列
sb.reverse(); //字符串翻转
System.out.println("sb:" + sb);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//创建对象
StringBuffer sb = new StringBuffer("Hello");
将StringBuffer转换为String
String str = sb.toString();
2、Math类
Math类包含用于执行基本数学运算的方法,如初等指数,对数,平方根和三角函数,其所有方法均为静态方法,并别不回创建对象,调用起来非常简单
Math.PI 常量,圆周率
public static double abs(double num)取绝对值
public static double ceil(double num)向上取整
public static double floor(double num)向下取整
public static long round(double num)四舍五入
public static int max(int a, int b)求最大值
public static int min(int a, int b)求最小值
public static double pow(double a, double b)求a的b次幂
public static double random()随机数,随机的范围[0,1)
3、Arrays类
此类包含用来操作数组的各种方法,比如排序和搜索等,其所有方法也为静态,调用起来也非常简单
public static String toString(int[] a):返回指定数组内容的字符串表示形式。
public static void sort(int[] a):对指定的int型数组按数字升序进行排序。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class ArraysDemo{
public static void main(String[] args) {
// 定义int 数组
int[] arr = {2,34,35,4,657,8,69,9};
// 打印数组,输出地址值
System.out.println(arr); // [I@2ac1fdc4
// 数组内容转为字符串
String s = Arrays.toString(arr);
// 打印字符串,输出内容
System.out.println(s); // [2, 34, 35, 4, 657, 8, 69, 9]
// 定义int 数组
int[] arr = {24, 7, 5, 48, 4, 46, 35, 11, 6, 2};
System.out.println("排序前:"+ Arrays.toString(arr));
// 排序前:[24,7,5,48,4,46,35,11,6,2]
// 升序排序
Arrays.sort(arr);
System.out.println("排序后:"+ Arrays.toString(arr));
// 排序后:[2,4,5,6,7,11,24,35,46,48]
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class ArraysTest {
public static void main(String[] args) {
// 定义随机的字符串
String line = "ysKUreaytWTRHsgFdSAoidq";
// 转换为字符数组
char[] chars = line.toCharArray();
// 升序排序
Arrays.sort(chars);
// 反向遍历打印
for (int i = chars.length‐1; i >= 0 ; i‐‐) {
System.out.print(chars[i]+" ");
// y y t s s r q o i g e d d a W U T S R K H F A
}
}
}
4、System类
System类提供了大量的静态方法,可以获取与系统相关的信息或系统级操作
点一个currentTimeMillis():返回以毫秒为单位的当前时间
点一个arraycopy(Object src,int srcPos,Object dest,intdestPos,int length ):将数组中的指定数据拷贝到另一个数组中
点一个exit(int status):用来结束正在运行的Java程序,通常传入0记为正常状态,其他为异常状态
currentTimeMillis()方法其实就是获取当前系统时间与1079年01月01日00:00点之间的毫秒差值
import java.util.Date;
public class SystemDemo {
public static void main(String[] args) {
//获取当前时间毫秒值
System.out.println(System.currentTimeMillis()); //毫秒值:1秒=1000毫秒
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| 参数序号 | 参数名称 | 参数类型 | 参数含义 |
| -------- | -------- | -------- | -------------------- |
| 1 | src | Object | 源数组 |
| 2 | srcPos | int | 源数组索引起始位置 |
| 3 | dest | Object | 目标数组 |
| 4 | destPos | int | 目标数组索引起始位置 |
| 5 | length | int | 复制元素个数 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class Demo11SystemArrayCopy {
public static void main(String[] args) {
int[] src = new int[]{1,2,3,4,5};
int[] dest = new int[]{6,7,8,9,10};
System.arraycopy( src, 0, dest, 0, 3);
/*代码运行后:两个数组中的元素发生了变化
src数组元素[1,2,3,4,5]
dest数组元素[1,2,3,9,10]
*/
}
}
5、包装类
Java提供了两个类型系统,基本类型和引用类型,使用基本类型在于效率,然而很多情况,因为对象可以做更多的功能,如果我们想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类
基本类型=对应的包装类(位于java.lang包中)
byte === Byte
short === Short
int === Integer
long === Long
float === Float
double === Double
char === Character
boolean === Boolean
基本类型与对应的包装类对象之间,来回转换的过程称之为【封箱】与【拆箱】
装箱:从基本类型转为对应的包装类对象
拆箱:从包装类对象转换为对应的基本类型
数值 转 包装对象
Integer i = new Integer(4); //使用构造函数函数
Integer iii = Integer.valueof(4);//使用包装类中的valueof方法
包装对象 转 数值
int num = i.intvalue();
自动装箱与自动拆箱:
由于我们经常要做基本类型与包装类之间的转换,从JDK 5开始,基本类型和包装类的装箱,拆箱动作可以自动完成
String转换成对应的基本类型:
方法 说明
public static byte parseByte(String s) 将字符串参数转换为对应的byte基本类型。
public static short parseShort(String s) 将字符串参数转换为对应的short基本类型。
public static int parseInt(String s) 将字符串参数转换为对应的int基本类型。
public static long parseLong(String s) 将字符串参数转换为对应的long基本类型。
public static float parseFloat(String s) 将字符串参数转换为对应的float基本类型。
public static double parseDouble(String s) 将字符串参数转换为对应的double基本类型。
public static boolean parseBoolean(String s) 将字符串参数转换为对应的boolean基本类型。
6、BigInteger类
Java中long类型为最大整数类型,对于超过long类型的输入如何表示呢?对于超过long类型的整数已经不能称之为整数了,他们被封装成BigInteger对象
public BigInteger(String val):将字符串的数组封装成BigInteger对象
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
方法说明
public BigInteger add(BigInteger val) 两个BigInteger进行相加,并返回BigInteger
public BigInteger subtract(BigInteger val) 两个BigInteger进行相减,并返回BigInteger
public BigInteger multiply(BigInteger val) 两个BigInteger进行相乘,并返回BigInteger
public BigInteger divide(BigInteger val) 两个BigInteger进行相除,并返回BigInteger
7、BigDecimal类
double和float类型在运算中很容易丢失精度,造成数据的不准确性,Java提供给我们BigDecimal类可以实现浮点数据的高精度运算
public BigDecimal(String val):将String类型的数组封装为BigDecimal对象
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public BigDecimal add(BigDecimal augend) 浮点类型数据相加操作
public BigDecimal subtract(BigDecimal subtrahend) 浮点类型数据相减操作
public BigDecimal multiply(BigDecimal multiplicand) 浮点类型数据相乘操作
public BigDecimal divide(BigDecimal divisor) 浮点类型数据相除操作
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
:浮点类型数据相除操作,按照指定的模式,保留几位小数
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static void main(String[] args) {
//大数据封装为BigDecimal对象
BigDecimal big1 = new BigDecimal("0.09");
BigDecimal big2 = new BigDecimal("0.01");
//add实现加法运算
BigDecimal bigAdd = big1.add(big2);
BigDecimal big3 = new BigDecimal("1.0");
BigDecimal big4 = new BigDecimal("0.32");
//subtract实现减法运算
BigDecimal bigSub = big3.subtract(big4);
BigDecimal big5 = new BigDecimal("1.105");
BigDecimal big6 = new BigDecimal("100");
//multiply实现乘法运算
BigDecimal bigMul = big5.multiply(big6);
//对于浮点数据的除法运算,和整数不同,可能出现无限不循环小数,因此需要对所需要的位数进行保留和选择舍入模式
}
8、日期时间类
Date类:
1、表示特定的瞬间,精确到毫秒
2、经过查阅Date类的描述,发现Date拥有多个构造函数,只是部分已经过时,但有未过时的构造可以把毫秒值转成日起对象
public Date()
分配Date对象并初始化此对象,以表示分配它的时间(精确到毫秒)
public Date(long date)
分配Date对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,
即1970年1月1日00:00:00 GMT)以来的指定毫秒数。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
由于我们处于东八区,所以我们的基准时间为1970年1月1日8时0分0秒。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class Demo01Date {
public static void main(String[] args) {
// 创建日期对象,把当前的时间
System.out.println(new Date()); // Tue Jan 16 14:37:35 CST 2018
// 创建日期对象,把当前的毫秒值转成日期对象
System.out.println(new Date(0L)); // Thu Jan 01 08:00:00 CST 1970
//在使用println方法时,会自动调用Date类中的toString方法。
//Date类对Object类中的toString方法进行了覆盖重写,所以结果为指定格式的字符串。
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public long getTime() 把日期对象转换成对应的时间毫秒值
public static void main(String[] args){
Date date = new Date();
// 获取从1970年到现在的毫秒值差
long time = date.getTime();
System.out.println(time);
}
DateForamt类:
1、日期/时间格式化子类的抽象类,我们通过这个类可以帮我们完成日期和文本之间的转换(Date === String)
2、由于DateFormat为抽象类,不能直接使用,所以需要常用的子类(SimpleDateFormat),来指定格式化或解析的标准
方法名 说明
public SimpleDateFormat()
用默认的模式和默认语言环境的日期格式符号构造SimpleDateFormat。
public SimpleDateFormat(String pattern)
用给定的模式和默认语言环境的日期格式符号构造SimpleDateFormat。
ps:参数pattern是一个字符串,代表日期时间的自定义格式。
y ===> 年
M ===> 月
d ===> 日
H ===> 时
m ===> 分
s ===> 秒
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Demo02SimpleDateFormat {
public static void main(String[] args) {
// 对应的日期格式如:2021-05-25 15:06:38
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
}
常用方法:
方法名 说明
public String format(Date date)
将Date对象格式化为字符串。
public Date parse(String source)
将字符串解析为Date对象。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public static void formatMethod(){
Date date = new Date();
// 创建日期格式化对象,在获取格式化对象时可以指定风格
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
String str = df.format(date);
System.out.println(str); //
}
public static void parseMethod(){
//把Date对象转换成String
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
String str = "2018年12月11日";
Date date = df.parse(str);
System.out.println(date); // Tue Dec 11 00:00:00 CST 2018
}
Calendar类
日历类,在Date后出现,替换掉了许多Date的方法,该类将所有可能用到的信息封装为静态成员变量,方便获取,日历类就是方便获取各个时间属性的
Calendar为抽象类,由于语言敏感,Calendar类在创建对象时并非直接创建,而是用过静态方法创建,返回子类对象
public static Calendar getInstance() :使用默认时区和语言环境获得一个日历
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.util.Calendar;
public class Demo06CalendarInit {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
}
}
常用方法:
public int get(int field)
返回给定日历字段的值。
public void set(int field, int value)
将给定的日历字段设置为给定值。
public abstract void add(int field, int amount)
根据日历的规则,为给定的日历字段添加或减去指定的时间量。
public Date getTime()
返回一个表示此Calendar时间值(从历元到现在的毫秒偏移量)的Date对象
Calendar勒种提供了很多成员变量,代表给定的日历字段
字段值 含义
YEAR ===> 年
MONTH ===> 月(从0开始,可以+1使用)
DAY_OF_MONTH ===> 月中的天(几号)
HOUR ===> 时(12小时制)
HOUR_OF_DAY ===> 时(24小时制)
MINUTE ===> 分
SECOND ===> 秒
DAY_OF_WEEK ===> 周中的天(周几,周日为1,可以-1使用)