API的基本使用
文章目录
1. API的基本使用
API: 应用程序接口,JDK中提供的类
是JDK官方所提供的类的说明书
如何查看?
- 查看这个类所在包,如果是java.long包在使用的时候不需要导包
- 查看这个类所对应的继承关系
- 查看这个类的描述
- 查看这个类的 开始jdk版本
- 查看这个类的构造方法
- 查看这个类的成员方法
2. Math类
Math类所在的包是java.long,在使用的时候不需要导包,Math类中提供了常见的数学运算的方法.
在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) |
示例
public class MathDemo {
public static void main(String[] args) {
// public static int abs(int a) 返回参数的绝对值
// int abs = Math.abs(10);
// System.out.println(abs);
// public static double ceil(double a) 向上取整
// double ceil = Math.ceil(10.1);
// System.out.println(ceil);
// public static double floor(double a) 向下取整
// double floor = Math.floor(10.9);
// System.out.println(floor);
// public static int round(float a) 四舍五入
// long round = Math.round(10.1);
// System.out.println(round);
//
// long round1 = Math.round(1.9);
// System.out.println(round1);
// public static int max(int a,int b) 返回两个int值中的较大值
// int max = Math.max(10, 20);
// System.out.println(max);
// public static int min(int a,int b) 返回两个int值中的较小值
// int min = Math.min(10, 20);
// System.out.println(min);
// public static double pow(double a,double b)返回a的b次幂的值
// double pow = Math.pow(2, 3);
// System.out.println(pow);
// public static double random() 返回值为double的正值,[0.0,1.0)
for (int i = 0; i < 10 ; i++) {
double random = Math.random();
System.out.println(random);
}
}
}
3. System类
System类所在的包是java.long,在使用的时候不需要导包.System类提供了一些和系统操作相关的方法
System类中没有公共的构造方法,但大部分方法都是静态的,可以通过类名直接访问.
public static void exit(int status) // 退出java虚拟机
public static long currentTimeMillis() // 获取当前系统时间对应的毫秒值
public static long nanoTime() // 获取当前系统时间的纳秒值
/*
src:表示的源数组
srcPos:源数组对应的元素的起始索引
dest:表示的目标数组
destPos:目标数组的索引
length:个数
*/
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) // 进行数组元素的copy
示例
public class SystemDemo {
public static void main(String[] args) {
// public static void exit(int status) 终止当前运行的 Java 虚拟机,非零表示异常终止
// System.out.println(111);
// //while(true){}
// System.exit(0); //当代码执行到这个方法的时候,就表示虚拟机已经停止了
// System.out.println(2222);
// public static long currentTimeMillis() 返回当前时间(以毫秒为单位)
// long start = System.currentTimeMillis();//获取当前时间
// //System.out.println(l);
// for (int i = 0; i < 10000; i++) {
// System.out.println(i);
// }
// long end = System.currentTimeMillis();//获取当前时间
// System.out.println(end - start);//472 --- 得到的就是这个for循环运行的时间.
// arraycopy(数据源数组, 起始索引, 目的地数组, 起始索引, 拷贝个数) 数组copy
int [] arr1 = {1,2,3,4,5};
int [] arr2 = new int[10];
//需求:我要把arr1中的数据拷贝到arr2中.
//System.arraycopy(arr1,0,arr2,0,arr1.length);
/*for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}*/
//我要把arr1最后两个元素,拷贝到arr2的最后两个索引上
System.arraycopy(arr1,3,arr2,8,2);
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
// 获取当前系统时间的纳秒值
long nanoTime = System.nanoTime();
System.out.println(nanoTime);
}
}
4 Object类
Object类是整个集成体系的父类,所有的类都直接或间接的继承了Object类,所有的类都可以去使用Object中的方法
4.1 toString方法
public String toString() // 返回对象的字符串表现形成,默认情况下是由两部分组成:包名 + 类名@int类的值
直接输出某一各引用变量时,都会默认调用Object中的toString方法,除法类中重写该方法
public class Demo {
public static void main(String[] args) {
Student s = new Student("张三",23);
System.out.println(s.toString());//com.itheima.demo1.Student@3f3afe78
System.out.println(s); // 默认情况下会调用toString方法
}
}
toString方法在Object类中默认返回值就是该对象的地址,所以需要对该方法在类中重写
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
4.2 equals方法
在Object类中的equals方法默认比较的是地址值
public boolean equals(Object obj) {
return (this == obj);
}
如果我们需要比较两个对象的内容,就需要去重写Object类中的equals方法
@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;
}
5. Objects类
Objects类所在的包是java.util,在使用时需要导包
提供了很多对象操作的方法,可以认为是对象的工具类
方法名 | 说明 |
---|---|
public static String toString(对象) | 返回参数中对象的字符串表示形式。 |
public static String toString(对象, 默认字符串) | 返回对象的字符串表示形式。 |
public static Boolean isNull(对象) | 判断对象是否为空 |
public static Boolean nonNull(对象) | 判断对象是否不为空 |
示例
public class MyObjectsDemo {
public static void main(String[] args) {
// public static String toString(对象): 返回参数中对象的字符串表示形式。
// Student s = new Student("小罗同学",50);
// String result = Objects.toString(s);
// System.out.println(result);
// System.out.println(s);
// public static String toString(对象, 默认字符串): 返回对象的字符串表示形式。如果对象为空,那么返回第二个参数.
//Student s = new Student("小花同学",23);
// Student s = null;
// String result = Objects.toString(s, "随便写一个");
// System.out.println(result);
// public static Boolean isNull(对象): 判断对象是否为空
//Student s = null;
// Student s = new Student();
// boolean result = Objects.isNull(s);
// System.out.println(result);
// public static Boolean nonNull(对象): 判断对象是否不为空
//Student s = new Student();
Student s = null;
boolean result = Objects.nonNull(s);
System.out.println(result);
}
}
6. BigDecimal
BigDecimal类在java.math这个包中,需要导包4
可以进行浮点数的数学运算的运算符,可以使结果更加的精确
构造方法
public BigDecimal(String val) // 把一个数字字符串转换成BigDecimal的对象
使用这个构造方法创建出来的BigDecimal对象,后期再进行数学运算的时候不会存在精度丢失的问题
成员方法
方法名 | 说明 |
---|---|
public BigDecimal add(另一个BigDecimal对象) | 加法 |
public BigDecimal subtract (另一个BigDecimal对象) | 减法 |
public BigDecimal multiply (另一个BigDecimal对象) | 乘法 |
public BigDecimal divide (另一个BigDecimal对象) | 除法 |
public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式) | 除法 |
示例
public class MyBigDecimalDemo3 {
//如果想要进行精确运算,那么请使用字符串的构造
public static void main(String[] args) {
// BigDecimal bd1 = new BigDecimal(0.1);
// BigDecimal bd2 = new BigDecimal(0.2);
BigDecimal bd1 = new BigDecimal("0.1");
BigDecimal bd2 = new BigDecimal("0.2");
// public BigDecimal add(另一个BigDecimal对象) 加法
BigDecimal add = bd1.add(bd2);
System.out.println("和为" + add);
//System.out.println(0.1 + 0.2);
// public BigDecimal subtract (另一个BigDecimal对象) 减法
BigDecimal subtract = bd1.subtract(bd2);
System.out.println("差为" + subtract);
// public BigDecimal multiply (另一个BigDecimal对象) 乘法
BigDecimal multiply = bd1.multiply(bd2);
System.out.println("积为" + multiply);
// public BigDecimal divide (另一个BigDecimal对象) 除法
BigDecimal divide = bd1.divide(bd2);
System.out.println("商为"+divide);
}
}