API概述

什么是API
API (Application Programming Interface) :应用程序编程接口
java中的API
指的就是 JDK 中提供的各种功能的 Java类,这些类将底层的实现封装了起来,我们不需要关心这些类是如何
实现的,只需要学习这些类如何使用即可,我们可以通过帮助文档来学习这些API如何使用。

Math

1、Math类概述
Math 包含执行基本数字运算的方法
2、Math中方法的调用方式
Math类中无构造方法,但内部的方法都是静态的,则可以通过 类名.进行调用
3、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

Object类概述
Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类,
换句话说,该类所具备的方法,所有类都会有一份
 
查看方法源码的方式
选中方法,按下Ctrl + B
 
重写toString方法的方式
1. Alt + Insert 选择toString
2. 在类的空白区域,右键 -> Generate -> 选择toString

toString

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

equals方法的作用
用于对象之间的比较,返回true和false的结果
 
举例:s1.equals(s2); s1和s2是两个对象
 
重写equals方法的场景
不希望比较对象的地址值,想要结合对象属性进行比较的时候。
 
重写equals方法的方式
1. alt + insert 选择equals() and hashCode(),IntelliJ Default,一路next,finish即可
2. 在类的空白区域,右键 -> Generate -> 选择equals() and hashCode(),后面的同上。
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对象,精确几位,舍入模式) 除法 
总结
1. BigDecimal是用来进行精确计算的
2. 创建BigDecimal的对象,构造方法使用参数类型为字符串的。
3. 四则运算中的除法,如果除不尽请使用divide的三个参数的方法。
 
BigDecimal divide = bd1.divide(参与运算的对象,小数点后精确到多少位,舍入模式);
参数1 ,表示参与运算的BigDecimal 对象。
参数2 ,表示小数点后面精确到多少位
参数3 ,舍入模式
BigDecimal.ROUND_UP 进一法
BigDecimal.ROUND_FLOOR 去尾法
BigDecimal.ROUND_HALF_UP 四舍五入
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

Java之常用API的更多相关文章

  1. Java 之常用API(一)

    常用API  1 API概述  2 Scanner类与String类  3 StringBuilder类 NO.one API概述 1.1 API概述 API(Application Programm ...

  2. Java 基础 常用API (Object类,String类,StringBuffer类)

    Java API Java 的API(API: Application(应用) Programming(程序) Interface(接口)) Java API就是JDK中提供给我们使用的类,这些类将底 ...

  3. Java 之常用API(二)

    Object类 & System类 日期相关类 包装类 & 正则表达式 Object类 & System类 1.1 Object类 1.1.1 概述 Object类是Java语 ...

  4. java selenium常用API(WebElement、iFrame、select、alert、浏览器窗口、事件、js) 一

     WebElement相关方法 1.点击操作 WebElement button = driver.findElement(By.id("login")); button.clic ...

  5. java自学-常用api

    API(Application Programming Interface),应用程序编程接口.Java API是JDK中提供给我们使用的类的说明文档.即jdk包里边写好的类,这些类将底层的代码实现封 ...

  6. Java的常用API

    Object类 1.toString方法在我们直接使用输出语句输出对象的时候,其实通过该对象调用了其toString()方法. 2.equals方法方法摘要:类默认继承了Object类,所以可以使用O ...

  7. Java的常用API之System类简介

    Syetem类 java.lang.System类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有: public static long c ...

  8. java selenium常用API汇总

    (WebElement.iFrame.select.alert.浏览器窗口.事件.js)     一 WebElement相关方法 1.点击操作 WebElement button = driver. ...

  9. Java 基础 常用API (System类,Math类,Arrays, BigInteger,)

    基本类型包装类 基本类型包装类概述 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类 ...

随机推荐

  1. JPG学习笔记3(附完整代码)

    #topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...

  2. LVS : Linux Virtual Server 负载均衡,集群,高并发,robust

    1 LVS : Linux Virtual Server http://www.linuxvirtualserver.org/ http://www.linuxvirtualserver.org/zh ...

  3. 正则表达式 test 踩坑指南

    正则表达式 test 踩坑指南 test 只能使用一次,第二次返回的是错误结果! reg = /edg|edge/g; /edg|edge/g reg.test(`edg`) true reg.tes ...

  4. The Weekly Web Dev Challenge: String Calculator

    The Weekly Web Dev Challenge: String Calculator https://twitter.com/intent/tweet?text=I just complet ...

  5. taro taro 多端同步调试

    taro 多端同步调试 debug https://nervjs.github.io/taro/docs/envs-debug.html

  6. 「NGK每日快讯」2021.1.15日NGK公链第73期官方快讯!

  7. 基于NGK发行的稳定币USDN如何撼动市场?

    近日,基于NGK发行的稳定币USDN在各大社区的热度越来越高,很多人都说USDN将会撼动市场,那么USDN究竟有怎样的优势,能引起这么大的轰动呢?今天我们就一起来分析一下USDN. USDN是基于公链 ...

  8. 华盛顿邮报专访:SPC能否再掀起币圈新浪潮?

    近日,美国知名媒体华盛顿邮报对话NGK灵石团队技术副总裁Daphne Patel女士,对话主题为"SPC能否再掀起币圈新浪潮".此次对话以问答的形式展开,将SPC的最新情况呈现在你 ...

  9. NGK.IO新一代高倍币BGV即将登陆交易所

    据悉NGK.IO新一代高倍币BGV已与全球前十大交易所进行深度恰谈,预计在不久的将来会完成上线计划.此次BGV的上线战略布局,将进一步扩大BGV生态,赋予BGV更多的便利性和发展空间.除此之外,NGK ...

  10. 【转】奇异值分解(SVD)原理详解及推导

    原文地址:https://blog.csdn.net/zhongkejingwang/article/details/43053513,转载主要方便随时查阅,如有版权要求,请及时联系. 在网上看到有很 ...