Java- API

这篇博客介绍了Java API的基本概念,包括Math类的常用方法如求绝对值和向上取整,以及包装类的功能,如将基本类型转换为对象。文章还详细展示了如何进行int和String类型之间的转换,提供了直接拼接和使用valueOf方法两种方式。此外,通过实例演示了如何将字符串数据排序,以及如何递归计算阶乘。
摘要由CSDN通过智能技术生成

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);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值