首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

一个小有关问题

2011-11-24 
一个小问题下面是一段我自己设计的代码,目的是为了了解java.lang.ByteJava codepublic class Tbyte{public

一个小问题
下面是一段我自己设计的代码,目的是为了了解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. */ 

热点排行