Java Number.shortValue()方法

shortValue() 以 short 形式返回指定的数值。

1 语法

public short shortValue()  

2 参数

3 返回值

以 short 形式返回指定的数值。

4 示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Number.shortValue()的例子
 */
public class Demo {

    public static void main(String[] args) {
        Number num1 = -57225.65650;
        //返回此Number的值作为short类型
        System.out.println("Output Value = " + num1.shortValue());
        Integer obj2 = 225090;
        //以short类型返回此Integer的值
        short s2 = obj2.shortValue();
        System.out.println("Output Value = " + s2);
    }
}

输出结果为:

Output Value = 8311
Output Value = 28482

5 示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Number.shortValue()的例子
 */
public class Demo {

    public static void main(String[] args) {
        Byte B = -92;
        short s = B.shortValue();
        System.out.println(s);
        Short S = 53;
        s = S.shortValue();
        System.out.println(s);
        Float F = (float) 23454.56;
        s = F.shortValue();
        System.out.println(s);
        Double D = 521789879.567868;
        s = D.shortValue();
        System.out.println(s);
    }
}

输出结果为:

-92
53
23454
-7753

6 示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Number.shortValue()的例子
 */
import java.util.Scanner;

public class Demo {

    public static void main(String[] args) {
        System.out.print("请输入数字: ");
        Scanner scan = new Scanner(System.in);
        //获取控制台输入
        Integer object = scan.nextInt();
        short value = object.shortValue();
        System.out.println("Output is = " + value);
        scan.close();
    }
}

输出结果为:

请输入数字: YIIDIAN
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:864)
	at java.util.Scanner.next(Scanner.java:1485)
	at java.util.Scanner.nextInt(Scanner.java:2117)
	at java.util.Scanner.nextInt(Scanner.java:2076)
	at com.yiidian.Demo.main(Demo.java:17)

 

热门文章

优秀文章