Java Integer signum()方法
signum函数是一个提取实数符号的奇数数学函数。signum函数也被称为符号函数。
java.lang的Integer.signum()方法返回指定整数值的符号函数。对于正值、负值和零,该方法分别返回1、-1和0。
语法:
public static int signum(int a)
参数: 该方法需要一个整数类型的参数 a ,要对其进行操作。
返回值: 该方法返回指定整数值的signum函数。如果指定的值是负的,则返回-1,如果指定的值是0,则返回0,如果指定的值是正的,则返回1。
例子
Consider an integer a = 27Since 27 is a positive int signum will return= 1Consider another integer a = -61Since -61 is a negative int signum will return= -1Consider the integer value _a_ = 0For a zero signum, it will return = 0
以下程序说明了Java.lang.Integer.signum()方法。
程序1 :
// Java program to illustrate the// Java.lang.Integer.signum() Methodimport java.lang.*; public class Geeks { public static void main(String[] args) { // It will return 1 as the int value is greater than 1 System.out.println(Integer.signum(1726)); // It will return -1 as the int value is less than 1 System.out.println(Integer.signum(-13)); // It will returns 0 as int value is equal to 0 System.out.println(Integer.signum(0));}}输出
1-10
程序2
// Java program to illustrate the// Java.lang.Integer.signum() Methodimport java.lang.*; public class Geeks { public static void main(String[] args) { // It will return 1 as the int value is greater than 1 System.out.println(Integer.signum(-1726)); // It will return -1 as the int value is less than 1 System.out.println(Integer.signum(13)); // It will returns 0 as int value is equal to 0 System.out.println(Integer.signum(0));}}输出
-110
程序3: 对于一个十进制值和字符串。
注意: 当十进制值和字符串作为参数传递时,会返回错误信息。
// Java program to illustrate the// Java.lang.Integer.signum() Methodimport java.lang.*; public class Geeks { public static void main(String[] args) { //returns compile time error as passed argument is a decimal value System.out.println(Integer.signum(3.81)); //returns compile time error as passed argument is a string System.out.println(Integer.signum("77")); }}输出
prog.java:10: error: incompatible types: possible lossy conversion from double to int System.out.println(Integer.signum(3.81)); ^prog.java:14: error: incompatible types: String cannot be converted to int System.out.println(Integer.signum("77")); ^Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output2 errors 