Java Number & Math 类

一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte、int、long、double 等。

然而,在实际开发过程中,我们经常会遇到需要使用对象,而不是内置数据类型的情形。为了解决这个问题,Java 语言为每一个内置数据类型提供了对应的包装类。

所有的包装类(Integer、Long、Byte、Double、Float、Short)都是抽象类 Number 的子类。

包装类 基本数据类型
Boolean boolean

Byte

byte
Short short
Integer int
Long long
Character char
Float float
Double double

Java Number类

这种由编译器特别支持的包装称为装箱,所以当内置数据类型被当作对象使用的时候,编译器会把内置类型装箱为包装类。相似的,编译器也可以把一个对象拆箱为内置类型。Number 类属于 java.lang 包。

下面是一个使用 Integer 对象的实例:

Test.java 文件代码:

public class Test{

public static void main(String[] args){
Integer x = 5;
x = x + 10;
System.out.println(x);
}
}

以上实例编译运行结果如下:

15

当 x 被赋为整型值时,由于x是一个对象,所以编译器要对x进行装箱。然后,为了使x能进行加运算,所以要对x进行拆箱。


Java Math 类

Java 的 Math 包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。

Math 的方法都被定义为 static 形式,通过 Math 类可以在主函数中直接调用。

Test.java 文件代码:

public class Test {
public static void main (String []args)
{
System.out.println(90 度的正弦值: + Math.sin(Math.PI/2));
System.out.println(0度的余弦值: + Math.cos(0));
System.out.println(60度的正切值: + Math.tan(Math.PI/3));
System.out.println(1的反正切值: + Math.atan(1));
System.out.println(π/2的角度值: + Math.toDegrees(Math.PI/2));
System.out.println(Math.PI);
}
}

以上实例编译运行结果如下:

90 度的正弦值:1.0
0度的余弦值:1.0
60度的正切值:1.7320508075688767
1的反正切值: 0.7853981633974483
π/2的角度值:90.0
3.141592653589793

Number & Math 类方法

下面的表中列出的是 Number & Math 类常用的一些方法:


Math 的 floor,round 和 ceil 方法实例比较

参数 Math.floor Math.round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1

floor,round 和 ceil 实例:

public class Main {
public static void main(String[] args) {
double[] nums = { 1.4, 1.5, 1.6, –1.4, –1.5, –1.6 };
for (double num : nums) {
test(num);
}
}

private static void test(double num) {
System.out.println(Math.floor( + num + )= + Math.floor(num));
System.out.println(Math.round( + num + )= + Math.round(num));
System.out.println(Math.ceil( + num + )= + Math.ceil(num));
}
}

以上实例执行输出结果为:

Math.floor(1.4)=1.0
Math.round(1.4)=1
Math.ceil(1.4)=2.0
Math.floor(1.5)=1.0
Math.round(1.5)=2
Math.ceil(1.5)=2.0
Math.floor(1.6)=1.0
Math.round(1.6)=2
Math.ceil(1.6)=2.0
Math.floor(-1.4)=-2.0
Math.round(-1.4)=-1
Math.ceil(-1.4)=-1.0
Math.floor(-1.5)=-2.0
Math.round(-1.5)=-1
Math.ceil(-1.5)=-1.0
Math.floor(-1.6)=-2.0
Math.round(-1.6)=-2
Math.ceil(-1.6)=-1.0

若文章对你有帮助,可以点赞或打赏支持我们。发布者:lyh会员,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/java-number-math-%e7%b1%bb/

(0)
lyhlyh会员认证作者
上一篇 2023年 3月 4日 下午9:19
下一篇 2023年 3月 4日 下午9:21

相关推荐

  • echo

    文章目录echo补充说明语法选项参数实例 echo 输出指定的字符串或者变量 补充说明 echo命令 用于在shell中打印shell变量的值,或者直接输出指定的字符串。linux…

    入门教程 2023年 12月 14日
  • scriptreplay

    文章目录scriptreplay补充说明语法选项参数实例 scriptreplay 重新播放终端会话的所有操作 补充说明 scriptreplay 用于在终端中,根据 script…

    入门教程 2024年 3月 4日
  • Helm | Helm 展示

    文章目录helm show简介可选项从父命令继承的命令请参阅 helm show 显示chart信息 简介 该命令由多条子命令组成来显示chart的信息 可选项 从父命令继承的命令…

    入门教程 2023年 12月 14日
  • mkbootdisk

    文章目录mkbootdisk补充说明语法选项参数实例 mkbootdisk 可建立目前系统的启动盘 补充说明 mkbootdisk命令 用来为当前运行的系统创建能够单独使用的系统引…

    入门教程 2024年 1月 3日
  • lvscan

    文章目录lvscan补充说明语法选项实例 lvscan 扫描逻辑卷 补充说明 lvscan命令 用于扫描当前系统中存在的所有的LVM逻辑卷。使用lvscan指令可以发现系统中的所有…

    入门教程 2023年 12月 19日
  • alias

    文章目录alias概要主要用途选项返回值例子知识点错误用法Q&A注意其他参考链接 alias 定义或显示别名。 概要 alias [-p] [name[=value] ..…

    入门教程 2023年 12月 6日
  • arch

    文章目录arch概要主要用途选项例子注意 arch 显示当前主机的硬件架构类型 概要 arch [OPTION]… 主要用途 打印机器架构信息;arch 命令输出结果有:i38…

    入门教程 2023年 12月 6日
  • smartmontools

    文章目录smartmontools安装语法选项参数实例以指定的间隔运行,同时又能通知硬盘的测试结果 smartmontools Smartmontools 是一种硬盘检测工具,通过…

    入门教程 2024年 3月 5日
  • bzcat

    文章目录bzcat补充说明语法参数实例 bzcat 不解压,直接查看指定的.bz2文件 补充说明 bzcat命令 无需解压缩指定的.bz2文件,即可显示解压缩后的文件内容。 语法 …

    入门教程 2023年 12月 6日
  • pvremove

    文章目录pvremove补充说明语法选项参数实例 pvremove 删除一个存在的物理卷 补充说明 pvremove命令 用于删除一个存在的物理卷。使用pvremove指令删除物理…

    入门教程 2024年 3月 1日
Translate »