JAVA常用类(String,date,comparable,system,math,枚举,注解)

1.字符串相关了的类

String:字符串,使用一对"引起来表示"
String实现了Serializable接口:表示字符串是支持序列化的。
实现了Comparable接口:表示Sring可以比较大小
Sring内部定义了final char【】 value用于存储字符串数据
Sring代表了不可变序列。简称不可变性
复习:引用数据类型==表示比较地址值

在这里插入图片描述

1.String在方法区中开辟新的字符串,如果有新的String出现,有相同字符串则将字符串地址值付给新String
2.String加减涉及变量的时候是先获取堆空间地址值,堆空间获取方法区地址值。
3.String.replace 变化一个字符时,原值不变。(不可变性)

public class stringpractice {
    String a ="kevin";
    String c ="kev";
    String e ="in";
    String d =c+e;
    @Test
    public void method(){
        System.out.println(a.equals(d));(ture)
        System.out.println(a==d);(false)
    }
}

String的几个实现方式
1.字面量(String a =“kevin”;)
2.new对象
两个方式在空间的位置不同

String和包装类的转换(Integer.parseint(),String.valueOf)

 String str ="kevin";
        Integer s =Integer.parseInt("kevin");
        String kk =String.valueOf(s);

String 与char[]数组的转换(String.tocharArray)

String str ="kevin";
        char []array =str.toCharArray();
        System.out.println(array.length);

String、StringBuffer、StringBulider
相同点:底层都是使用char型数组存储对象
不同点:String是final的,不可变的。StringBuffer、StringBulider不是final的,是可变的。StringBuffer执行速度最快,但线程不安全。
StringBuffer、StringBulider方法
tringBuffer.reverse();
stringBuffer.append(“def”);
stringBuffer.delete(3,6);
stringBuffer.insert();
stringBuffer.replace();
stringBuffer.substring(1,3);
stringBuffer.charAt();
stringBuffer.indexOf();

2.JDK之前的日期时间API(date,sql.date,simpledateformate,calendar)

实例化:

Date date =new Date();
Java.sql.Date jsd =new Java.sql.Date(long型数据类);

两个类的互相转换

java.sql.Date kjj =new java.sql.Date(date.getTime());

simpledateformate对日期date类的格式化和解析(日期转字符串,字符串转日期)

Date date =new Date();
SimpleDateFormat simpleDateFormat =new SimpleDateFormat();
String hbc = simpleDateFormat.format(date);
Date data=simpleDateFormat.parse(hbc);
System.out.println(data);

当遇到指定的格式的时候

Date date =new Date();
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(simpleDateFormat.format(date));
Date date =new Date();
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date data =simpleDateFormat.parse("2020-03-21 09:07:50");
System.out.println(data);

Calender常用方法

