三、常用类
1. 包装类
Java是面向对象语言,但是基本数据类型不是对象。有的时候我们需要将基本数据类型转成对象,也提供了八个包装类。
Integer
class Solution {
public static void main(String[] args) {
//基本数据类型转包装类
Integer a = new Integer(3);
Integer b = Integer.valueOf(4);
//包装类对象转基本数据类型
int c = b;
int d = b.intValue();
double e = b.doubleValue();
//字符串转包装类
Integer f = new Integer("123"); //字符串只能是数字
Integer g = Integer.parseInt("1234");
//包装类对象转字符串
String str = g.toString();
//常见的常量
System.out.println("int最大整数:" + Integer.MAX_VALUE);
}
}
其他包装类类似。
自动装箱和拆箱
class Solution {
public static void main(String[] args) {
//自动装箱
Integer a = 234; //Integer a = Integer.valueOf(234);
//自动拆箱
int b = a; //int b = a.intValue();
//报错,背后自动拆箱,调用c.intValue()
Integer c = null;
int d = c;
}
}
缓存
class Solution {
public static void main(String[] args) {
Integer in1 = 1234;
Integer in2 = 1234;
System.out.println(in1 == in2); //false 不同的对象
System.out.println(in1.equals(in2)); //true
Integer in3 = -128;
Integer in4 = -128;
System.out.println(in3 == in4); //true
System.out.println(in3.equals(in4)); //true
//系统初始时,创建了一个缓存数组【-128,127】
//当我们调用valueOf()时,检测是否在[-128,127]之间,在就从缓存数组里拿
//不在就创建新的对象
}
}
2.String类
不可变的字符序列,被称为“不可变对象”。
一般在比较时使用equals,==是比较是否是同一个对象。
class Solution {
public static void main(String[] args) {
String str1 = "hello world";
String str2 = "hello" + " world";
System.out.println(str1 == str2); // true
String str3 = "hello";
String str4 = " world";
String str5 = str3 + str4;
System.out.println(str1 == str5); //false
}
}
StringBuilder
是可变的,因为它没有final。
class Solution {
public static void main(String[] args) {
//StringBuilder线程不安全,效率高(常用)。StringBuffer线程安全,效率低
StringBuilder sb = new StringBuilder("abcd");
System.out.println(Integer.toHexString(sb.hashCode())); //地址
System.out.println(sb); //值
sb.setCharAt(2,'M');
System.out.println(Integer.toHexString(sb.hashCode()));
System.out.println(sb);
}
}
地址不变,值改变。
常用方法
class Solution {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 26; i++) {
sb.append((char)('a' + i));
}
System.out.println(sb);
sb.reverse();
System.out.println(sb);
sb.setCharAt(3,'一');
System.out.println(sb);
sb.insert(4,'我').insert(5,'你'); //链式调用
System.out.println(sb);
sb.delete(4,6).deleteCharAt(10);
System.out.println(sb);
}
}
class Solution {
public static void main(String[] args) {
String str1 = "";
long num1 = Runtime.getRuntime().freeMemory();
long time1 = System.currentTimeMillis();
for (int i = 0; i < 500; i++) {
str1 += i; //相当于产生了1000个对象
}
long num2 = Runtime.getRuntime().freeMemory();
long time2 = System.currentTimeMillis();
System.out.println(num1 - num2);
System.out.println(time2 - time1);
//应该使用这种
long num3 = Runtime.getRuntime().freeMemory();
long time3 = System.currentTimeMillis();
StringBuilder sb1 = new StringBuilder("");
for (int i = 0; i < 500; i++) {
sb1.append(i);
}
long num4 = Runtime.getRuntime().freeMemory();
long time4 = System.currentTimeMillis();
System.out.println(num3 - num4);
System.out.println(time4 - time3);
}
}
3.Date时间类
通过这条语句获取当前时刻。
long now = System.currentTimeMillis();
class Solution {
public static void main(String[] args) {
Date d1 = new Date();
Date d2 = new Date();
System.out.println(d1);
System.out.println(d1.getTime()); //返回毫秒数
System.out.println(d2.getTime());
System.out.println(d2.after(d1));
}
}
DateFormat
DateFormat是抽象类,不能直接被new。
class Solution {
public static void main(String[] args) throws ParseException {
//把时间对象按照指定的格式转成字符串
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String str = df.format(new Date());
System.out.println(str);
//把字符串转成时间对象
DateFormat df2 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
Date date = df2.parse("2021年8月2日 4时1分50秒");
System.out.println(date);
}
}
4.File类
代表文件和目录。
class Solution {
public static void main(String[] args) throws ParseException, IOException {
File f = new File("d:/a.txt");
System.out.println(f);
f.renameTo(new File("d:/b.txt"));
System.out.println(System.getProperty("user.dir"));
File f2 = new File("c.txt");
f2.createNewFile();
System.out.println(f.exists());
System.out.println(f.isDirectory());
System.out.println(f.isFile());
System.out.println(new Date(f2.lastModified())); // 最后修改时间
System.out.println(f.length());
System.out.println(f.getName());
System.out.println(f.getPath());
System.out.println(f.getAbsolutePath());
f2.delete();
}
}
class Solution {
public static void main(String[] args) throws ParseException, IOException {
File f1 = new File("d:/你好/w/a");
boolean flag = f1.mkdir(); //false 目录有一个不存在就不行
boolean flag1 = f1.mkdirs(); //true 构建整个目录树
}
}
递归打印目录树
class Solution {
public static void main(String[] args) throws ParseException, IOException {
File f = new File(System.getProperty("user.dir"));
printFile(f,0);
}
static void printFile(File file,int level){
for (int i = 0; i < level; i++) {
System.out.print("-");
}
System.out.println(file.getName());
if (file.isDirectory()){
File[] files = file.listFiles();
for (File temp : files){
printFile(temp,level + 1);
}
}
}
}
5.枚举
所有的枚举都是常量,本质上还是类。隐形的继承java.lang.Enum,每个被枚举的成员实际就是一个枚举类型的实例,默认是public static final修饰的。
class Solution {
public static void main(String[] args) {
System.out.println(Season.SPRING);
Season a = Season.SUMMER;
switch (a){
case SPRING:
System.out.println();
case SUMMER:
System.out.println();
case AUTUMN:
System.out.println();
case WINTER:
System.out.println();
}
}
}
enum Season{
SPRING,SUMMER,AUTUMN,WINTER
}
enum Week{
星期一,星期二
}