文章目录
九 - 常用类
java.long包下的类不用导包
8 - 1 字符串相关类
不会修改原字符串
String str = "huierb";
----此创建方式创建的数据都存放在字符串常量池中
- String类
方法名 | 返回值类型 | 用途 | 返回 |
---|---|---|---|
charAt(int index) | char | 查看某个索引处的值 | char 指定索引处的值。 |
indexOf(int ch) | int | 查看某个值处的索引 | 指定字符第一次出现的字符串内的索引。 |
isEmpty() | boolean | 判断字符串是否为空 | 返回 true ,当且仅当 length() 为0(即空字符串) |
replace(char oldChar, char newChar) | String | 已有字符串的替换 | 返回替换后的字符串 |
split(String regex) | String[] | 分割字符串 | 分割后的字符串会存入String[] |
startsWith(String prefix) | boolean | 判断字符串是否以指定的字符串开头 | |
trim() | String | 去掉字符串前后的空格 | 去掉空格后的字符串 |
substring(int beginIndex, int endIndex) —[beginIndex,endIndex) | String | 截取字符串(下标左闭右开) | 返回一个字符串,该字符串是此字符串的子字符串。 |
toUpperCase() | String | 转换为大写 | 返回转换后的字符串 |
toLowerCase() | String | 转换为小写 | 返回转换后的字符串 |
valueOf(Object obj) | String | 将参数转换为String类型 |
-
==
和equals()
区别new
的对象存放在堆中=
创建的对象放在常量池中equals()
:- 判断的时两个数据的值是否相同
==
:- 比较基本数据类型时,判断的时两个数据的值是否相同
- 比较引用数据类型时,判断的时两个数据的地址是否相同
-
StringBuilder
不是线程安全的类
是在原来字符串的基础上进行的操作,改变了原字符串,字符串拼接时,
StringBuilder
用的较多方法名 返回值类型 作用 返回 append(Object obj)
StringBuilder 专门用来在原字符串上拼接的 拼接后的字符串 delete(int start, int end)
StringBuilder 删除一段字符串[start,end] 删除后的字符串 replace(int start, int end, String str)
StringBuilder 用str替换[start,end]上的字符 替换后的字符串 insert(int offset, char c)
StringBuilder 指定位置插入 reverse()
StringBuilder 逆序输出 -
StringBuffer
与StringBuilder
中的方法基本一致StringBuffer是线程安全的类
8 - 2 数字相关类
8 - 2 -1 引用数据类型与基本数据类型
集合中的泛型必须用引用数据类型
引用数据类型的默认初始值均为null
引用数据类型 | 基本数据类型 |
---|---|
Byte | byte |
Short | short |
Integer | int |
Long | long |
Double | double |
Folat | float |
Boolean | boolean |
Character | char |
- 运行时常量池
Integer a = 12;
----引用数据类型创建的数据放在运行时常量池运行时常量池有数据范围—整数的常量池范围均是一个字节(
-128----127
)故在此范围内创建的数据地址相同,如果不再这个范围内,则创建的数据的地址不同
public class Test {
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
System.out.println("a==b : "+ (a==b)); // true
System.out.println("a.equals(b)) : " + a.equals(b));
System.out.println("a.intValue()==b : "+ (a.intValue()==b)); // equals()的底层实现
Integer a1 = 128;
Integer b1 = 128;
System.out.println("a1==b1 : "+ (a1==b1)); // false
System.out.println("a1.equals(b1)) : "+a1.equals(b1));
}
}
-
自动拆箱和自动装箱
基本数据类型-------------------(自动装箱)-------------------->引用数据类型—包装类(具有了一定的功能)
引用数据类型-------------------(自动拆箱)-------------------->基本数据类型
8 - 2 - 2 Random
类
生成随机数
random.nextInt(10)
其中的参数为随机数的范围(取不到10)
/**
* 随机产生4位验证码(包含数字、大小写字母)
*/
public class Test {
public static void main(String[] args) {
String str = "1234567890qwertyuiopasdfghjklzxcvbnmWQERTYUIOPASDFGHJKLZXCVBNM";
Random random = new Random();
char[] arr = new char[4];
for (int i = 0; i < arr.length ; i++){
arr[i] = str.charAt(random.nextInt(str.length()));
}
System.out.println(new String(arr));
}
}
8 - 2 - 3 Scanner()
类
控制台输入数据
-
next()
和nextLine()
区别两者同时使用会出错
-
next()
接收一行数据,识别到回车就认为是一行,并且会清空回车
-
nextLine()
接收字符串的数据,回车只是用来接收数据,控制台上会留有回车
-
8 - 3 Math类
方法 | 作用 |
---|---|
abs(int a) | 求绝对值 |
addExact(int x, int y) | 求和 |
max(int a, int b) | 求最大值 |
min(int a, int b) | 求最小值 |
random() | 返回0.0–1.0之间的Double类型的随机数 |
floor(double a) | 向下取整 |
ceil(double a) | 向上取整 |
Math.PI | |
pow(double a, double b) | a的b次方 |
sqrt(double a) | a的平方根 |
8 - 4 精确数字匹配(精确小数运算)
float、double都是不精确的小数
public class Test {
public static void main(String[] args) {
System.out.println(100.0/3); // 33.333333333333336
System.out.println(0.01+0.05); // 0.060000000000000005
System.out.println(1.0-0.9); // 0.09999999999999998
}
}
-
涉及到精确小数运算,不能用float、double,得用
BigDecimal
使用
BigDecimal
时,参数必须使用的构造方法,参数必须为Stringpublic class InputTest { public static void main(String[] args) { System.out.println(100.0/3); // 33.333333333333336 BigDecimal bigDecimal1 = new BigDecimal("0.01"); // 参数必须为String BigDecimal bigDecimal2 = new BigDecimal("0.05"); System.out.println(0.01+0.05); // 0.060000000000000005 System.out.println(bigDecimal1.add(bigDecimal2)); // 0.6 System.out.println(1.0-0.9); // 0.09999999999999998 System.out.println(new BigDecimal("1.0").subtract(new BigDecimal("0.9"))); // 0.1 } }
-
需要将
BigDecimal
数据格式化输出时,使用DecimalFormat
public class InputTest { public static void main(String[] args) { DecimalFormat decimal = new DecimalFormat("#,###.00"); // 定义输出的格式 String df1 = decimal.format(1000000000); // 格式化后的数据为字符串 System.out.println(df1); BigDecimal bigDecimal1 = new BigDecimal("1.0"); BigDecimal bigDecimal2 = new BigDecimal("3"); // 参与运算的数据1 参与运算的数据2 运算的舍入模式 BigDecimal divide = bigDecimal1.divide(bigDecimal2, BigDecimal.ROUND_HALF_DOWN); DecimalFormat decimalFormat = new DecimalFormat("0.00"); // 定义输出的格式 String df = decimalFormat.format(divide); // 格式化后的数据为字符串 System.out.println(df); } }
8 - 5 时间类
8 - 5 - 1 Data(日期类)
Date
yyyy-MM-dd HH:mm:ss
y:年
M:月
d:一月中的第几天
D:一年中的第几天
H:24小时
h:12小时
m:分钟
s:秒
S:毫秒
-
日期格式化输出
SimpleDateFormat
public class Test { public static void main(String[] args) { Date date = new Date(); System.out.println(date); // Mon Jun 07 20:05:10 CST 2021 // 格式化打印时间(可以单独的获取年、月、日等) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format = sdf.format(date); System.out.println(format); // 2021-06-07 20:05:10 // 将字符串格式转化为时间格式 String str = "2020-5-21 13:14:21"; try { Date myDate = sdf.parse(str); System.out.println(date.after(myDate)); //true System.out.println(date.before(myDate)); //false } catch (ParseException e) { e.printStackTrace(); } } }
8 - 5 - 1 Calendar(日历类)
涉及到日期类的计算,使用Calendar
public class InputTest {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance(); // 得到当前时间(日历表示)
System.out.println(calendar);
System.out.println(calendar.get(Calendar.YEAR)); // 哪一年
System.out.println(calendar.get(Calendar.MONTH+1)); // 哪一月
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); // 一年中的第几天
calendar.add(Calendar.DAY_OF_YEAR,100); // 计算100天以后的时间
Date date = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String f = sdf.format(date); // 将100天以后的日期格式化输出
System.out.println(f);
}
}
- 自己设置时间计算时间段
public class Test {
public static void main(String[] args) {
// 手动输入一个时间
String str = "2020-5-21 13:14:21";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = sdf1.getCalendar();
// 计算3年后的第100天是哪一天
calendar.add(Calendar.YEAR, 3);
calendar.add(Calendar.DAY_OF_YEAR, 100);
//将日历转化为日期
Date time = calendar.getTime();
//将日期转化为指定格式的字符串
String format = sdf1.format(time);
System.out.println(format);
}
}