符号函数也称为符号函数,是一种奇数函数,可提取实数的符号。
java.lang.Long.signum()方法用于获取指定的long值的signum函数。对于正值,负值和零,该方法分别返回1,-1和0。
用法:
public static int signum(long num)
参数:该方法接受一个长类型的参数num,对其执行符号运算。
返回值:该方法返回指定的long值的signum函数。如果指定的值是:
负数,该方法返回-1。
零,该方法返回0。
正数,该方法返回1。
例子:
Input:(Long) 2731766
Output:1
Input:(Long) -233611
Output:-1
Input:(Long) 0
Output:0
以下程序说明了Java.lang.Long.signum()方法:
程序1:
// Java program to illustrate the
// Java.lang.Long.signum() Method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
// It will return 1 as long value is greater than 1
System.out.println(Long.signum(36565531));
// It will return -1 as long value is less than 1
System.out.println(Long.signum(-628127));
// Returns 0 as long value is equal to 0
System.out.println(Long.signum(0));
}
}
输出:
1
-1
0
程序2:用于十进制值和字符串。
// Java program to illustrate the
// Java.lang.Long.signum() Method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
// It will return compile time error
System.out.println(Long.signum(36565.531));
// It will return compile time error
System.out.println(Long.signum("628127"));
}
}
输出:
prog.java:10:error:incompatible types:possible lossy conversion from double to long
System.out.println(Long.signum(36565.531));
^
prog.java:13:error:incompatible types:String cannot be converted to long
System.out.println(Long.signum("628127"));
^
Note:Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors