09.常用类

1.Object 类

jdk 中文在线文档:https://www.matools.com/api/java8

1.1.equals 方法 与 == 对比

== 比较运算符:

  • 既可以判断基本类型,又可以判断引用类型;
  • 如果判断的是基本类型,判断的是值是否相等;
  • 如果判断的是引用类型,判断的是地址是否相等,即判断是不是同一个对象
    equals 方法:
  • 只能判断引用类型
  • 默认判断的是地址是否相等,子类(Object 类是所有类的父类)往往重写该方法,用于判断内容是否相等
    只能判断引用类型,默认判断的是地址是否相等,子类中往往重写该方法,用于判断内容是否相等(比如:Integer 和 String 重写了 equals 方法)
package Equals;

public class EqualsTest01 {
    public static void main(String[] args) {
        Person p1 = new Person();
        p1.name = "tom";
        Person p2 = new Person();
        p2.name = "tom";

        System.out.println(p1 == p2);// 引用类型——判断是否为同一个对象(地址) false
        System.out.println(p1.name.equals(p2.name));// p.name是String类型,重写了equals()方法——判断内容是否一样 true
        System.out.println(p1.equals(p2));//p1,p2属于Person类,该类并没有重写equals()方法(继承父类equals()方法,即判断地址) false

        String s1 = new String("abc");
        String s2 = new String("abc");

        System.out.println(s1.equals(s2)); // true
        System.out.println(s1 == s2); // false

    }
}

class Person{
    public String name;
}

1.2.hashCode()

返回对象的哈希码值

  • 提高具有哈希结构的容器的效率;
  • 两个引用,如果指向的是同一个对象,则哈希值肯定一样,否则不一样;
  • 哈希值主要根据地址号来,不能将哈希值完全等价于地址;
  • 在后面的集合中 hashCode 如果需要的话,也会重写。

1.3.toString()

返回对象的字符串表示形式。
默认返回:全类名 + @ + 哈希值的十六进制
子类往往重写 toString 方法,用于返回对象的属性信息(快捷键:Fn + Alt + Insert)
当输出一个对象时,toString 方法会被默认调用

1.4.clone()

创建并返回此对象的副本

1.5.getClass()

表示 类对象的运行时类的Class对象

1.6.notify()

唤醒正在等待对象监视器的单个线程

1.7.wait()

导致当前线程等待,直到另一个线程调用该对象的notify()方法或notifyAll()方法

2.Math 类

abs():返回绝对值
max():返回两个值的较大值
min():返回两个值的较小值
pow(a, b):返回a的b次幂
random():返回大于等于 0.0,小于 1.0 之间的随机数
round():四舍五入
sqrt():求平方根

3.File 类

创建文件
public File(String pathname);//pathname 可以是绝对路径或相对路径
File f = new File("a.txt");
f.createNewFile();
通过 File 对象访问文件的属性
image
f.getAbsolutePath():返回绝对路径

通过 File 对象创建空文件或目录(在该对象所指的文件或目录不存在的情况下)
image

4.包装类

基本数据类型以及对应的包装类
byte -> Byte
short -> Short
int -> Integer
long -> Long
float -> Float
double -> Double
char -> Character
boolean -> Boolean
这些类都在java.lang包
包装类的意义:

  • 让基本数据类型有面向对象的特征;
  • 封装了字符串转化成基本数据类型的方法

包装类常用方法:
Integer.parseInt()
Long.paseLong()
Double.parseDouble()

public class Test {
    public static void main(String[] args) {
        
//        Integer i = new Integer(10);// 创建包装类对象
//        Integer ii = 10; // 自动打包
//        System.out.println(i+10); // 在使用上,int 和Integer 其实没有区别,可以互相使用
//        System.out.println(ii+10);
//        int j = ii;// 自动解包
//        System.out.println(j+100);

        String a = "12";
        String b = "34";
        System.out.println(a+b); // 1234
    // 转型:
        // 字符串转成int的唯一方案
        int c = Integer.parseInt(a);
        int d = Integer.parseInt(b);
        System.out.println(c+d); // 46
        
        // 字符串转成double类型
        String e = "1.25";
        double f = Double.parseDouble(e);
        System.out.println(f*6); // 7.5

        // 转成long类型
        long l = Long.parseLong("1234567");
        System.out.println(l);
    }
}

5.Date 类

5.1.Date 日期

new Date():可以获取到系统时间
getTime():可以获取到时间的 long 形式,可以用来计算时间差,单位毫秒

import java.util.Date;

public class Test {
    public static void main(String[] args) {
        Date d = new Date();
        System.out.println(d); // 系统时间
        //get...()——获取年月日.....
        System.out.println(d.getYear()+1900); // 从1900年开始算的
        System.out.println(d.getMonth()+1); // 月份从0开始计算
        System.out.println(d.getDate()); // 天数
        System.out.println(d.getHours());// 小时

        //getTime()——获取到时间的毫秒形式 返回的是long
        System.out.println(d.getTime());
    }
}

