Java - 常用类

一、包装类

1. 包装类的分类

image.png

2. 包装类和基本数据类型转换

image.png

String 和 Integer 转换

image.png

字符串的特性

image.png

二、StringBuffer

image.png
image.png

package StringBuffer_;

public class StringBuffer01 {
    public static void main(String[] args) {
        //1. StringBuffer 的直接父类是 AbstractStringBuilder
        //2. StringBuffer 实现了 Serializable,即 StringBuffer的对象可以串行化
        //3. 在父类中 AbstractStringBuilder 有属性 char[] value,不是 final
        //   该value 数组存放字符串内容,引出存放在堆中的
        //4. StringBuffer 是一个 final 类,不能被继承
        //5. 因为StringBuffer字符内容是存放在 char[] value,所以在变化(增加/删除)
        //   不用每次都要更换地址(即不是每次创建新对象),所以效率要高于 String
        StringBuffer stringBuffer = new StringBuffer("hello");
    }
}

1. String VS StringBuffer

image.png

2. StringBuffer 构造器

image.png

3. String 与 StringBuffer 相互转换

package StringBuffer_;

public class StringBuffer03 {
    public static void main(String[] args) {

        // String --> StringBuffer
        String str = "hello tom";
        //方式1:使用构造器
        //注意:返回的是 StringBuffer 对象,对 str 本身没有影响
        StringBuffer stringBuffer = new StringBuffer(str);

        //方式2:使用 append 方法
        StringBuffer stringBuffer1 = new StringBuffer();
        stringBuffer1 = stringBuffer1.append(str);

        // StringBuffer --> String
        StringBuffer stringBuffer2 = new StringBuffer("hello world");
        //方式1:使用 StringBuffer 提供的 toString 方法
        String s = stringBuffer2.toString();

        //方式2:使用构造器
        String s1 = new String(stringBuffer2);
    }
}

三、StringBuilder

image.png

1. String、StringBuffer 和 StringBuilder比较

image.png

2. 效率比较

StringBuilder > StringBuffer > String

3. String、StringBuffer 和 StringBuilder的选择

image.png

四、Arrays类

image.png
image.png

五、System类

image.png

六、BigInteger 和 BigDecimal 类

image.png

1. BigInteger

import java.math.BigInteger;

public class BigInteger01 {
    public static void main(String[] args) {
        //当我们编程中,需要处理很大的整数 long 不够用
        //可以使用 BigInteger 的类来搞定
//        long l = 2357685999999999999999999;
//        System.out.println(l);

        BigInteger bigInteger = new BigInteger("2357685999999999999999999");
        BigInteger bigInteger1 = new BigInteger("100");
        System.out.println(bigInteger);

        //1. 在堆 BigInteger 进行加减乘除的时候,需要使用对应的方法,不能直接进行 + - * /
        //2. 可以创建一个 要操作的 BigInteger 然后进行相应的操作
        BigInteger add = bigInteger.add(bigInteger1);
        System.out.println(add); // 加法

        BigInteger subtract = bigInteger.subtract(bigInteger1);
        System.out.println(subtract); //减法

        BigInteger multiply = bigInteger.multiply(bigInteger1);
        System.out.println(multiply); // 乘法

        BigInteger divide = bigInteger.divide(bigInteger1);
        System.out.println(divide); // 除法
    }
}

2. BigDecimal

import java.math.BigDecimal;

public class BigDecimal_ {
    public static void main(String[] args) {
        //当我们需要保存一个精度很高的数时,double 不够用
        // 这时可以用 BigDecimal
//        double d = 199.12123213213221321312123213d;
//        System.out.println(d);

        BigDecimal bigDecimal = new BigDecimal("199.12123213213221321312123213");
        System.out.println(bigDecimal);

        BigDecimal bigDecimal1 = new BigDecimal("1.1");
        System.out.println(bigDecimal.add(bigDecimal1));
        System.out.println(bigDecimal.subtract(bigDecimal1));
        System.out.println(bigDecimal.multiply(bigDecimal1));

        //除法这样写可能会报异常:ArithmeticException,会出现无限循环小数
        //System.out.println(bigDecimal.divide(bigDecimal1));

        //在调用 divide 方法时,指定精度即可,BigDecimal.ROUND_CEILING
        //如果有无限循环小数,就会保留 分子 的精度
        System.out.println(bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING));
    }
}

