目录
Java 实用类
-
枚举
枚举 Enum,是一种有确定值区间的数据类型,本质上就是一个类,具有简洁、安全、方便等特点。
枚举的值被约束到了一个特定的范围内,只能从这个范围以内取值。
为什么要有枚举?
因为在描述某些对象的属性时,该属性的值不能随便定义,必须在某个特定的区间内取值。
出于对数据的安全性考虑,类似这种有特定取值范围的数据我们就可以使用枚举来描述。
枚举指由一组常量组成的类型,指定一个取值区间,我们只能从该区间中取值。
public enum Week {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY;
}
public class Test {
public static void main(String[] args) {
System.out.println(Week.MONDAY);
}
}
底层源码:
final class Week extends Enum{
public static final Week MONDAY;
public static final Week TUESDAY;
public static final Week WEDNSDAY;
public static final Week THURSDAY;
public static final Week FRIDAY;
public static final Week SATURDAY;
public static final Week SUNDAY;
private static final Week $VALUES[];
static{
MONDAY = new Week("MONDAY",0);
TUESDAY = new Week("TUESDAY",1);
WEDNSDAY = new Week("WEDNSDAY",2);
THURSDAY = new Week("THURSDAY",3);
FRIDAY = new Week("FRIDAY",4);
SATURDAY = new Week("SATURDAY",5);
SUNDAY = new Week("SUNDAY",6);
$VALUES[] = (new Week[]{
MONDAY,TUESDAY,WEDNSDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
})
}
public static Week[] values(){
return $VALUES.clone();
}
public static Week valueOf(String s){
return Enum.valueOf(s);
}
private Week(String s,int i){
super(s,i);
}
}
Math
Math 类为开发者提供了一系列的数学方法,同时还提供了两个静态常量 E(自然对数的底数)和 PI(圆周率)。
public class Test {
public static void main(String[] args) {
System.out.println("常量E"+Math.E);
System.out.println("常量PI"+Math.PI);
System.out.println("9的平方根"+Math.sqrt(9));
System.out.println("8的立方根"+Math.cbrt(8));
System.out.println("2的3次方"+Math.pow(2,3));
System.out.println("较大值"+Math.max(6.5,1));
System.out.println("-10.3的绝对值"+Math.abs(-10.3));
System.out.println("向上取整" + Math.ceil(10.000001));
System.out.println("向下取整" + Math.floor(10.999999));
System.out.println("随机数" + (int)(Math.random()*10));
System.out.println("四舍五入" + Math.rint(5.4));
}
}
Random
用来产生随机数的类,并且可以任意指定一个区间,在此区间范围内产生一个随机数。
方法 | 描述 |
---|---|
public Random() | 创建一个无参的随机数构造器,使用系统时间作为默认种子 |
public Random(long seed) | 使用 long 数据类型的种子创建一个随机数构造器 |
public boolean nextBoolean() | 返回一个 boolean 类型的随机数 |
public double nextDouble() | 返回一个 double 类型的随机数,0.0 - 1.0 之间 |
public float nextFloat() | 返回一个 float 类型的随机数,0.0 - 1.0 之间 |
public int nextInt() | 返回一个 int 类型的随机数 |
public int nextInt(n) | 返回一个 int 类型的随机数,0-n之间 |
public long nextLong() | 返回一个 long 类型的随机数,0-1 之间 |
import java.util.Random;
public class Test {
public static void main(String[] args) {
Random random = new Random();
//生成订单编号(时间戳+随机数)
for (int i = 1; i <= 10000; i++) {
//随机生成一个六位数
System.out.println("订单"+i+"的编号是:"+System.currentTimeMillis()+random.nextInt(100000)+100000);
}
}
}
String
Java 通过 String 类来创建和操作字符串数据。
-
String 实例化
1、直接赋值
String str = "Hello World";
2、通过构造函数创建对象
String str = new String("Hello World");
isLatin1() ? StringLatin1.equals(value, aString.value)
:StringUTF16.equals(value, aString.value);
三目运算符 三元表达式
String 常用方法
方法 | 描述 |
---|---|
public String() | 创建一个空的字符串对象 |
public String(String value) | 创建一个值为 value 的字符串对象 |
public String(char value[]) | 将一个char数组转换为字符串对象 |
public String(char value[],int offset, int count) | 将一个指定范围的char数组转为字符串对象 |
public String(byte value[]) | 将一个byte数组转换为字符串对象 |
public String(byte value[],int offset, int count) | 将一个指定范围的byte数组转为字符串对象 |
public int length() | 获取字符串的长度 |
public boolean isEmpty() | 判断字符串是否为空 |
public char charAt(int index) | 返回指定下标的字符 |
public byte[] getBytes() | 返回字符串对应的byte数组 |
public boolean equals(Object anObject) | 判断两个字符串值是否相等 |
public boolean equalsIgnoreCase(Object anObject) | 判断两个字符串值是否相等(忽略大小写) |
public int compareTo(String value) | 对字符串进行排序 |
public int compareToIgnoreCase(String value) | 忽略大小写进行排序 |
public boolean startsWith(String value) | 判断字符串是否以 value 开头 |
public boolean endsWith(String value) | 判断字符串是否以 value 结尾 |
public int hashCode() | 返回字符串的 hash 值 |
public int indexOf(String str) | 返回 str 在字符串中的下标 |
public int indexOf(String str,int formIndex) | 从指定位置查找字符串的下标 |
public String subString(int beginIndex) | 从指定位置开始截取字符串 |
public String subString(int beginIndex,int endIndex) | 截取指定区间的字符串(左开右闭) |
public String concat(String str) | 追加字符串 |
public String replaceAll(String o,String n) | 将字符串中所有的 o 替换成 n |
public String[] split(String regex) | 用指定的字符串对目标进行分割,返回数组 |
public String toLowerCase() | 转小写 |
public String toUpperCase() | 转大写 |
public char[] toCharArray() | 将字符串转为字符数组 |
null 和空是两个概念。
null 是指对象不存在,引用地址为空。
空是指对象存在,没有内容,长度为零。
StringBuffer
String 对象一旦创建,值不能修改(原来的值不能修改,一旦修改就是一个新的对象,只要一改动,就会创建一个新的对象)
修改之后会重新开辟内存空间来存储新的对象,会修改 String 的引用。
String 的值为什么不能修改?修改之后会创建一个新的对象?而不是在原有对象的基础上进行修改?
因为 String 底层是用数组来存值的,数组长度一旦创建就不可修改,所以导致上述问题。
StringBuffer 可以解决 String 频繁修改造成的空间资源浪费的问题。
StringBuffer 底层也是使用数组来存值。
-
StringBuffer 数组的默认长度为 16,使用无参构造函数来创建对象。
-
使用有参构造创建对象,数组长度=值的长度+16。
public class Test {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("Hello");
StringBuffer stringBuffer1 = new StringBuffer();
//stringBuffer 底层数组的长度是 21
//stringBuffer1 底层数组的长度是 16
stringBuffer1.append("Hello");
System.out.println(stringBuffer.toString().equals(stringBuffer1.toString()));
System.out.println(stringBuffer.length());
System.out.println(stringBuffer1.length());
}
}
length 方法返回的并不是底层数组的长度,而是它的有效长度(值的长度)
StringBuffer 一旦创建,默认会有 16 个字节的空间去修改,但是一旦追加的字符串长度超过 16,如何处理?
StringBuffer 不会重新开辟一块新的内存区域,而是在原有的基础上进行扩容,通过调用父类 ensureCapacityInternal() 方法对底层数组进行扩容,保持引用不变。
StringBuffer 的常用方法,StringBuffer 是线程安全的,但是效率较低,StringBuilder 是线程不安全的,但是效率较高。
HashMap:线程不安全,效率高
Hashtable:线程安全,效率低
方法 | 描述 |
---|---|
public StringBuffer() | 创建一个空的 StringBuffer对象 |
public StringBuffer(String str) | 创建一个值为 str 的 StringBuffer 对象 |
public synchronized int length() | 返回 StringBuffer 的长度 |
public synchronized char charAt(int index) | 返回指定位置的字符 |
public synchronized StringBuffer append(String str) | 追加内容 |
public synchronized StringBuffer delete(int start,int end) | 删除指定区间的值 |
public synchronized StringBuffer deleteCharAt(int index) | 删除指定位置的字符 |
public synchronized StringBuffer replace(int start,int end,String str) | 将指定区间的值替换成 str |
public synchronized String substring(int start) | 截取字符串从指定位置到结尾 |
public synchronized String substring(int start,int end) | 截取字符串从start开始,到end结束 |
public synchronized StringBuffer insert(int offset,String str) | 在指定位置插入 str |
public int indexOf(String str) | 从头开始查找指定字符的位置 |
public int indexOf(String str,int fromIndex) | 从fromIndex开始查找指定字符的位置 |
public synchronized StringBuffer reverse() | 进行反转 |
public synchronized String toString() | 转为 String |
读取数据不需要考虑线程安全问题,因为这种操作不存在安全隐患。
日期类
-
java.util.Date
Date 对象表示当前的系统时间
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = simpleDateFormat.format(date);
System.out.println(format);
}
}
-
java.util.Calendar
Calendar 用来完成日期数据的逻辑运算
运算思路:
1、将日期数据传给 Calendar(Calendar 提供了很多静态常量,专门用来记录日期数据)
常量 | 描述 |
---|---|
public static final int YEAR | 年 |
public static final int MONTH | 月 |
public static final int DAY_OF_MONTH | 天,以月为单位 |
public static final int DAY_OF_YEAR | 天,以年为单位 |
public static final int HOUR_OF_DAY | 小时 |
public static final int MINUTE | 分钟 |
public static final int SECOND | 秒 |
public static final int MILLSECOND | 毫秒 |
2、调用相关方法进行运算
方法 | 描述 |
---|---|
public static Calendar getInstance() | 获取Calendar实例化对象 |
public void set(int field,int value) | 给静态常量赋值 |
public int get(int field) | 获取静态常量的值 |
public final Date getTime() | 将Calendar转为Date对象 |
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) {
//计算今天所在的周是2022年的第几周
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,2022);
//1月为0,4月为3
calendar.set(Calendar.MONTH,3);
calendar.set(Calendar.DAY_OF_MONTH,9);
int week = calendar.get(Calendar.WEEK_OF_YEAR);
System.out.println(week);
//今天之后的63天是几月几号
int days = calendar.get(Calendar.DAY_OF_YEAR);
days += 63;
calendar.set(Calendar.DAY_OF_YEAR,days);
Date date = calendar.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(simpleDateFormat.format(date));
//今天之前的63天是几月几号
// calendar.set(Calendar.YEAR,2022);
// //1月为0,4月为3
// calendar.set(Calendar.MONTH,3);
// calendar.set(Calendar.DAY_OF_MONTH,9);
calendar.set(Calendar.DAY_OF_YEAR,100);
calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)-63);
date = calendar.getTime();
System.out.println(simpleDateFormat.format(date));
}
}