1.Arrays——数组常用类
(1)Arrays类是专门操作数组的工具类,位于java.util中
(2)Arrays类常用方法
(3)数组拷贝的方式
①通过for循环遍历的方式
②clone
③System.arraycopy()
④Arrays.copyof();
2.String类
(1)String类通过final修饰——不能被继承
(2)实现了Serializable接口——序列化,可以进行传递
(3)实现了Comparable接口——可以进行比较
(4)实现了CharSequence ——length和charAt方法
(5)String对象的创建
①String str = “hello”; //字面量定义
②String s = new String(); //本质上this.value = new char[0];
③String s = new String(String original); //本质上 this.value = original.value;
④String s = new String(char [] a);//本质上 this.value = Arrays.copyOf(value,value.length);
⑤String s = new String(char [] a, int startIndex , int count);
(6)String创建方式
①字面量定义:直接定义在方法区的常量池中(常量池中不能存在两个相同的数据),如果栈内存中又两个不同变量,但内容相同,那么会指向常量池中的同一个地址,所以在==时会返回true(对比的是地址值)
②通过new定义:是在堆内存中创建了对象,==对比的是栈内存中的地址值
③String s = new String(“”)一共创建了两个对象,new的时候在堆内存中创建了一个,String()的char数组在方法区中的常量池里又创建了一个。
(7)String常用方法
valueOf(基本数据类型 b)——将基础类型数据的文本转换为字符串
Substring(int beginIndex)——切割原字符串,返回一个新的字符串,从下标为beginIndex的地方开始 切割
replace(char oldChar , char newChar)——返回一个新的字符串,它是通过newChar替换此字符串中 出现的所有oldChar
trim()——返回字符串的副本,忽略前导空白喝尾部空白
isEmpty()——当且仅当length()为0时返回true
toLowerCase()——将此String 中的所有字符串转换为小写
toUpperCase()——将此String 中的所有字符串转换为大写
(8)在字符串中查询指定子字符串的位置
charAt(int index)——获取index索引处的char值,没有则为null
indexOf(string)——返回指定字符在此字符串中第一次出现处的索引,没有则为0
lastIndexOf(int ch)——返回指定字符在此字符串中最后一次出现处的索引
lastIndexOf(int ch, int fromIndex)——返回指定字符在此字符串中最后一次出现处的索引,从指定的 索引处开始进行反向搜索
contains(CharSequence s)——判断字符串是否包含指定的char值,有则返回true
(9)比较两个字符串的内容
equals(Obiect anObiect)——将此字符串与指定的对象比较
equalsIgnoreCase(String anotherString)——将此String和另一个String比较,不考虑大小写
3.StringBuffer和StringBuilder
(1)StringBuffer和StringBuilder继承于AbstractStringBuilder,底层的存储逻辑也是用了char [ ]
(2)StringBuffer和StringBuilder和String的区别
①String是不可变的
②StringBuffer是可变的,线程安全的
③StringBuilder是可变的,线程不安全的
④StringBuffer和StringBuilder为什么可变?
1)当创建一个空的StringBuffer的时候,会默认创建一个长度为16的char数组
2)当StringBuffer( ‘ abc’ )有参数时,会在字符串长度的基础上再去添加16的长度
(3)常用方法
增——StringBuffer append( xx ):——用于字符串拼接
删——StringBuffer delete(int start , int end):——删除指定位置的内容
替换——StringBuffer replace(int start , int end, String str)——把[start ,end)位置替换为str
插——StringBuffer insert(int offset, xx )——在指定位置插入str
逆转——StringBuffer reverse( )——把当前字符序列逆转
长度——public int length()——返回当前字符串的长度
遍历——for( ) + charAt( ) / toString( )
(4)扩容机制
4.包装类
(1)为什么要有包装类
①Java语⾔是⾯向对象的编程语⾔,⽽基本数据类型声明的时变量,不是对象,为其提供包装类,增强了Java⾯向对象的性质。
②如果只有基本数据类型,使⽤时很不⽅便,⽐如,在集合类中,⽆法将int 、double等类型放进去,因为集合的容器要求元素是Object类型
③包装类还为基本类型添加了属性和方法,丰富了基本类型的操作
(2)为什么要保留基本数据类型
因为Java创建的对象都是存储在堆⾥的,使⽤的时候需要通过栈中的引⽤,常⽤的基本数据类型,不需要使⽤new在堆上创建,⽽是直接在栈内存中存储,不创建对象,就会⽐较⾼效。
(3)基本数组类型对应的包装类
基本数据类型 byte short int long float double boolean char
包装类 Byte Short Integer Long Float Double Boolean Character
其中Byte、Short、Integer、Long、Float、Double的⽗类是——Number
(4)基本数据类型与包装类相互转换
①基本数据类型转包装类——装箱
1)通过构造器:Integer t = new Integer(1);
2)通过字符串传入:Integer t = new Integer(“100”);
3)自动装箱:Integer num1 = 6;
②包装类转基本数据类型——拆箱
1)调用包装类的方法:xxxValue()
2)自动拆箱:int num2 = num1;
③自动拆箱和装箱的原理时代替我们调用了一下手动装箱和拆箱的方法
(5)String类与包装类相互转换
①String类转包装类
通过字符串参数调用构造函数:Integer i = new Integer (“123”);
②包装类转String类
1)调用包装类对象的toString方法
2)调用包装类.toString(123);
(6)基本数据类型与String类相互转换
①基本数据类型转String类型
1)调用String类的valueOf方法
2)123+123+””字符串拼接的方式
②String类转基本数据类型
调用对应包装类的parseXXX(“123”);
5.Math类
(1)Math类包含执行基本数字运算的方法。
(2)常用方法
Math.abs(a):取a的绝对值
Math.sqrt(a):取a的平方根
Math.cbrt(a):取a的立方根
Math.max(a,b):取a,b之间的最大值
Math.min(a,b):取a,b之间的最小值
Math.pow(a,b):取a的b平方
Math.ceil():逢余进一
Math.floor():逢余舍一
Math.rint():四舍五入
Math.round():四舍五入
Math.random():随机数,在[0.0,1.0)之间随机取一个值
Math.sin():正弦
Math.cos():余弦
Math.ten():正切
6.时间类
(1)时间戳
①指格林威治时间1970年01月01日00时00分00秒起至现在的总秒数或者毫秒数
②Java中获取时间戳
System.currentTimeMillis()——返回一个long型
(2)Date
①有两个Date,分别是utils下的和sql下的,是父子关系
②Date获取标准时间
Date date = new Date();
System.out.println(date); //返回了年月日 时分秒
③Date获取时间戳
long l = date.getTime();//获取日期,年份,天数
System.out.println(l);
④Date通过毫秒值,创建指定日期
Date date = new Date(885290269000L);
System.out.println(date);
⑤Date格式化
1)时间格式化
SimpleDateFormat sf = new SimpleDateFormat ();
Date date = new Date();
String format = sf.format(date); //22-12-2 下午3:54
System.out.println(format );
2)自定义格式化
SimpleDateFormat sf = new SimpleDateFormat (“yyy-MM-dd hh:mm:ss”);
Date date = new Date();
String format = sf.format(date); //22-12-2 03:54:30
System.out.println(format );
(3)Calendar类
①Calendar是一个抽象类——不能直接实例化
②主要提供特定时刻和一组日历的字段的转换方法,用于操作日历字段的方法
③创建方式
1)创建子类对象:
GregorianCalendar g = new GregorianCalendar ();
2)通过Calendar的getInstance方法创建:
Calendar c = Calendar.getInstance();
④常用方法
get():get(Calendar.DAY_OF_MONTH) get(Calendar.DAY_OF_YEAR)
set():set(Calendar.DAY_OF_MONTH,22)
add():add(Calendar.DAY_OF_MONTH,-3)
getTime():获取当前系统时间——日历类——>Date
setTime():Date——>日历类
Date date = new Date();
calendar.setTime(date);
6)获取星期几时,周日返回1,周一返回2,周二返回3…
(4)LocalDate类、LocalTime类、LocalDateTime类
①它们的实例是不可变的对象,分别表示日历系统的日期,时间,日期和时间
②创建方式
1)LocalDate now = LocalDate . now();
2)LocalTime now = LocalTime . now();
3)LocalDateTime now = LocalDateTime . now();
③自定义时间
LocalDateTime of = LocalDateTime . of( 2023 , 2 , 23 );
System.out.println( of );
System.out.println( of . getDayOfWeek() );
System.out.println( of . getMonth() );
④格式化
DateTimeFormatter dtf = DateTimeFormatter . ofPattern( “ yyy 年 MM月 dd日 ” );
String format = date . format( dtf );
System.out.println( format );
⑤LocalDateTime 常用方法
7.LocalDate常用方法整理
localDate date = new LocalDate.now();