常用的工具类

在这里插入图片描述

Object类

  • 1、Object 是所有的类的超类、基类。位于继承树的最顶层。
  • 2、任何一个没有显示定义extends父类的类。都直接继承Object,否则就是间接继承
  • 3、任何一个类都可以享有Object提供的方法
  • 4、Object类可以代表任何一个类(多态),可以作为方法的参数、方法的返回值
    Object中常用方法
    getClass方法
    此方法用于返回该对象的真实类型(返回一个类)
    public final Class<?> getClass()
//判断运行时d对象和c对象是否是同一个类型
Animal d = new Dog();
Animal c = new Cat();

//方式1:通过 instanceof 关键字判断
if((d instanceof Dog && c instanceof Dog) ||(d instanceof Cat && c instanceof Cat)) {
    System.out.println("是同一个类型");
}else {
    System.out.println("不是同一个类型");
}
//方式2:通过getClass方法 判断
if(d.getClass() == c.getClass()) {
    System.out.println("是同一个类型");
}else {
    System.out.println("不是同一个类型");
}

hashCode方法
public native int hashCode();

  • 1、返回该对象的十进制的哈希吗值
  • 2、hash值是由hash算法通过对象的地址、对象中的字符串、数字等,计算出来的
  • 3、相同的对象应当返回相同的哈希吗值,不同的对象尽量返回不同的哈希码值
Student stu1 = new Student("zhangsan", 30);
Student stu2 = new Student("zhangsan", 30);
Student stu3 = stu1;
System.out.println(stu1.hashCode());
System.out.println(stu2.hashCode());
System.out.println(stu3.hashCode());

//hash突出
String str1 = "通话";
String str2 = "重地";

System.out.println(str1.hashCode());
System.out.println(str2.hashCode());

toString方法
返回对象的字符串表现形式

  • 全限定名+@+十六进制的hash值(地址)
  • 如果直接输出一个对象,那么默认会调用这个对象的toString方法,而toString方法是Object类提供的,返回的是“对象的地址”。但是我们一般输出对象希望输出的是对象的属性信息,所以可以重写父类的toString方法
@Override
public String toString() {
    return "Student [name=" + name + ", age=" + age + "]";
}

equals方法
Object类的equals方法的作用是比较两个对象是否相等。比较的是内存地址。其底层代码的是==
如果不想比较内存地址,那么需要重写equals方法

Student stu1 = new Student("zhangsan", 30);
Student stu2 = new Student("zhangsan", 30);

//重写equals方法之前
System.out.println(stu1 == stu2); //false
System.out.println(stu1.equals(stu2));//false

//希望如果两个对象的属性一样,就认为两个对象是相同的对象
//重写equals方法之后
System.out.println(stu1 == stu2); //false
System.out.println(stu1.equals(stu2));//true

System.out.println(stu1.hashCode());
System.out.println(stu2.hashCode());

重写equals方法

public boolean equals(Object obj) { 
    //1、非空判断
    if(obj == null) {
        return false;
    }
    //2、如果当前对象与obj相等
    if(this == obj) {
        return true;
    }
    //3、判断obj是否属于Student类型
    if(obj instanceof Student) {
        Student stu = (Student)obj;
        //4、判断属性
        if(this.name.equals(stu.name) && this.age == stu.age) {
            return true;
        }
    }
    return false;
}

总结:== 和 equals的区别

  • 两个东西都是用于比较的

  • == 可以用于基本类型和引用类型

    • ==在基本类型的比较中,比较的值是否相等,如果相等返回true,否则返回false
    • ==在引用类型的比较中,比较的地址是否相等,如果相等返回true,否则返回false
  • equals只能用于引用类型的比较

    • equals方法是Object类提供的方法,其底层实现是==比较,所以在没有重写父类的equals方法时。比较的也是地址。如果希望两个对象的属性一样,就认为两个对象是相同的对象,那么需要重写equals方法,但是重写了equals的同时也需要重写hashcode方法,因为java中约定两个对象相等,那么两个对象的hash值也应该相等

    finalize方法
    当垃圾回收器回收垃圾对象的时候,自动调用

public class Test5 {
	public static void main(String[] args) {
		Person p = new Person();
		//手动将对象标记为垃圾对象
		p = null;
		//触发垃圾回收器,回收垃圾对象
		System.gc();
	}
}
class Person{
	@Override
	protected void finalize() throws Throwable {
		super.finalize();//不要删除
		System.out.println("finalize方法执行了");
	}
}

包装类

概念:
基本数据类型没有属性和方法,引用数据类型可以拥有方法和属性,使用方便
基本数据类型对应的包装类
byte Byte
short Short
int Interge
float Float
double Double
char Character
boolean Boolean

