java math abs_Java Math类静态长abs(long l)方法与示例

java math abs

数学课静态长abs(长l) (Math Class static long abs(long l))

  • This method is available in java.lang package.

    此方法在java.lang包中可用。

  • This method is used to return the absolute value of the given parameter in the method.

    此方法用于返回方法中给定参数的绝对值。

  • This is a static method so this method is accessible with classname too.

    这是一个静态方法,因此也可以使用类名访问此方法。

  • The return type of this method depends on given argument datatype and here we are passing long data type in the method that means the return type of this method is long.

    此方法的返回类型取决于给定的参数数据类型,此处我们在该方法中传递long数据类型,这意味着该方法的返回类型为long。

  • In this method, we pass only one parameter as an argument in the method of Math class.

    在此方法中,我们仅将一个参数作为参数传递给Math类的方法。

  • This method does not throw any exception.

    此方法不会引发任何异常。

  • In this method, if the given argument is equal to the value of long.MIN_VALUE it returns the same negative value.

    在此方法中,如果给定参数等于long.MIN_VALUE的值, 则它将返回相同的负值。

  • This is an overridable method so various versions of this method are available but here we are looking long datatype argument so the following syntax is given below,

    这是一个可重写的方法,因此可以使用该方法的各种版本,但是在这里我们正在寻找long数据类型参数,因此以下语法如下:

Syntax:

句法:

    public static long abs(long l){
    }

Parameter(s):

参数:

long l – a long value whose absolute value to be found.

long l-一个要确定其绝对值的long值。

Return value:

返回值:

The return type of this method is long, returns absolute value as a long.

该方法的返回类型为long ,将绝对值返回为long。

Note:

注意:

  • If we pass a positive long integer, it returns the same value.

    如果我们传递一个正长整数,它将返回相同的值。

  • If we pass a negative long integer, it returns the value without sign i.e. it returns the positive long integer.

    如果我们传递一个负长整数,它将返回不带符号的值,即它将返回正长整数。

  • If we pass positive zero (0) or negative zero (-0), it returns zero without sign (0).

    如果我们传递正零(0)或负零(-0),则它将返回零而没有符号(0)。

  • If we pass an infinity parameter, it returns exception.

    如果我们传递一个无穷大参数,它将返回异常。

  • If we pass "NaN" (Not A Number), it returns the same i.e. a "NaN".

    如果我们传递“ NaN”(非数字),则返回相同的值,即“ NaN”。

演示abs(long l)方法示例的Java程序 (Java program to demonstrate example of abs(long l) method)

// Java program to demonstrate the example  of 
// abs(long l) method of Math class

class AbsLongTypeMethod {
    public static void main(String[] args) {
        // We are declaring few variables
        long a = 123121l;
        long b = -123121l;
        long c = 0l;
        long d = -0l;
        /*
        long e = 74587l/0l;
        long f = -7458l/0l;
        */

        // By using abs(long l) method we will find  the 
        // absolute value of given parameter in the method

        System.out.println("The absolute value of a is : " + Math.abs(a));
        System.out.println("The absolute value of b is : " + Math.abs(b));
        System.out.println("The absolute value of c is : " + Math.abs(c));
        System.out.println("The absolute value of d is : " + Math.abs(d));

        /* 
        // In the below code exception will be thrown 
        // because we are passing infinity
        System.out.println("The absolute value of e is : "+Math.abs(e));
        System.out.println("The absolute value of f is : "+Math.abs(f));
        */
    }
}

Output

输出量

E:\Programs>javac AbsLongTypeMethod.java

E:\Programs>java AbsLongTypeMethod
The absolute value of a is : 123121
The absolute value of b is : 123121
The absolute value of c is : 0
The absolute value of d is : 0


翻译自: https://www.includehelp.com/java/math-class-static-long-abs-long-l-method-with-example.aspx

java math abs

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值