七、Date类

1. 第一代日期类

image.png
示例:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Date01 {
    public static void main(String[] args) throws ParseException {

        //1. 获取当前系统时间
        //2. 这里的 Date 类是在 java.util 包
        //3. 默认输出的日期格式是国外的方式,因此通常需要对格式进行转换
        Date d1 = new Date(); // 获取当前系统时间
        System.out.println("当前日期 = " + d1);
        Date d2 = new Date(93902032); // 通过指定毫秒数得到时间
        System.out.println("d2 = " + d2);

        //1. 创建 SimpleDateFormat 对象,可以指定相应的格式
        //2. 这里的格式使用的字母是规定好的,不能乱写
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
        String format = sdf.format(d1); //format:将日期转换成指定格式的字符串
        System.out.println("当前日期 = " + format);

        //1. 可以把一个格式化的String 转换为对应的 Date
        //2. 得到Date 仍然在输出时,还是按照国外的形式,如果希望指定格式输出,需要转换
        //3. 在把String->Date,使用的 sdf 格式需要和你给的String的格式一样,否则会抛出转换异常
        String s = "1996年01月01日 10:20:30 星期一"; // 要符合 "yyyy年MM月dd日 hh:mm:ss E" 格式
        Date parse = sdf.parse(s);
        System.out.println("parse = " + sdf.format(parse));
    }
}

image.png

2. 第二代日期类

image.png

import java.util.Calendar;

public class Calender01 {
    public static void main(String[] args) {

        //1. Calendar 是一个抽象类,并且构造器是 private
        //2. 可以通过 getInstance() 来获取实例
        //3. 提供大量的方法和字段给程序员
        //4. 如果我们需要按照 24小时进制来获取时间,Calendar.HOUR ==改成==> Calendar.HOUR_OF_DAY
        Calendar c = Calendar.getInstance(); // 创建日期类对象
        System.out.println("c = " + c);

        //2. 获取日历对象的某个日历字段
        System.out.println("年:" + c.get(Calendar.YEAR));
        System.out.println("月:" + (c.get(Calendar.MONTH))+1);
        System.out.println("日:" + c.get(Calendar.DAY_OF_MONTH));
        System.out.println("小时:" + c.get(Calendar.HOUR));
        System.out.println("分钟:" + c.get(Calendar.MINUTE));
        System.out.println("秒:" + c.get(Calendar.SECOND));

        //Calendar 没有专门的格式化方法,所以需要程序员自己来组合显示
        System.out.println(c.get(Calendar.YEAR) + "年-" + c.get(Calendar.MONTH) + "月");
    }
}

image.png

3. 第三代日期类

image.png
image.png
image.png

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDate_ {
    public static void main(String[] args) {
        //1. 使用now() 返回表示当前日期时间的 对象
        LocalDateTime ldt = LocalDateTime.now();//LocalDate.now();//LocalTime.now();
        System.out.println(ldt);

        //2. 使用 DateTimeFormatter 对象来进行格式化
        // 创建 DateTimeFormatter 对象
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss E");
        String format = dtf.format(ldt);
        System.out.println(format);
    }
}

image.png

image.png

import java.time.Instant;
import java.util.Date;

public class Instant_ {
    public static void main(String[] args) {

        //1. 通过静态方法 now() 获取表示当前时间戳的对象
        Instant now = Instant.now();
        System.out.println(now);

        //2. 通过 from 可以把 Instant 转换成 Date
        Date date = Date.from(now);

        //3. 通过 date的toInstant() 可以把 date 转换成 Instant 对象
        Instant instant = date.toInstant();
    }
}

image.png

image.png

posted @   interestinmachine  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示