Java常用类—Math&Random&包装类的常用方法

Math

Math类主要用来完成一些数学运算,有以下主要方法:

常用方法:

方法声明功能描述
Math.abs(double n)计算绝对值
Math.sin(double n)计算正弦值
Math.cos(double n)计算余弦值
Math.tan(double n)计算正切值
Math.sqrt(double n)计算平方根的值
Math.cbrt(double n)计算立方根的值
Math.pow(double a,double b)计算乘方的值
Math.ceil(double n)计算大于n的最小整数
Math.floor(double n)计算小于n的最大整数
Math.round(double n)对小数n进行四舍五入的结果
Math.max(double a,double b)计算两个数的最大值
Math.min(double a,double b)计算两个数的最小值
Math.random()生成一个[0.0,1.0]的随机数
Math.PIπ ≈ 3.1415926
Math.Ee ≈ 2.7182818

测试如下:

import java.lang.Math;

public class Main {
    public static void main(String[] args) {
        System.out.println("计算绝对值的结果:" + Math.abs(-1));
        System.out.println("计算正弦的结果:" + Math.sin(1.5));
        System.out.println("计算余弦的结果:" + Math.cos(2.0));
        System.out.println("计算正切的结果:" + Math.tan(0.8));
        System.out.println("计算平方根的结果:" + Math.sqrt(9));
        System.out.println("计算立方根的结果:" + Math.cbrt(8));
        System.out.println("计算大于参数的最小整数:" + Math.ceil(1.2));
        System.out.println("计算小于参数的最大整数:" + Math.floor(2.1));
        System.out.println("计算小数四舍五入的结果:" + Math.round(3.4));
        System.out.println("计算乘方的结果:" + Math.pow(2, 4));
        System.out.println("求两个数的最大值:" + Math.max(3, 5));
        System.out.println("求两个数的最小值:" + Math.min(3, 5));
        System.out.println("生成一个[0.0,1.0]的随机数:" + Math.random());
        System.out.println("π的值 ≈ " + Math.PI);
        System.out.println("e的值 ≈ " + Math.E);
    }
}

运行结果如下:

计算绝对值的结果:1
计算正弦的结果:0.9974949866040544
计算余弦的结果:-0.4161468365471424
计算正切的结果:1.0296385570503641
计算平方根的结果:3.0
计算立方根的结果:2.0
计算大于参数的最小整数:2.0
计算小于参数的最大整数:2.0
计算小数四舍五入的结果:3
计算乘方的结果:16.0
求两个数的最大值:5
求两个数的最小值:3
生成一个[0.0,1.0]的随机数:0.9553129403901677
π的值 ≈ 3.141592653589793
e的值 ≈ 2.718281828459045

Random

Random类主要是生成随机数

Random构造方法:

方法声明功能描述
Random用于创建一个随机数生成器,每次实例化Random对象都会生成不同的随机数
Random(long seed)使用单个 long 种子创建一个新的随机数生成器

Random类的常用方法:

方法声明功能描述
boolean nextBoolean()随机生成boolean类型的随机数
double nextDouble()随机生成double类型的随机数
float nextFloat()随机生成float类型的随机数
int nextInt()随机生成int类型的随机数
int nextInt(int n)随机生成[0,n)之间int类型的随机数
long nextLong()随机生成long类型的随机数

测试如下:

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random random = new Random();
        System.out.println("生成boolean类型的随机数:" + random.nextBoolean());
        System.out.println("生成double类型的随机数:" + random.nextDouble());
        System.out.println("生成float类型的随机数:" + random.nextFloat());
        System.out.println("生成int类型的随机数:" + random.nextInt());
        System.out.println("生成0到10之间int类型的随机数:" + random.nextInt(10));
        System.out.println("生成long类型的随机数:" + random.nextLong());
    }
}

运行结果如下:

生成boolean类型的随机数:true
生成double类型的随机数:0.5337956087709074
生成float类型的随机数:0.8942886
生成int类型的随机数:510684174
生成010之间int类型的随机数:9
生成long类型的随机数:-7772573334475064663

使用种子:

