javaAPI 概述:
API(Application Programming Interface ):应用程序编程接口
Java中的API
指的就是 JDK 中提供的各种功能的 Java类,这些类将底层的实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可。
常用API
1.String 类和StringBuilder 类
String类与StringBuilder基础用法_華同学.的博客-CSDN博客
2.Math类
1.Math类概述:
Math 包含执行 基本数字运算 的方法
2.Math中无 构造方法 ,内部方法都是静态的 直接 类名.调用方法
3.Math 类的常用方法:
public class Demo {
public static void main(String[] args) {
// 求绝对值 public static int abs(int a)返回参数的绝对值
System.out.println(Math.abs(-1));//1
// 向上取整 ceil 天花板
// public static double ceil(double a) 返回大于或等于参数的最小double值,等于一个整数
System.out.println(Math.ceil(1.3));//2
// 向下取整
// public static double floor(double a) 返回小于或等于参数的最大double值,等于一个整数
System.out.println(Math.floor(1.9));//1
// 四舍五入
// public static int round(float a) 按照四舍五入返回最接近参数的int
System.out.println( Math.round(1.4));//1
// 较大值
// public static int max(int a,int b)返回两个int值中的较大值
System.out.println(Math.max(1, 2));//2
// 较小值
// public static int min(int a,int b)返回两个int值中的较小值
System.out.println(Math.min(1, 2));//1
// 次幂 2 的 3 次幂
// public static double pow (double a,double b)返回a的b次幂的值
System.out.println(Math.pow(2,3));//8.0
// 随机数 (0.0 - 1.0]; 一般不用
// public static double random()返回值为double的正值,[0.0,1.0)
System.out.println(Math.random());
}
}
3.Object类
1.toString 方法
源码:
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
测试:
public class Demo {
public static void main(String[] args) {
Student student = new Student();
Object o = new Student();
// @ 分隔符 后边十六进制
System.out.println(student);//Api.Object类.Student@3b07d329
// 默认 打印的是 toString()
// 全类名 + @ + 地址值 的十六进制
System.out.println(student.toString());
// 全类名
System.out.println(student.getClass().getName());
// 十六进制
// hashCode() :返回的是一个int整数
// 得到的是内存中地址值 进过哈希运算 得出来的
// 结论 地址值不同 hashCode值一般不同
// 有例外
// 对象不同 地址值 肯定不同
// 进过哈希运算 会存在误差
// 地址值不同,但是hash值一样
int i = student.hashCode();
System.out.println(i);
Student student1 = new Student();
int i1 = student1.hashCode();
System.out.println(i1);
}
}
getClass().getName() 全类名
如果打印当前对象 默认 打印的是 toString()
所以将 toString() 必要时 重写:
// 重写 toString
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
2.equals :比较
== 比较
基本数据类型 比较值
引用数据类型 比较地址值
equals
String: true
String equals源码:
stu: * 如果不重写 * 源码比的还是地址值
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
return (anObject instanceof String aString)
&& (!COMPACT_STRINGS || this.coder == aString.coder)
&& StringLatin1.equals(value, aString.value);
}
所以要重写:
// 在类里 重写 equals
@Override
public boolean equals(Object o){
// 比较的对象地址值是否相同
if(this == o){
return true;
}
// 当前地址值不同时
if(o instanceof Student1){
// 当参数是stu的实例时 强转
Student1 stu = (Student1) o;
// 当名字内容相同时,我们认为同一个对象
if(stu != null && stu.getName().equals(this.name) && stu.getAge() == this.age){
return true;
}
}
return false;
}
alt + insert 一键生成:
// 重写
@Override
public boolean equals(Object o) {
// 地址值相同 直接return true
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Teacher teacher = (Teacher) o;
return age == teacher.age && Objects.equals(name, teacher.name);
}
测试类:
public class Text {
public static void main(String[] args) {
Teacher t1 = new Teacher("张三",12);
Teacher t2 = new Teacher("张三",12);
// 如果不进行 重写 equals equals 进行比较的 还是地址值
// 重写之后 比较的是 两个值是否相同
boolean equals = t1.equals(t2);
System.out.println(equals);
}
}
4.Objects类
objects 工具类 传递引用数据类型 * isNull( Object obj) 是否为 null * equals(Object obj, Object obj2) 判断 这两个 对象 的equals比较结果
类:
public class Student1 {
private String name;
private int age;
public Student1() {
}
public Student1(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//重写 equals
@Override
public boolean equals(Object o){
// 比较的对象地址值是否相同
if(this == o){
return true;
}
// 当前地址值不同时
if(o instanceof Student1){
// 当参数是stu的实例时 强转
Student1 stu = (Student1) o;
// 当名字内容相同时,我们认为同一个对象
if(stu != null && stu.getName().equals(this.name) && stu.getAge() == this.age){
return true;
}
}
return false;
}
/* @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student1 student1 = (Student1) o;
return age == student1.age && Objects.equals(name, student1.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}*/
@Override
public String toString() {
return "Student1{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
测试类:
public class Demo {
public static void main(String[] args) {
Student1 stu1 = new Student1();
Student1 stu2 = null;
System.out.println(stu1 == stu2);//false
/*
* 判断这个对象 是否为null
* 通过 ==
*
* */
if(stu2 == null){
System.out.println("该对象为null");//打印这个
}else {
System.out.println("该对象不为null");
}
Student1 stu3 = null;
System.out.println(stu3 == stu1);//false
System.out.println("==================");
// 判断是否为null
boolean aNull = Objects.isNull(stu3);
System.out.println(aNull);//true
System.out.println("===============");
String str1 = "abc";
String str2 = new String("abc");
System.out.println(str1.equals(str2));//true
// 效率更高 上边可能 null 调用 就会报空指针异常
boolean equals = Objects.equals(str2, str1);
// 源码 判断 o1 不为null 才进行equals比较
System.out.println(equals);//true
}
}
5.System类常用方法
1.exit()
public class Demo01 {
public static void main(String[] args) {
// public static void exit(int status) 终止当前运行的 Java 虚拟机,非零表示异常终止
System.out.println("哈哈");
System.exit(0);//结束 java 虚拟机 下面代码不会执行
System.out.println("呵呵");
}
}
2.System.currentTimeMillis()返回当前事件 自1970年 1 月1 日 0 点0 分
public class SystemDemo {
public static void main(String[] args) {
// 获取开始的时间节点
long start = System.currentTimeMillis();
for (int i = 1; i <= 10000; i++) {
System.out.println(i);
}
// 获取代码运行结束后的时间节点
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
}
3.数组拷贝arraycopy()
public class demo {
public static void main(String[] args) {
int[] arr = {11,22,33,44,55};
int[] arrnew = new int[6];
// 用工具类
System.arraycopy(arr,0,arrnew,1,arr.length);
for (int i = 0; i < arrnew.length; i++) {
System.out.println(arrnew[i]);
}
// 具体需求 查看文档
}
}
6.BigDecimal 用来精确计算
1.构造方法:
BigDecimal(double val) | 参数为double |
BigDecimal(String val) | 参数为String |
常用方法:
方法名 | 说明 |
---|---|
public BigDecimal add(另一个BigDecimal对象) | 加法 |
public BigDecimal subtract (另一个BigDecimal对象) | 减法 |
public BigDecimal multiply (另一个BigDecimal对象) | 乘法 |
public BigDecimal divide (另一个BigDecimal对象) | 除法 |
public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式) | 除法 |
总结:
-
BigDecimal是用来进行精确计算的
-
创建BigDecimal的对象,构造方法使用参数类型为字符串的。
-
四则运算中的除法,如果除不尽请使用divide的三个参数的方法。
代码示例:
BigDecimal divide = bd1.divide(参与运算的对象,小数点后精确到多少位,舍入模式);
参数1 ,表示参与运算的BigDecimal 对象。
参数2 ,表示小数点后面精确到多少位
参数3 ,舍入模式
BigDecimal.ROUND_UP 进一法
BigDecimal.ROUND_FLOOR 去尾法
BigDecimal.ROUND_HALF_UP 四舍五入
7.Date时间类
import java.util.Date;
public class Demo {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);// 打印当前时间 年月日 时分秒Tue Jul 26 16:51:42 CST 2022
// 从计算机 元年 过了指定时间后 的时间
Date date1 = new Date(0);
System.out.println(date1);//Thu Jan 01 08:00:00 CST 1970
Date date3 = new Date(1000);
System.out.println(date3);//Thu Jan 01 08:00:01 CST 1970
Date date2 = new Date(1000 * 60 * 60 * 24);
System.out.println(date2);//Fri Jan 02 08:00:00 CST 1970
}
}
8.SimpleDateFormat类 简单的日期格式化
/*
* y 年
* M 月
* d
* h
* m
* s 秒
*
* */
/*
* 日期 转 字符串
* 方便阅读 交给前端
* 字符串 转 日期对象
* 方便 程序员 使用 交给后端
*
*
* */
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo {
public static void main(String[] args) throws ParseException {
// 创建 日期 转换 格式化 对象 " 日其规则"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
// 将日期对象 解析成 字符串 format 格式化
String str = sdf.format(date);
System.out.println(str);
// 将字符串 转对象 parse 解析
String str1 = "2002-07-12";
Date date1 = sdf.parse(str1);
System.out.println(date1);
}
}
方法名 | 说明 |
---|---|
public long getTime() | 获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值 |
public void setTime(long time) | 设置时间,给的是毫秒值 |