API (Application Programming Interface) :应用程序编程接口
java中的API
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() |
package Nomal_API.APIdemo;
public class MathDemo {
public static void main(String[] args) {
// public static int abs(int a); //返回参数的绝对值
int abs = Math.abs(10);
System.out.println("abs的绝对值为:"+abs);
// public static double ceil(double a); //向上取整
double ceil = Math.ceil(10.1);
System.out.println("ceil的向上取整:"+ceil);
// public static double floor(double a); //向下取整
double floor = Math.floor(10.9);
System.out.println("floor的向下取整为:"+floor);
// public static int round(float a); //四舍五入
long round = Math.round(10.1);
System.out.println("round的四舍五入为:"+round);
long round1 = Math.round(1.9);
System.out.println("round1的四舍五入为:"+round1);
// public static int max(int a,int b); //返回两个int值中的较大值
int max = Math.max(10, 20);
System.out.println("max的最大值为:"+max);
// public static int min(int a,int b); //返回两个int值中的较小值
int min = Math.min(10, 20);
System.out.println("min的最小值为:"+min);
// public static double pow(double a,double b); //返回a的b次幂的值
double pow = Math.pow(2, 3);
System.out.println("pow的幂次方为:"+pow);
// public static double random(); //返回值为double的正值,[0.0,1.0)
System.out.println("随机值【double类型】");
for (int i = 0; i < 10 ; i++) {
double random = Math.random();
System.out.println(random);
}
}
}
| 方法名 | 说明 |
|---|---|
| public static void exit(int status) | 终止当前运行的 Java 虚拟机,非零表示异常终止 |
| public static long currentTimeMillis() |
package Nomal_API.APIdemo;
public class Systemdemo {
public static void main(String[] args) {
// 获取开始的时间节点
long start = System.currentTimeMillis();
System.out.println("开始时间为"+start);
int i;
for (i = 1; i <= 10000; i++) {
int c = 0;
c += i;
}
// 获取代码运行结束后的时间节点
long end = System.currentTimeMillis();
System.out.println("结束时间为:"+end);
System.out.println("循环"+i+"共耗时:" + (end - start) + "毫秒");
}
}
Object 是类层次结构的根,每个类都可以将 Object 作为超类。所有类都直接或者间接的继承自该类,换句话说,该类所具备的方法,所有类都会有一份
查看方法源码的方式
选中方法,按下Ctrl + B
重写toString方法的方式
Alt + Insert 选择toString
在类的空白区域,右键 -> Generate -> 选择toString
toString方法的作用:
用于对象之间的比较,返回true和false的结果
举例:s1.equals(s2); s1和s2是两个对象
重写equals方法的场景
不希望比较对象的地址值,想要结合对象属性进行比较的时候。
重写equals方法的方式
alt + insert 选择equals() and hashCode(),IntelliJ Default,一路next,finish即可
package Nomal_API.APIdemo;
//让student类继承祖宗类object,不写也是默认继承他
class Student extends Object {
private String name;
private int age;
//生成空参构造方法
public Student() {
}
//生成有参构造方法
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//生成get方法
public String getName() {
return name;
}
//生成set方法
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//重写object祖宗类的toString方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//重写equals方法,如果不重写equals方法,那么他们的比较是比较地址是否相同,跟 ‘ = = ‘ 是同样的效果
@Override
public boolean equals(Object o) {
//this -- s ,this代表的是调用者
//o -- s2 , o代表的是与调用者相比较的对象
if (this == o) return true; // 如果他们地址相等的话,返回TRUE
// getClass()方法的作用:通过返回的Class对象获取Person的相关信息,比如:获取Person的构造方法,方法,属性有哪些等等信息。
if (o == null || getClass() != o.getClass()) return false; // 如果被比较者为空,或者他们的类对象不相等,则返回为FALSE
//将o对象赋为student,
Student student = (Student) o; //student -- s2
// 如果调用者的age不等于被比较者的age,返回为FALSE
if (age != student.age) return false;
// 三元比较:name不等于null为TRUE-->调用者与被比较者比较,否则被比较者的name也为null
return name != null ? name.equals(student.name) : student.name == null;
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s = new Student();
s.setName("李思思");
s.setAge(30);
System.out.println(s);
// 打印重写的toString方法
System.out.println(s.toString());
Student s2 = new Student();
s2.setName("李思思");
s2.setAge(30);
//需求:比较两个对象的内容是否相同,重写之后比较他们的内容是否相同,而不是比较地址了
System.out.println(s.equals(s2));
}
}
| 方法名 | 说明 |
|---|---|
| public static String toString (对象) | 返回参数中对象的字符串表示形式。 |
| public static String toString(对象, 默认字符串) | 返回对象的字符串表示形式。 |
| public static Boolean isNull(对象) | 判断对象是否为空 |
| public static Boolean nonNull(对象) |
package Nomal_API.APIdemo.objects;
package Nomal_API.APIdemo.objects;
class Student {
private String name;
private int age;
public Student() {
}
public Student(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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
import java.util.Objects;
public class MyObjectsDemo {
public static void main(String[] args) {
// public static String toString(对象): 返回参数中对象的字符串表示形式。
Student s1 = new Student("小小同学",50);
String result1 = Objects.toString(s1);
System.out.println(result1); // 打印toString方法
System.out.println(s1);
// public static String toString(对象, 默认字符串): 返回对象的字符串表示形式。如果对象为空,那么返回第二个参数.
Student s2 = new Student("小花同学",23);
Student s3 = null;
String result2 = Objects.toString(s3, "随便写一个");
System.out.println(result2);
// public static Boolean isNull(对象): 判断对象是否为空
Student s4 = null;
Student s5 = new Student();
boolean result3 = Objects.isNull(s5);
System.out.println("对象是否为空:"+result3);
// public static Boolean nonNull(对象): 判断对象是否不为空
Student s6 = new Student();
Student s7 = null;
boolean result4 = Objects.nonNull(s6); // 判断对象是否不为空
System.out.println("对象是否不为空"+ result4);
}
}
可以用来进行精确计算
| 方法名 | 说明 |
|---|---|
| BigDecimal(double val) | 参数为double |
| BigDecimal(String val) |
| 说明 | |
|---|---|
| public BigDecimal add(另一个BigDecimal对象) | 加法 |
| public BigDecimal subtract (另一个BigDecimal对象) | 减法 |
| public BigDecimal multiply (另一个BigDecimal对象) | 乘法 |
| public BigDecimal divide (另一个BigDecimal对象) | 除法 |
| public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式) |
BigDecimal是用来进行精确计算的
创建BigDecimal的对象,构造方法使用参数类型为字符串的。
BigDecimal divide = bd1.divide(参与运算的对象,小数点后精确到多少位,舍入模式);
参数1 ,表示参与运算的BigDecimal 对象。
参数2 ,表示小数点后面精确到多少位
参数3 ,舍入模式
BigDecimal.ROUND_UP 进一法
BigDecimal.ROUND_FLOOR 去尾法
BigDecimal.ROUND_HALF_UP 四舍五入
package Nomal_API.APIdemo.bigdecimal;
import java.math.BigDecimal;
public class BigdecimalDemo {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("10.0");
BigDecimal bd2 = new BigDecimal("0.4");
System.out.println("字符串数字"+bd1);
System.out.println(bd2);
// BigDecimal bd1 = new BigDecimal(0.1);
// BigDecimal bd2 = new BigDecimal(0.2);
BigDecimal bd3 = new BigDecimal("0.1");
BigDecimal bd4 = new BigDecimal("0.2");
// public BigDecimal add(另一个BigDecimal对象) 加法
BigDecimal add = bd3.add(bd4); // 加法
System.out.println("和为" + add);
//System.out.println(0.1 + 0.2);
// public BigDecimal subtract (另一个BigDecimal对象) 减法
BigDecimal subtract = bd3.subtract(bd4);
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);
}
}
import java.math.BigDecimal;
public class MyBigDecimalDemo4 {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("0.3");
BigDecimal bd2 = new BigDecimal("4"); //0.075
/* BigDecimal divide = bd1.divide(bd2);
System.out.println(divide);*/
//参数一:表示参数运算的另一个对象
//参数二:表示小数点后精确到多少位
//参数三:舍入模式
//进一法 BigDecimal.ROUND_UP
//去尾法 BigDecimal.ROUND_FLOOR
//四舍五入 BigDecimal.ROUND_HALF_UP
BigDecimal divide = bd1.divide(bd2, 2, BigDecimal.ROUND_HALF_UP);
System.out.println(divide);
}
}
基本类型包装类的作用
将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据
常用的操作之一:用于基本数据类型与字符串之间的转换
基本类型对应的包装类
| 包装类 | |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean |
| 说明 | |
|---|---|
| public Integer(int value) | 根据 int 值创建 Integer 对象(过时) |
| public Integer(String s) | 根据 String 值创建 Integer 对象(过时) |
| public static Integer valueOf(int i) | 返回表示指定的 int 值的 Integer 实例 |
| public static Integer valueOf(String s) |
自动装箱
把基本数据类型转换为对应的包装类类型
自动拆箱
把包装类类型转换为对应的基本数据类型
package Nomal_API.APIdemo.basicInterger;
public class IntergerDemo3 {
public static void main(String[] args) {
Integer i1 = 100;
// 对象 = 默认是一个基本数据类型
// jdk1.5的特性 --- 自动装箱
//装箱: 把一个基本数据类型 变量对应的包装类.
//自动: Java底层会帮我们自动的调用valueof方法.
System.out.println("自动装箱i1为interger:"+i1);
//jdk1.5的特性 --- 自动拆箱
//拆箱: 把一个包装类型 变成对应的基本数据类型
int i2 = i1;
System.out.println("自动拆箱i2为int:"+i2);
Integer i3 = 100; //自动装箱机制
i3 += 200;//i3 = i3 + 200;
//会把i3这个对象变成基本数据类型100.
//100 + 200 = 300
//把基本数据类型300再次自动装箱变成Integer对象赋值给i3
System.out.println("自动拆装箱,最后i3为Interger类型:"+i3);
//细节:
Integer i4 = null;
if (i4 != null) {
i4 += 200;
System.out.println(i4);
}
}
}
查找指定元素在数组中的位置时,以前的方式是通过遍历,逐个获取每个元素,看是否是要查找的元素,这种方式当数组元素较多时,查找的效率很低
前提条件
数组内的元素一定要按照大小顺序排列,如果没有大小顺序,是不能使用二分查找法的
二分查找的实现步骤
1,定义两个变量,表示要查找的范围。默认min = 0 , max = 最大索引
2,循环查找,但是min <= max
3,计算出mid的值
4,判断mid位置的元素是否为要查找的元素,如果是直接返回对应索引
5,如果要查找的值在mid的左半边,那么min值不变,max = mid -1.继续下次循环查找
6,如果要查找的值在mid的右半边,那么max值不变,min = mid + 1.继续下次循环查找
7,当 min > max 时,表示要查找的元素在数组中不存在,返回-1.
package Nomal_API.APIdemo.MyBinarySearchDemo;
public class MybinarySearchDemo {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int number = 11;
//1,现在要干嘛? --- 二分查找
//2.这件事情需要什么? --- 数组 元素
//3,干完了, 要不要把结果返回调用者 --- 把索引返回给调用者
int index = binarySearchForIndex(arr, number);
System.out.println(index);
}
private static int binarySearchForIndex(int[] arr, int number) {
//1,定义查找的范围
int min = 0;
int max = arr.length - 1;
//2.循环查找 min <= max
while (min <= max) {
//3.计算出中间位置 mid
int mid = (min + max) >> 1;
//mid指向的元素 > number
if (arr[mid] > number) {
//表示要查找的元素在左边.
max = mid - 1;
} else if (arr[mid] < number) {
//mid指向的元素 < number
//表示要查找的元素在右边.
min = mid + 1;
} else {
//mid指向的元素 == number
return mid;
}
}
//如果min大于了max就表示元素不存在,返回-1.
return -1;
}
}

