Java中的Integer shortValue()方法

整数。shortValue()是java的一种内置方法。lang,它返回 短的 类型

null

语法:

public short shortValue()

参数 :该方法不采用任何参数。

返回值: 将该对象转换为short类型后,该方法返回该对象表示的整数值。

下面的程序演示了整数。shortValue()方法: 项目1: 对于正整数。

// Java program that demonstrates
// Integer.shortValue() method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
Integer sh_object = new Integer( 763 );
// It will return the value of this Integer as a short type
short sh_value = sh_object.shortValue();
System.out.println( " The Value of sh_value = " + sh_value);
}
}


输出:

The Value of sh_value = 763

项目2: 对于负数。

// Java program that demonstrates
// Integer.shortValue() method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
Integer sh_object = new Integer(- 43 );
// It will return the value of this Integer as a short type
short sh_value = sh_object.shortValue();
System.out.println( " The Value of sh_value = " + sh_value);
}
}


输出:

The Value of sh_value = -43

方案3: 对于十进制值和字符串。 注: 当十进制值和字符串作为参数传递时,它返回错误消息。

// java program that demonstrates
// Integer.shortValue() method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
// passing a decimal value
Integer sh_object = new Integer( 27.51 );
short sh_value = sh_object.shortValue();
System.out.println( " The Value of sh_value = " + sh_value);
// passing a string
Integer sh_object2 = new Integer( "51" );
short sh_value2 = sh_object2.shortValue();
System.out.println( " The Value of sh_value2 = " + sh_value2);
}
}


输出:

prog.java:10: error: no suitable constructor found for Integer(double)
    Integer sh_object = new Integer(27.51);
                        ^
    constructor Integer.Integer(int) is not applicable
      (argument mismatch; possible lossy conversion from double to int)
    constructor Integer.Integer(String) is not applicable
      (argument mismatch; double cannot be converted to String)
1 error
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享