Java之常用API
API概述
Math
public static int abs(int a)
|
返回参数的绝对值 |
public static double ceil(double a)
|
返回大于或等于参数的最小double值,等于一个整数
|
public static double floor(double a) |
返回小于或等于参数的最大double值,等于一个整数
|
public static int round(float a) | 按照四舍五入返回最接近参数的int |
public static int max(int a,int b) | 返回两个int值中的较大值 |
public static int min(int a,int b) | 返回两个int值中的较小值 |
public static double pow (double a,double b)
|
返回a的b次幂的值 |
public static double random() | 返回值为double的正值,[0.0,1.0) |
System
public static void exit(int status) 终止当前运行的 Java 虚拟机,非零表示异常终止
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。从 src 引用的源数组到 dest 引用的目标数组,数组组件的一个子序列被复制下来。被复制的组件的编号等于 length 参数。
public static long currentTimeMillis() 返回当前时间(以毫秒为单位)
int [] array={1,2,3,4};
int[] array2=new int[4];
System.arraycopy(array,0,array2,2,2);
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i]);
}
Object
toString
public class Student {
private int age;
private String name;
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public Student(int age, String name) {
this.age = age;
this.name = name;
}
}
public class test {
public static void main(String[] args) {
Student student=new Student(10,"张三");
System.out.println(student);
}
}
输出:Student{age=10, name='张三'}
equals
public class Student {
private int age;
private String name;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
public Student(int age, String name) {
this.age = age;
this.name = name;
}
}
public class test {
public static void main(String[] args) {
Student student=new Student(10,"张三");
Student student2=new Student(10,"张三");
System.out.println(student.equals(student2));
}
}
输出:true
Objects
public static String toString(对象)
|
返回参数中对象的字符串表示形式。 |
public static String toString(对象, 默认字符串) | 返回对象的字符串表示形式。 |
public static Boolean isNull(对象) | 判断对象是否为空 |
public static Boolean nonNull(对象) | 判断对象是否不为空 |
public class Student {
private int age;
private String name;
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
public Student(int age, String name) {
this.age = age;
this.name = name;
}
}
import java.util.Objects;
public class test {
public static void main(String[] args) {
Student student=new Student(10,"张三");
// Student student2=new Student(10,"张三");
String result= Objects.toString(student);
Student student2=null;
String result2=Objects.toString(student2,"null");
Student student3=null;
boolean result3=Objects.isNull(student3);
boolean result4=Objects.nonNull(student);
System.out.println(result);
System.out.println(result2);
System.out.println(result3);
System.out.println(result4);
}
}
输出:
Student{age=10, name='张三'}
null
true
true
BigDecimal
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 bd1=new BigDecimal("10.0");
BigDecimal bd2=new BigDecimal("3.0");
BigDecimal divide = bd1.divide(bd2, 2, BigDecimal.ROUND_UP);
System.out.println(divide);
输出:3.34
本文作者:GHR
本文链接:https://www.cnblogs.com/gonghr/p/14589741.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步