把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解

package Nomal_API.APIdemo.MyFactorialDemo;
//递归:求阶乘
public class factorialDemo2 {
public static void main(String[] args) {
//调用方法
int result = jc(5);
//输出结果
System.out.println("5的阶乘是:" + result);
}
//定义一个方法,用于递归求阶乘,参数为一个int类型的变量
public static int jc(int n) {
//在方法内部判断该变量的值是否是1
if (n == 1) {
//是:返回1
return 1;
} else {
//不是:返回n*(n-1)!
return n * jc(n - 1);
}
}
}
冒泡排序概述
一种排序的方式,对要进行排序的数据中相邻的数据进行两两比较,将较大的数据放在后面,依次对所有的数据进行操作,直至所有数据按要求完成排序
如果有n个数据进行排序,总共需要比较n-1次
每一次比较完毕,下一次的比较就会少一个数据参与
package Nomal_API.APIdemo.BubbleSortDemo;
//冒泡排序
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {3, 5, 2, 1, 4};
//1 2 3 4 5
bubbleSort(arr);
}
private static void bubbleSort(int[] arr) {
//外层循环控制的是次数 比数组的长度少一次.
for (int i = 0; i < arr.length - 1; i++) {
//内存循环就是实际循环比较的
//-1 是为了让数组不要越界
//-i 每一轮结束之后,我们就会少比一个数字.
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printArr(arr);
}
private static void printArr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Arrays的常用方法
| 说明 | |
|---|---|
| public static String toString(int[] a) | 返回指定数组的内容的字符串表示形式 |
| public static void sort(int[] a) | 按照数字顺序排列指定的数组 |
| public static int binarySearch(int[] a, int key) |
构造方法用 private 修饰
package Nomal_API.APIdemo.ArrayDemo;
import java.util.Arrays;
public class MyArraysDemo {
public static void main(String[] args) {
// public static String toString(int[] a) 返回指定数组的内容的字符串表示形式
int [] arr1 = {3,2,4,6,7};
System.out.println("返回数组的内容字符串:"+Arrays.toString(arr1));
// public static void sort(int[] a) 按照数字顺序排列指定的数组
int [] arr2 = {3,2,4,6,7};
Arrays.sort(arr2);
System.out.println("进行从小到大顺序排序:"+Arrays.toString(arr2));
// public static int binarySearch(int[] a, int key) 利用二分查找返回指定元素的索引:key为要查找的值
int[] arr3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int index = Arrays.binarySearch(arr3, 0);
System.out.println(index);
//1,数组必须有序
//2.如果要查找的元素存在,那么返回的是这个元素实际的索引
//3.如果要查找的元素不存在,那么返回的是 (-插入点-1)
//插入点:如果这个元素在数组中,他应该在哪个索引上.
}
} 大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
我正在使用Mandrill的RubyAPIGem并使用以下简单的测试模板:testastic按照Heroku指南中的示例,我有以下Ruby代码:require'mandrill'm=Mandrill::API.newrendered=m.templates.render'test-template',[{:header=>'someheadertext',:main_section=>'Themaincontentblock',:footer=>'asdf'}]mail(:to=>"JaysonLane",:subject=>"TestEmail")do|format|format.h