Java Integer signum()方法

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 = 27
Since 27 is a positive int signum will return= 1

Consider another integer a = -61
Since -61 is a negative int signum will return= -1

Consider the integer value _a_ = 0
For a zero signum, it will return = 0 

以下程序说明了Java.lang.Integer.signum()方法。

程序1 :

// Java program to illustrate the
// Java.lang.Integer.signum() Method
import 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
-1
0

程序2

// Java program to illustrate the
// Java.lang.Integer.signum() Method
import 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
1
0

程序3: 对于一个十进制值和字符串。
注意: 当十进制值和字符串作为参数传递时,会返回错误信息。

// Java program to illustrate the
// Java.lang.Integer.signum() Method
import 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 output
2 errors

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程