1 什么是API ?
API (Application Programming interface) 应用程序编程接口
2 java 中的API
指JDK 中提供的各种功能的java 类
Math 类 :包含执行基本数字运算的方法
Math 中方法的调用方式
Math 类中无构造方法 但内部的方法都是静态的 则可以 类名. 进行调用
public static int abs(int a) 返回参数的绝对值
public static double ceil (double a) 返回大于或等于参数的最小double值,等于一个整数
包装类
基本类型包装类的作用
将基本数据类型封装成对象的好处在于在对象中定义更多的功能方法
操作该数据
常用操作之一 : 用于基本数据类型与字符串之间的转换
基本类型对应的包装类
byte Byte
short Short
long Long
float Float
double Double
char Character
boolean Boolean
2 Integer
方法
public Integer (int value)
public Integer(String s)
public static Integer valueOf(int i)
public static Integer valueOf(St)
3 int 和String 类型的互相转换
int 转换为 String
转换方式
方式一 :直接在数字后加一个空字符串
方式二 :通过String类静态方法valueOf()
public class IntegerDemo {
public static void main(String[] args) {
//int --- String
int number = 100;
//方式1
String s1 = number + "";
System.out.println(s1);
//方式2
//public static String valueOf(int i)
String s2 = String.valueOf(number);
System.out.println(s2);
System.out.println("--------");
}
}
String 转换为int
一:先将字符串数字转成Integer 在调用 valueOf 方法
二:通过Intger静态方法parsseInt 进行转换
public class IntegerDemo {
public static void main(String[] args) {
//String --- int
String s = "100";
//方式1:String --- Integer --- int
Integer i = Integer.valueOf(s);
//public int intValue()
int x = i.intValue();
System.out.println(x);
//方式2
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println(y);
}
}
4 字符串数据排序案例
案例需求:
有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果是:27 38 46 50 9
public class IntegerTest {
public static void main(String[] args) {
//定义一个字符串
String s = "91 27 46 38 50";
//把字符串中的数字数据存储到一个int类型的数组中
String[] strArray = s.split(" ");
// for(int i=0; i<strArray.length; i++) {
// System.out.println(strArray[i]);
// }
//定义一个int数组,把 String[] 数组中的每一个元素存储到 int 数组中
int[] arr = new int[strArray.length];
for(int i=0; i<arr.length; i++) {
arr[i] = Integer.parseInt(strArray[i]);
}
//对 int 数组进行排序
Arrays.sort(arr);
for(int i=0; i<arr.length; i++){
System.out.print(arr[i] + " ");
}
}
2
案例需求
用递归求5的阶乘,并把结果在控制台输出
public class DiGuiDemo01 {
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);
}
}
}