Java Number类

来源:互联网 发布:口风琴基础教程软件 编辑:程序博客网 时间:2024/06/04 18:41

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

实例

int a = 5000;float b = 13.65;byte c = 0x4a;

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

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

Java Number类

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

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

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进行拆箱。


Number 方法

下面的表中列出的是 Number 子类实现的方法:

序号方法与描述1xxxValue()
将number对象转换为xxx数据类型的值并返回。2compareTo()
将number对象与参数比较。3equals()
判断number对象是否与参数相等。4valueOf()
返回一个 Number 对象指定的内置数据类型5toString()
以字符串形式返回值。6parseInt()
将字符串解析为int类型。7abs()
返回参数的绝对值。8ceil()
对整形变量向左取整,返回类型为double型。9floor()
对整型变量向右取整。返回类型为double类型。10rint()
返回与参数最接近的整数。返回类型为double。11round()
返回一个最接近的int、long型值。12min()
返回两个参数中的最小值。13max()
返回两个参数中的最大值。14exp()
返回自然数底数e的参数次方。15log()
返回参数的自然数底数的对数值。16pow()
返回第一个参数的第二个参数次方。17sqrt()
求参数的算术平方根。18sin()
求指定double类型参数的正弦值。19cos()
求指定double类型参数的余弦值。20tan()
求指定double类型参数的正切值。21asin()
求指定double类型参数的反正弦值。22acos()
求指定double类型参数的反余弦值。23atan()
求指定double类型参数的反正切值。24atan2()
将笛卡尔坐标转换为极坐标,并返回极坐标的角度值。25toDegrees()
将参数转化为角度。26toRadians()
将角度转换为弧度。27random()
返回一个随机数。

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'Microsoft Yahei', ΢���ź�, ����; font-size: 13px; line-height: 19.5px;"><img src="http://img.blog.csdn.net/20161023143315315?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" style="border: none; max-width: 100%;" /></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'Microsoft Yahei', ΢���ź�, ����; font-size: 13px; line-height: 19.5px;"><span style="color: rgb(51, 51, 51); text-align: justify; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 13.3333px; line-height: 24px;">喜欢我的文章的,可以关注</span><a target=_blank target="_blank" href="http://lib.csdn.net/base/wechat" class="replace_word" title="微信开发知识库" style="color: rgb(223, 52, 52); text-decoration: none; text-align: justify; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 13.3333px; line-height: 24px; font-weight: bold;"></a><a target=_blank target="_blank" href="http://lib.csdn.net/base/wechat" class="replace_word" title="微信开发知识库" style="color: rgb(223, 52, 52); text-decoration: none; font-size: 14px; line-height: 26px; text-align: justify; font-family: sans-serif; font-weight: bold;"></a><a target=_blank target="_blank" href="http://lib.csdn.net/base/wechat" class="replace_word" title="微信开发知识库" style="color: rgb(223, 52, 52); text-decoration: none; font-size: 14px; font-family: simsun; line-height: 21px; font-weight: bold;"></a><a target=_blank target="_blank" href="http://lib.csdn.net/base/wechat" class="replace_word" title="微信开发知识库" style="color: rgb(223, 52, 52); text-decoration: none; font-size: 14px; font-family: 'PingFang SC', Helvetica, 'Helvetica Neue', 微软雅黑, Tahoma, Arial, sans-serif; line-height: 19.2px; font-weight: bold;"></a><a target=_blank target="_blank" href="http://lib.csdn.net/base/wechat" class="replace_word" title="微信开发知识库" style="color: rgb(223, 52, 52); text-decoration: none; font-family: tahoma, arial, 宋体; font-size: 14px; line-height: 25.2px; font-weight: bold;"></a><a target=_blank target="_blank" href="http://lib.csdn.net/base/wechat" class="replace_word" title="微信开发知识库" style="color: rgb(223, 52, 52); text-decoration: none; font-weight: bold;"></a><a target=_blank href="http://lib.csdn.net/base/wechat" class="replace_word" title="微信开发知识库" target="_blank" style="color: rgb(223, 52, 52); text-decoration: none; font-weight: bold;">微信</a><span style="color: rgb(51, 51, 51); text-align: justify; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 13.3333px; line-height: 24px;">公众号“测试项目开发”,需要什么内容可以在里面提,我看到后会给大家解答。</span></p>

0 0
原创粉丝点击