Java Number.byteValue()方法

byteValue() 返回字节一个字节的值。

1 语法

public byte byteValue()  

2 参数

3 返回值

返回字节一个字节的值。

4 示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Number.byteValue()的例子
 */
public class Demo {
    public static void main(String[] args) {
        Integer i = 123456;
        //将数字值转换为字节类型
        int Result = i.byteValue();
        System.out.println("Number as Byte: "+Result);
    }
}

输出结果为:

Number as Byte: 64

5 示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Number.byteValue()的例子
 */
public class Demo {
    public static void main(String[] args) {
        Integer i = -123456;
        Float f = 435f;
        Double d = 65868.685;
        //将数字值转换为字节类型  
        System.out.println("Integer Number as Byte: "+i.byteValue());
        System.out.println("Float Number as Byte: "+f.byteValue());
        System.out.println("Double Number as Byte: "+d.byteValue());
    }
}

输出结果为:

Integer Number as Byte: -64
Float Number as Byte: -77
Double Number as Byte: 76

6 示例3

package com.yiidian;

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

public class Demo {
    public static void main(String[] args)throws IOException {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the Number Value: ");
        Integer num = scan.nextInt();
        //将数字值转换为字节类型  
        System.out.println("Integer Number as Byte: "+num.byteValue());
        scan.close();
    }
}

输出结果为:

Enter the Number Value: 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:16)

 

热门文章

优秀文章