欢迎访问宙启技术站
智能推送

Java核心函数库:Math类、Random类、String类等

发布时间:2023-06-15 01:21:00

Java核心函数库是指Java内置的一些常用的类和函数,这些函数库主要用于实现常用的算法、数学运算、字符串操作等功能。其中,Math类、Random类、String类是常用的三个核心函数库。

一、Math类

Math类是Java中的一个常用的类,它包含了许多与数学相关的静态方法,可以直接使用而无需实例化对象。Math类的常用的方法包括:

1. abs方法:取绝对值

2. ceil方法:向上取整

3. floor方法:向下取整

4. pow方法:求幂

5. sqrt方法:求平方根

6. max方法:求最大值

7. min方法:求最小值

8. round方法:四舍五入

9. random方法:生成随机数

Math类的使用示例:

public class MathDemo {

    public static void main(String[] args) {

    

        System.out.println(Math.abs(-5)); // 5

        

        System.out.println(Math.ceil(5.5)); // 6.0

        

        System.out.println(Math.floor(5.5)); // 5.0

        

        System.out.println(Math.pow(2, 3)); // 8.0

        

        System.out.println(Math.sqrt(4)); // 2.0

        

        System.out.println(Math.max(2, 3)); // 3

        

        System.out.println(Math.min(2, 3)); // 2

        

        System.out.println(Math.round(5.5)); // 6

        

        System.out.println(Math.random()); // 生成一个0到1之间的随机数

        

    }

}

二、Random类

Random类是Java提供的一个用于生成随机数的类,它包含了许多方法,可以用于生成不同类型的随机数。Random类的常用方法包括:

1. nextInt方法:生成指定范围内的随机整数

2. nextDouble方法:生成0到1之间的随机小数

3. nextBoolean方法:生成随机的true或false

4. setSeed方法:设置随机生成器的种子

Random类的使用示例:

public class RandomDemo {

    public static void main(String[] args) {

    

        // 创建新的随机数生成器

        Random rand = new Random();

        

        // 生成一个0到100之间的随机整数

        int randomNumber = rand.nextInt(100);

        System.out.println("随机整数:" + randomNumber);

        

        // 生成一个0到1之间的随机小数

        double randomDouble = rand.nextDouble();

        System.out.println("随机小数:" + randomDouble);

        

        // 生成随机的true或false

        boolean randomBoolean = rand.nextBoolean();

        System.out.println("随机布尔值:" + randomBoolean);

        

        // 设置随机生成器的种子

        rand.setSeed(12345678);

        

        // 重新生成随机整数

        int newRandomNumber = rand.nextInt(100);

        System.out.println("重新生成的随机整数:" + newRandomNumber);

        

    }

}

三、String类

String类是Java中最常用的一个类,它用于表示字符串,并提供了常用的字符串操作方法。String类的常用方法包括:

1. length方法:获取字符串长度

2. equals方法:判断两个字符串是否相等

3. charAt方法:获取指定位置的字符

4. substring方法:提取子字符串

5. indexOf方法:查找指定字符串在原字符串中的位置

6. replace方法:替换指定字符串

7. toUpperCase方法:将字符串转换为大写

8. toLowerCase方法:将字符串转换为小写

String类的使用示例:

public class StringDemo {

    public static void main(String[] args) {

    

        String str = "Hello, World!";

        System.out.println("字符串长度:" + str.length());

        

        String str1 = "Hello, World!";

        String str2 = "hello, world!";

        System.out.println("字符串是否相等:" + str1.equals(str2));

        

        System.out.println("第7个字符是:" + str.charAt(6));

        

        String subStr = str.substring(7);

        System.out.println("提取子字符串:" + subStr);

        

        int index = str.indexOf(",");

        System.out.println("逗号的位置为:" + index);

        

        String replaceStr = str.replace(",", "/");

        System.out.println("替换后的字符串:" + replaceStr);

        

        String upperCaseStr = str.toUpperCase();

        System.out.println("大写字符串:" + upperCaseStr);

        

        String lowerCaseStr = str.toLowerCase();

        System.out.println("小写字符串:" + lowerCaseStr);

        

    }

}

总结

Math类、Random类、String类是Java中常用的三个核心函数库。Math类包含了许多与数学相关的静态方法,可以直接使用;Random类可以用于生成随机数;String类是Java中最常用的一个类,用于表示字符串,并提供了常用的字符串操作方法。熟练掌握这些函数库的使用,对Java开发人员来说非常重要。