calendar.get(Calendar.DAY_OF_MONTH)
calendar.set(Calendar.DAY_OF_MONTH,20)
calendar.add(Calendar.DAY_OF_MONTH,-5
Date date =calendar.getTime()
calendar.setTime()

3.JDK8中新日期时间API

之前的方法是有弊端的,最大的弊端是,原来方法没有返回值,导致调用方法后原来日期格式会被更改,造成了很大的麻烦。
Time
实例化

LocalTime  localTime =LocalTime.now();
LocalDate  localDate =LocalDate.now();
LocalDateTime localDateTime =LocalDateTime.now();

方法

LocalDateTime.of(2020,7,12,13,23);设置时间
localDateTime.getDayOfMonth();获取当月的第几天
localDateTime.getDayOfWeek();
localDateTime.getDayOfYear();
localDateTime1 =localDateTime.withDayOfMonth(22);设置当月的第几天

加减
localDateTime1 = localDateTime1.plusWeeks(1);
localDateTime1 =localDateTime1.minusWeeks(1);

instant类
实例化

Instant instant = Instant.now();

调整时区为东八区

OffsetDateTime instant1 =instant.atOffset(ZoneOffset.ofHours(8));

DateTimeFormatter取代了Simpledateformate
实例化

DateTimeFormatter dateTimeFormatter =DateTimeFormatter.ISO_LOCAL_DATE_TIME;
DateTimeFormatter dateTimeFormatter =DateTimeFormatter.ofPattern("yyyy-MM-dd")定义了格式的实例化

LocalDtaTime转字符串和解析

LocalDateTime localDateTime = LocalDateTime.now();
String s = dateTimeFormatter.format(localDateTime);
解析
TemporalAccessor dateTimeFormatter1 = dateTimeFormatter.parse(s);

4.Java比较器

方法
Comparabe接口的使用
创建一个类实现comparable接口
重写方法
准备用于比较的数组 调用Arrays.sort(比较数组)具体掠过

Comparator接口(跳过)

5.System类

都是静态方法,不用实现类

常用方法
currentTimeMIllis();
exit(退出程序)
gc(垃圾回收期)
getProperty(获取属性如java版本,javahome)

6.Math类

一些静态方法,不用new对象。
一系列和数学相关的方法

7.BigInteger与BigDecimal

int数据存储有限(4字节)long也有限(8字节) 引申出BigInteger
可以是任意大的数。
里面提供一些特殊的计算方法

BigDecimal大浮点型,精度高

枚举类

1. 枚举类的理解
当一个类的对象是有限多个的时候,确定的。我们称之为枚举类。如果只有一个对象我们称之为单例模式当需要一组常量的时候推荐使用枚举类。
2.如何使用枚举类(枚举类的格式)

public enum  practice {
//    public static final practice test1 =new practice();
//    public static final practice test2 =new practice();
//    public static final practice test3 =new practice();
//    public static final practice test4 =new practice();
    test1("春天","春暖花开"),
    test2("夏天","夏日炎炎"),
    test3("秋天","秋高气爽"),
    test4("冬天","冰天雪地");
    private final String seasonName;
    private final String seasonDetail;
    private practice(String seasonName,String seasonDetail){
        this.seasonDetail =seasonDetail;
        this.seasonName=seasonName;
    }

    public String getSeasonDetail() {
        return seasonDetail;
    }

    public String getSeasonName() {
        return seasonName;
    }
    @Override
    public String toString() {
        return "practice{" +
                "seasonName='" + seasonName + '\'' +
                ", seasonDetail='" + seasonDetail + '\'' +
                '}';
    }
}

  1. 主要方法

1.values

System.out.println(EnuTest.values());

2.valueof

 System.out.println(EnuTest.valueOf("Spring"));

3.toString

 EnuTest enuTest = EnuTest.Spring;
        enuTest.toString();

5.实现接口的枚举类
Enum实现抽象接口
情况一
和正常一样
情况二

import javax.swing.*;

import java.lang.annotation.Annotation;

import static java.awt.SystemColor.info;

/**
 * @Author kevin
 * @Description
 * @Create 2021-03-22-16:42
 * @Modify
 */
public enum EnuTest implements annotationtest {
    Spring("春天","春天好"){
        @Override
        public Class<? extends Annotation> annotationType() {
            return null;
        }

        @Override
        public String value() {
            return null;
        }
    },
    Summer("夏天","夏天好"){
        @Override
        public Class<? extends Annotation> annotationType() {
            return null;
        }

        @Override
        public String value() {
            return null;
        }
    };
    private final String season;
    private final String Detail;
     EnuTest(String season ,String Detail){
        this.season=season;
        this.Detail=Detail;

    }
    public String getSeason() {
        return season;
    }

    public String getDetail() {
        return Detail;
    }

    @Override
    public String toString() {
        return "EnuTest{" +
                "season='" + season + '\'' +
                ", Detail='" + Detail + '\'' +
                '}';
    }
}

注解(结合反射)

概述:代码里的特殊标记
常见的注解
@author
@override
@version
@Text

创建自定义一个注解

/**
 * @Author kevin
 * @Description
 * @Create 2021-03-22-16:12
 * @Modify
 */
public @interface annotationtest {
    String value() default "hello"; //有默认值
    String[] value() ;			//定义多个
}

@annotationtesst value{(value,value2)}
JAVA中4种元注解用于修饰注解
@Retention(生命周期):
三种状态
1.SOURCE(被编译)
2.ClASS(默认);
3.RUNTIME(可以被反射)

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String value() default "hello";
}

@Target(声明注释可以使用的位置)
TYPE:类、注释、枚举、声明、接口
FIELD:枚举
METHOD:方法
PARAMETER:声明参数
CONSTRUCTOR:构造器
LOCAL_VARIABLE:声明变量
ANNOTATION_TYPE:注释
PACKAGE:包
TYPE_PARAMETER:
TYPE_USE:通用


/**
 * @autor kevin
 * @detatil
 * @create 2021-03-22-21:19
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface MyAnnotation {
    String value() default "hello";
}

@documented
@Inherited
继承对象也可以拥有相同注释

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值