装箱
将基本数据类型转换成包装类

//装箱
int a=10;
Integer aa=a;
//jdk1.5之前需要利用类的方法进行转换
//int转化成Integer  Integer.valueof(int 类型);
//Integer 转化成int aa.intValue();

拆箱
将包装类转换成基本数据类型

//拆箱
Integer a=10;
int aa=a;

写实体类时,属性类型建议写成包装类
Number类
Byte、Short、Integer、Long、Float、Double六个子类
提供一组方法,用于将其中某一种类型转换成其他类型 xxxValue()方法

Integer a=10;
Byte b = a.byteValue();
Short c = a.shortValue();
Long d = a.longValue();
Float e = a.floatValue();
Double f = a.doubleValue();
Integer g = a.intValue();

常用的包装类 Integer Double
常用的方法
字符串转化成包装类
1.包装类类型,xxxx.valueOf(); (重要)推荐使用
2. xxxx.parseInt();

//将字符串转换成int
String s1="123";
int a=Integer.valueOf(s1);

//将字符串转换成double
String s2="10.12";
double b=Double.valueOf(s2);

缓冲区(面试题)

/*
总结:Integer包装类,变量范围在[-128,127]中,数值相等,只要没有new新的对象,都是同一个对象
超出这个范围,或者new新的对象,都不是同一个对象 
*/
//没有new新的对象,所以相等
Integer a1=10;
Integer a2=10;
System.out.println(a1==a2);//true
System.out.println(a1.equals(a2));//true equals方法被重写,比较的是值

//age为Integer类型,没有创建新的对象,所以相等
System.out.println("--------------");
Person p1 = new Person();
Person p2=new Person();
p1.age=10;
p2.age=10;
System.out.println(p1.age==p2.age); //true,同一个对象
System.out.println(p1.age.equals(p2.age));//true

//数值在[-128,127]之间是同一个对象,没有new 新的对象,超出这个范围就要new一个对象
/*
源码
public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
*/
Integer b1=1000;
Integer b2=1000;
System.out.println(b1==b2);//false
System.out.println(b1.equals(b2));//true

//创建新的对象所有不是同一个对象
Integer c1=new Integer(10);
Integer c2=new Integer(10);
System.out.println(c1==c2);//false
System.out.println(c1.equals(c2));//true

String类

定义方式:
1.String s1=“abc”;
2.String s2=new String(“abc”);
3.字节数组方式
byte[] bytes={97,98,99};
String s3=new String(bytes,0,bytes.length);
4.字符数组方式
char[] chars={‘a’,‘b’,‘b’};
String s4=new String(chars,0,chars.length);

字符串常用方法
经常用到的:

获取类的:indexOf() length()
判断类的: equals() contains()
其它方法: replace() substring()!!! 、getBytes()、 valueOf()

//获取的方法
String s="abcdf";
//根据索引获取目标字符
char a=s.charAt(2);
//根据目标字符串获取下标,第一次出现的目标字符串,没有返回-1(重要)
int i=s.indexOf("abc");
//最后一次出现的目标字符串,没有返回-1
int i = str.lastIndexOf("0");
System.out.println(i);
//※※※※ 字符串长度(重要)
int length = str.length();
System.out.println(length);

//判断类的方法,返回值都为boolean类型
		String st1="abc";
		String st2="abc";
        String st3="ABC";
        //判断是否相同
        System.out.println(st1.equals(st2));//true
        //不区分大小写,判断是否相同
        System.out.println(st1.equalsIgnoreCase(st3));//true

        //判断字符串是否为空
        String str4="";
        System.out.println(str4.isEmpty());//true

        //字符串是否某字符串开头
        String str5="abcdfgggg";
        System.out.println(str5.startsWith("abc"));//true

        //字符串是否某字符串结尾
        System.out.println(str5.endsWith("ggg"));//true

        //判断是否包含某字符串,也可以用indexOf(),进行判断
        System.out.println(str5.contains("cd"));//true

//        //NullPointerException,报错,为null时,不能调用任何方法
//        String str6=null;
//        System.out.println(str6.isEmpty());


        //字符串的操作方法
        //字符串拼接
        String str6="abcdef";
        System.out.println(str6.concat("hello"));
        //字符串替换
        System.out.println(str6.replace("abc", "***"));

        //字符串截取,区间为左闭右开
        System.out.println(str6.substring(2, 5));
        System.out.println(str6.substring(4));

        //字符串分割
        String s7="abc-opq-lmn";
        String[] s8 = s7.split("-");
        System.out.println(Arrays.toString(s8));
        //字符串替换
        String s9 = s7.replace("-", "");
        System.out.println(s9);

        //字符串去除首尾空格
        String s10=" abc mnb  123 ";
        System.out.println(s10.trim());

        //字符串变成字节数组
        String s11="abcd";
        byte[] bytes = s11.getBytes();
        System.out.println(Arrays.toString(bytes));
        //字符串变成字符数组
        char[] chars = s11.toCharArray();
        System.out.println(Arrays.toString(chars));
        String s12="abcd你好";
        //转成大写
        System.out.println(s12.toUpperCase());
        //转成小写
        System.out.println(s11.toLowerCase());

        //将整数转为字符串
        int a=10;
        String s=String.valueOf(a);
        System.out.println(s);