5.2.Calendar 日历

get():获取到时间的某一部分

package date;

import java.util.Calendar;

public class TestCalendar {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
//        System.out.println(cal);
        
        /*
 假设当天:
        2021
        8
        10
         */
		cal.set(Calendar.DATE,cal.get(Calendar.DATE)+31); // 计算时间(这里用天数计算的) 
        
        // 获取Calendar创建的对象里的所有内容
        System.out.println(cal.get(Calendar.YEAR)); // 2021 年
        System.out.println(cal.get(Calendar.MONTH)+1); // 月份:从0开始的  结果:为10月
        System.out.println(cal.get(Calendar.DATE)); // 日
        System.out.println(cal.get(Calendar.HOUR_OF_DAY));// 小时
        System.out.println(cal.get(Calendar.MINUTE));
        System.out.println(cal.get(Calendar.SECOND));
    }
}

set():设置时间 --> 计算时间
注:cal.setTime(d); 把Date转化成Calendar

package date;

import java.util.Calendar;
import java.util.Date;

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

        Date d = new Date();

        Calendar cal = Calendar.getInstance();

        cal.setTime(d);// 把Date转化成Calendar

        System.out.println(cal);
        System.out.println(cal.get(Calendar.YEAR)); //  年
        System.out.println(cal.get(Calendar.MONTH)+1); // 月份:从0开始的
        System.out.println(cal.get(Calendar.DATE)); // 日
    }
}

5.3.SimpleDateFormat 格式化时间

时间格式:yyyy-MM-dd HH:mm:ss
format(Date):将时间转成字符串

package Simple;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

public class Test {
    public static void main(String[] args) {
        Date d = new Date();
        System.out.println(d); //Thu Aug 12 08:40:08 CST 2021  不美观

		//                  设置格式化时间的模式,我们常用yyyy-MM-dd HH:mm:ss这个模式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 时间格式
        String s = sdf.format(d); // 格式化时间
        System.out.println(s); // 2021-08-12 08:45:09
    }
}

parse(String):将字符串转化为时间

package Simple;

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

public class Test2 {
    public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入一个时间(yyyy-MM-dd HH:mm:ss):");

        String s = sc.nextLine();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

       Date d =  sdf.parse(s); // 把字符串转成时间

        System.out.println(d);
        /*
        请输入一个时间(yyyy-MM-dd HH:mm:ss):
        2021-08-12 12:25:21
        Thu Aug 12 12:25:21 CST 2021
         */
    }
}

6.String 类

字符串类常用方法:
image

注:字符串是一个不可变类型(final 类),几乎所有的字符串操作都会返回一个新字符串而不是在原有基础上进行修改

7.StringBuilder 和 StringBuffer

7.1.String 类的缺点

String 是一个不可变的数据类型,每每拼接都会产生一个新的字符串,那么内存迟早会被这些拼接的字符串塞满。

7.2.String类和 StringBuilder 和 StringBuffer 类的区别

StringBuilder 和 StringBuffer:可变字符串,不产生新对象,比较省内存,当进行大量的字符串拼接时,建议使用 StringBuffer 和 StringBuilder,但他们两个一些方法的实现几乎跟 String 一样

7.3.StringBuilder 和 StringBuffer 类

相似点:两者用法一模一样,可以认为是一个类
区别:

  1. StringBuffer 线程安全,StringBuilder 非线程安全;
  2. StringBuilder 相比于 StringBuffer 有速度优势,多数情况下建议使用 StringBuilder 类,但当被要求线程安全时,必须使用 StringBuffer 类

字符串拼接方法:append() 方法
StringBuffer 和 StringBuilder 转成 String 类:

StringBuilder sb = new StringBuilder("猫喜欢吃鱼");
String s = sb.toString();
package String;

public class TestStringBuilder {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();// 一个空的字符串""

        StringBuilder sb2 = new StringBuilder("猫喜欢吃鱼");
        System.out.println(sb2);// 猫喜欢吃鱼

        sb2.append(",狗也喜欢吃鱼");
        System.out.println(sb2);// 追加  猫喜欢吃鱼,狗也喜欢吃鱼

        sb2.insert(1,"哈哈哈");
        System.out.println(sb2); //猫哈哈哈喜欢吃鱼,狗也喜欢吃鱼

        // 上述的操作huan'c

        // 把StringBuilder转化成String
        String s = sb2.toString();
        System.out.println(s); //猫哈哈哈喜欢吃鱼,狗也喜欢吃鱼

        // 上述操作都可以将StringBuilder换成StringBuffer,结果一样

    }
}

推荐:StringBuffer(多线程数据量较大使用)、StringBuilder(单线程数据量较大使用)、String(操作量较少使用)

posted @ 2024-05-05 23:40  落落的学习  阅读(1)  评论(0编辑  收藏  举报