import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Random random1 = new Random(40);
        Random random2 = new Random(40);

        System.out.println("生成int类型的随机数:" + random1.nextInt());
        System.out.println("生成double类型的随机数:" + random1.nextDouble());
        System.out.println("生成float类型的随机数:" + random1.nextFloat());
        System.out.println("生成long类型的随机数:" + random1.nextLong());

        System.out.println("-----------------------------------");

        System.out.println("生成int类型的随机数:" + random2.nextInt());
        System.out.println("生成double类型的随机数:" + random2.nextDouble());
        System.out.println("生成float类型的随机数:" + random2.nextFloat());
        System.out.println("生成long类型的随机数:" + random2.nextLong());
    }
}

运行结果:

生成int类型的随机数:-1170874532
生成double类型的随机数:0.5927296880232136
生成float类型的随机数:0.6501284
生成long类型的随机数:8288385764484271288
-----------------------------------
生成int类型的随机数:-1170874532
生成double类型的随机数:0.5927296880232136
生成float类型的随机数:0.6501284
生成long类型的随机数:8288385764484271288

相同种子数的Random对象,对应相同次数生成的随机数字是完全相同的

包装类

Java中很多类的方法都需要接收引用类型的对象,就无法将基本类型的值传入,所以,提供了包装类

基本类型对应的包装类

基本数据类型对应的包装类
byteByte
charCharacter
intInteger
shortShort
longLong
floatFloat
doubleDouble
booleanBoolean

1、String的valueOf()方法可以将8种基本类型转换为String类型

2、包装类的valueOf()方法将基本类型或字符串转换为包装类

3、包装类的parseXxx()方法将字符串转换为基本数据类型

public class Main {
    public static void main(String[] args) {
        // 1
        int num = 123;
        String str = String.valueOf(num);
        System.out.println(str);
        // 2
        String string = "456";
        Integer integer = Integer.valueOf(string);
        System.out.println(integer);
        // 3
        int n = Integer.parseInt(string);
        System.out.println(n);
    }
}

其他

还有个常用的各种数据类型的最大最小值:

package com.study;

import java.awt.*;
import java.util.Random;

public class Main {
    public static void main(String[] args) {
        System.out.println("Byte.MAX_VALUE = " + Byte.MAX_VALUE);
        System.out.println("Byte.MIN_VALUE = " + Byte.MIN_VALUE);

        System.out.println("Short.MAX_VALUE = " + Short.MAX_VALUE);
        System.out.println("Short.MIN_VALUE = " + Short.MIN_VALUE);

        System.out.println("Integer.MAX_VALUE = " + Integer.MAX_VALUE);
        System.out.println("Integer.MIN_VALUE = " + Integer.MIN_VALUE);

        System.out.println("Long.MAX_VALUE = " + Long.MAX_VALUE);
        System.out.println("Long.MIN_VALUE = " + Long.MIN_VALUE);

        System.out.println("Float.MAX_VALUE = " + Float.MAX_VALUE);
        System.out.println("Float.MIN_VALUE = " + Float.MIN_VALUE);

        System.out.println("Double.MAX_VALUE = " + Double.MAX_VALUE);
        System.out.println("Double.MIN_VALUE = " + Double.MIN_VALUE);

        System.out.println("Character.MAX_VALUE = " + (int) Character.MAX_VALUE);
        System.out.println("Character.MIN_VALUE = " + (int) Character.MIN_VALUE);
    }
}

运行结果:

Byte.MAX_VALUE = 127
Byte.MIN_VALUE = -128
Short.MAX_VALUE = 32767
Short.MIN_VALUE = -32768
Integer.MAX_VALUE = 2147483647
Integer.MIN_VALUE = -2147483648
Long.MAX_VALUE = 9223372036854775807
Long.MIN_VALUE = -9223372036854775808
Float.MAX_VALUE = 3.4028235E38
Float.MIN_VALUE = 1.4E-45
Double.MAX_VALUE = 1.7976931348623157E308
Double.MIN_VALUE = 4.9E-324
Character.MAX_VALUE = 65535
Character.MIN_VALUE = 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值