String的面试题

//        1.String s1=new String("abc"); 创建了几个对象
//        如果常量池中有,"abc" ,则只会在堆中创建一个对象,
//        如果,常量池中不存在"abc",则需要在常量池中和堆中分别建一个对象也就是两个
//        2.拼接字符串时,什么时候才相等,只有拼接常量时,才相等
//        3.String.valueOf(),传入一个null时,返回的是一个"null"字符串
        String s1="a";
        String s2="b";
        String s3="ab";
        String s4=s1+s2;
        System.out.println(s3==s4);//false,s4会new两个StringBuilder对象,所以不相等

        String s5=new String("a")+"b";
        System.out.println(s5==s4);//false
        System.out.println(s5==s3);//false

        String s6=s3.intern();
        System.out.println(s3==s6);//true,将s3指向的常量池,指向s6,不会创建新的对象,所以相等,常量池不会重复

可变字符串

StringBuffer和StringBuilder
常用方法

  • append(String str);
  • delete(int start, int end)
  • insert(int offset, String str)
  • reverse()
  • toString()
StringBuffer buffer1 = new StringBuffer("abc");
        //※※※追加
        buffer1.append("abc");
        System.out.println(buffer1);

        //删除
        buffer1.delete(0,3);
        System.out.println(buffer1);

        //插入
        buffer1.insert(0,"abc");
        System.out.println(buffer1);

        //※※※逆序
        buffer1.reverse();
        System.out.println(buffer1);

        //练习:将一个字符串逆序
        String str="abcdef";
        StringBuffer sb = new StringBuffer(str);
        sb.reverse();
        String s = sb.toString();
        System.out.println(s);

String、StringBuffer和StringBuilder的区别
1、String字符串常量,一旦定义不可变
2、Stringbuffer和StringBuilder 可变的,有缓冲区,默认为大小为16个字符
3、StringBuffer线程安全,效率低;StringBuilder线程不安全,效率高
总结:在大量的字符串拼接的时候,使用 StringBuffer、StringBuilder。
而不考虑线程安全的时候,选择StringBuilder,否则选择StringBuffer

Date类

创建Date类的对象,和 SimpleDateFormat类结合使用
Date d1 = new Date();
获取当前时间的时间戳(毫秒数)
d1.getTime();

//创建Date类的对象
Date d1 = new Date();
//获取当前时间的时间戳(毫秒数)
System.out.println(d1.getTime());
//计算时间差
Date d2 = new Date(2000 - 1900, 9 - 1, 10);
long l = d1.getTime() - d2.getTime();
System.out.println(l/1000/60/60/24);
//判断d1日期是不是在d2日期之前
System.out.println(d1.before(d2));
//判断d1日期是不是在d2日期之后
System.out.println(d1.after(d2));

SimpleDateFormat类

SimpleDateFormat类的创建 format()
1.将日期格式化成:yyyy-MM-dd HH:mm:ss,也可以是yyyy年MM月dd日 HH:mm:ss
Date d1 = new Date();
2.创建SimpleDateFormat类
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
3.格式化当前时间,并返回一个字符串
String s = sdf.format(d1);
4.将字符串日期转换成Date类型日期的,parse()方法
String s1=“2022-01-01 10:00:00”;
SimpleDateFormat sdf1 = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
5.将字符串日期转换成Date类型日期的
Date d2 = sdf1.parse(s1);

Date d1 = new Date();
//创建SimpleDateFormat类
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//格式化当前时间,并返回一个字符串
String s = sdf.format(d1);
System.out.println(s);
String s1="2022-01-01 10:00:00";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//将字符串日期转换成Date类型日期的
Date d2 = sdf1.parse(s1);
System.out.println(d2);

Calendar类

Calendar 类是一个抽象类,表示一个日历类。其包含有时间的三组方法
1.get(字段) 获取指定字段的值
2.set(字段,值) 设置指定字段的指定值
3.add(字段,值) 在指定的字段添加或者减去指定的值

