当前位置: 代码迷 >> Java相关 >> 一个小有关问题
  详细解决方案

一个小有关问题

热度:777   发布时间:2013-02-25 21:50:14.0
一个小问题
下面是一段我自己设计的代码,目的是为了了解java.lang.Byte
Java code
public class Tbyte{    public static void main(String args[]){        Byte b1=new Byte("a");                int i1;        i1=b1.intValue();                System.out.println(i1);    }}

虽然编译通过,但运行结果出现了下面的文字
--------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
  at java.lang.Integer.parseInt(Integer.java:449)
  at java.lang.Byte.parseByte(Byte.java:151)
  at java.lang.Byte.<init>(Byte.java:325)
  at Tbyte.main(Tbyte.java:3)

Process completed.

我不知道为什么会这样,求解释啊........

------解决方案--------------------------------------------------------
Java code
public class test {    public static void main(String args[]) {        Byte b1 = new Byte((byte)'a');        int i1;        i1 = b1.intValue();        System.out.println(i1);    }}
------解决方案--------------------------------------------------------
Java code
String str = "127";//取值范围:"-128","-127",...."-2","-1","0","1","2",....,"126","127"Byte b1 = new Byte(str);//因为它只有八个二进制位,十进制形式为大于-128小于127int i1;i1 = b1.intValue();/*Constructs a newly allocated Byte object that represents the byte value indicated by the String parameter. The string is converted to a byte value in exactly the manner used by the parseByte method for radix 10. */
  相关解决方案