Java学习day08-Java高级-Java常用类

字符串相关类

String类以及常用方法

在这里插入图片描述
String:字符串。使用一对“”扩起来表示
1.String声明为final的,不可被继承
2.String实现了Serializable接口:表示字符串支持序列化。
实现了Comparable接口:表示String可以比较大小
3.String定义了final char[] value数组用于存储字符串数据
4.String代表一个不可变的字符序列。
5.通过字面量的方式(区别与new)给一个字符串赋值,此时的字符串声明在字符串常量中。
6. 字符串常量池中是不会存储相同内容的字符串

String两种初始化方式的区别

在这里插入图片描述
String s1=“abc” s1的数据javaEE声明在方法区中的字符串常量池中。
String s2= new String(“JAVA”) s2的数据(只要是new的)在堆空间中开辟。

String不同拼接操作的对比

但凡含有对象的拼接,都是在堆空间中开辟新的内存去建立临时对象。如果拼接结果调用intern()方法,返回值就在常量池中。
在这里插入图片描述

三种VJM

针对于不同问题对JVM进行优化
1.Sun公司的HotSpot 被Oracle 收购
2.BEA公司的JRockit 被Oracle 收购
3.IBM公司的J9 VM

字符串常量池在堆空间的元空间中

String类常用方法

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

String与基本数据类型和包装类之间的转换

String–>基本数据类型、包装类:调用parsexxx(str);
基本数据类型、包装类–>String:调用String重载的valueof(xxx)方法

String与char[]数组之间的转换

String–>char[] :调用String中的toCharAarray方法
char[] -->String:调用String的构造器

String与byte[]数组之间的转换

String–>byte[] :调用String中的getBytes方法
char[] -->String:调用String的构造器

可变字符串之StringBuffer和StringBuilder

String:不可变字符序列,底层使用char[]存储
StringBuffer:可变字符序列:线程安全的,效率低,底层使用char[]存储
StringBuilder:可变字符序列:线程不安全的,效率高,JDK5.0新增,底层使用char[]存储

可变的意思是:可以改变对象中原本char数组的值,而不可变是用新的对象替换旧的。且长度可变。如果添加字符超过原有长度16则扩容为原来的两倍+2

JDK8之前的日期时间API

System静态方法

1.System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差。一般称为时间戳。

package com.packtest.java;/*
@author LiRui
@create 2021-10-29 8:57
*/

import org.junit.Test;

public class TimeTest {
    @Test
    public void test1(){
        long time = System.currentTimeMillis();
        System.out.println(time);
    }
}

在这里插入图片描述

Date类

package com.packtest.java;/*
@author LiRui
@create 2021-10-29 8:57
*/

import org.junit.Test;

import java.util.Date;


public class TimeTest {

    @Test
    public void test2(){
    //构造器1:
        Date date1= new Date();
        System.out.println(date1.toString());//显示年月日时分秒
        System.out.println(date1.getTime());//获取时间戳
     //构造器2:
        Date date2= new Date(1550306204104L);
        System.out.println(date2.toString());//显示年月日时分秒
    }

}

SimpleDateFormat类

一种无参数,将Date格式化,一种有参数输入固定时间模式,将Date格式化为日期。

    @Test
    public void test3(){
       //实例化SimpleDateFormat
        SimpleDateFormat sdf= new SimpleDateFormat();
        //格式化:日期转化为字符串
        Date date1= new Date();
        System.out.println(date1.toString());
        String format=sdf.format(date1);
        System.out.println(format);
        //解析 格式化逆过程,字符串变为日期
       String str="21-10-29 上午9:47";
        Date date2= null;
        try {
            date2 = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(date2);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format1 = sdf1.format(date1);
        System.out.println(format1);
    }

Calendar类

Calender日历类(抽象类)的使用

 @Test
    public void testCalender(){
        //1.实例化
        //方式1:创建子类GregorianCalendar对象
        //方式2:调用其静态方法
        Calendar instance = Calendar.getInstance();
        //System.out.println(instance.getClass());
        //2.常用方法
        int days = instance.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        instance.set(Calendar.DAY_OF_MONTH,22);
        days = instance.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        instance.add(Calendar.DAY_OF_MONTH,3);
        days = instance.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        Date time = instance.getTime();
        System.out.println(time);
        Date date = new Date();
        instance.setTime(date);
        days=instance.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
    }

JDK8中新的日期时间API

在这里插入图片描述

LocalDate, LocalTime, LocalDateTime

    @Test
    public void testNewMethod(){
        //now():获取当前日期、时间、日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);
        //of():设置指定的年月日时分秒没有偏移量
        LocalDateTime time1 = LocalDateTime.of(2021, 10, 29, 10, 39);
        System.out.println(time1);
    }

Instant瞬时

   @Test
    public void InstantTest(){
        Instant instant = Instant.now();
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
        long time = instant.toEpochMilli();//获取1970年1月1日0:00UTC到现在的毫秒数
        System.out.println(time);
    }

DateTimeFormatter

类似于SimpleDateFormat格式化解析日期、时间

    @Test
    public void DateTimeFormatTest(){
        //方式1:预定义的标准格式
        DateTimeFormatter isoLocalDateTime = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        LocalDateTime localDateTime = LocalDateTime.now();
        String format = isoLocalDateTime.format(localDateTime);
        System.out.println(format);
        //方式2:ofLocalizedDate(FormatStyle.SHORT)
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
        String format1 = dateTimeFormatter.format(localDateTime);
        System.out.println(format1);
        //方式3:ofPattern()
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss E");
        String format2 = dateTimeFormatter1.format(localDateTime);
        System.out.println(format2);
    }

其他类

在这里插入图片描述

Java比较器

Comparable接口 自然排序

在这里插入图片描述

Comparator接口 定制排序

在这里插入图片描述

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;

public class CompareTest {

    @Test
    public void test(){

    String[] str=new String[]{"AA","CC","KK"};
    Arrays.sort(arr,new Comparator(){

        @Override
        public int compare(Object o1, Object o2) {
        //重写比较方法, 让sort根据该比较规则进行排序
            return 0;
        }
    });
    }
}

System类

在这里插入图片描述
在这里插入图片描述

Math类

在这里插入图片描述

BigInterger与BigDecimal

用于大整数的运算
在这里插入图片描述
在这里插入图片描述
用于大浮点型数计算
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值