日期的中外差别:
Calendar 中的一月 返回 0
​ 周日 返回1

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

        Calendar c = Calendar.getInstance();
        //Calendar的get方法
        //年
        System.out.println(c.get(Calendar.YEAR));
        //月 要+1
        System.out.println(c.get(Calendar.MONTH)+1);
        //日
        System.out.println(c.get(Calendar.DAY_OF_MONTH));
        //小时
        System.out.println(c.get(Calendar.HOUR_OF_DAY));
        //分钟
        System.out.println(c.get(Calendar.MINUTE));
        //秒
        System.out.println(c.get(Calendar.SECOND));
        //星期几 要-1
        System.out.println(c.get(Calendar.DAY_OF_WEEK)-1);


        //set方法 设置年月日的日期
        c.set(Calendar.YEAR,2022);
        System.out.println(c.get(Calendar.YEAR));

        c.set(Calendar.MONTH,6-1);
        System.out.println(c.get(Calendar.MONTH));
        System.out.println("--------------------------------");
        //add  根据具体的年月日进行加减日期,加日期是正数,减日期是负数
        Calendar c1 = Calendar.getInstance();
        System.out.println(c1.get(Calendar.DAY_OF_MONTH));
        //当前日期+7天
        c1.add(Calendar.DAY_OF_MONTH,7);
        System.out.println(c1.get(Calendar.DAY_OF_MONTH));

        System.out.println(c1.get(Calendar.YEAR));
        //年份+3年
        c1.add(Calendar.YEAR,3);
        System.out.println(c1.get(Calendar.YEAR));

        System.out.println(c1.get(Calendar.MONTH)+1);
        //月份+6个月
        c1.add(Calendar.MONTH,6);
        System.out.println(c1.get(Calendar.MONTH));
        System.out.println("-------------");
        //月份-1个月
        c1.add(Calendar.MONTH,-1);
        System.out.println(c1.get(Calendar.MONTH));

        System.out.println(c1.get(Calendar.DAY_OF_WEEK));
        //星期+2天
        c1.add(Calendar.DAY_OF_WEEK,2);
        System.out.println(c1.get(Calendar.DAY_OF_WEEK));


    }
}

System类

常用方法:
1.System.currentTimeMillis() 返回当前系统时间的毫秒数 从1970年开始计算
2.System.exit(0) 终止Java虚拟机的运行 参数表示终止的状态 0表示正常退出

Math类

向上取整
Math.ceil();
向下取整
Math.floor();
四舍五入
Math.round();
随机数,范围[0,1)
Math.random();

public class MathDemo {
	public static void main(String[] args) {
		//求a的b次方法  参数1:底数   参数2:幂数
		System.out.println(Math.pow(2, 10));
		//求a平方根       参数1:要开方的数
		System.out.println(Math.sqrt(100));
		//求a立方根       参数1:要开立方的数
		System.out.println(Math.cbrt(27));
		
		//向上取整
		System.out.println(Math.ceil(10.2));
		//向下取整
		System.out.println(Math.floor(10.9));
		//四舍五入
		System.out.println(Math.round(10.5));
		
		//随机数 默认的范围[0,1)
		System.out.println(Math.random());
		//需求:随机一个两位数  [0,1)*90   [0,90) + 10     
		System.out.println((int)(Math.random()*90)+10);
	}
}

BigDecimal类

结论:1 金额要用BigDecimal
2 创建BigDecimal对象 BigDecimal decimal1 = new BigDecimal(“1.0”);
3 加减乘法 ,设置保留几位小数 setScale
4 doubleValue()获取数值,常用于比较判断

public class Demo06 {
    public static void main(String[] args) {
        BigDecimal bd1 = new BigDecimal("1.0");
        BigDecimal bd2 = new BigDecimal("0.9");
        //加
        BigDecimal bd3 = bd1.add(bd2);
        System.out.println(bd3);
        //减
        BigDecimal bd4 = bd1.subtract(bd2);
        System.out.println(bd4);
        //乘
        //设置精度,setScale(2,BigDecimal.ROUND_HALF_UP) 保留两位小数,四舍五入
        BigDecimal bd5 = bd1.multiply(bd2).setScale(2,BigDecimal.ROUND_HALF_UP);
        System.out.println(bd5);
        //除  divide(bd2, 2, BigDecimal.ROUND_HALF_UP),保留两位小数,四舍五入
        //BigDecimal.ROUND_CEILING 向上取整
        //BigDecimal.ROUND_FLOOR  向下取整
        BigDecimal bd6 = bd1.divide(bd2, 2, BigDecimal.ROUND_HALF_UP);
        System.out.println(bd6);
        //获取数值
        double d = bd1.doubleValue();
        System.out.println(d);


    }


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值