Byte類型的SIZE是8,開始還以為是1呢。這個值是指使用的bit位元,而不是對象占記憶體大小。
Byte為有符號數,-128~127,初始化是初始值不能超過表示的範圍,會拋異常,而不象C++會截斷。
使用字串類型初始化時,字串不能為null或者"",否則會拋數字格式化異常。
package tjava.testlang;</p><p>public class TestByte extends BasicClassTest<br />{<br />static String className = "java.lang.Byte";</p><p>public static void test() throws ClassNotFoundException<br />{<br />logPrintln("Test Show:");<br />logPrintln(" Byte(/"123/") = " + new Byte("123"));<br />logPrintln(" Byte(/"-123/") = " + new Byte("-123"));<br />try<br />{<br />logPrintln(" Byte(/"abc/") = " + new Byte("abc"));<br />} catch (NumberFormatException e)<br />{<br />logPrintln(" Byte(/"abc/") = new Byte(/"abc/") throw a NumberFormatException!");<br />}<br />logPrintln(" Byte((byte) (123)) = " + new Byte((byte) 123));</p><p>try<br />{<br />logPrintln(" Byte.parseByte(null) = " + Byte.parseByte(null));<br />} catch (NumberFormatException e)<br />{<br />logPrintln(" Byte(null) = new Byte(null) throw a NumberFormatException!");<br />}<br />try<br />{<br />logPrintln(" Byte.parseByte(/"/") = " + Byte.parseByte(""));<br />} catch (NumberFormatException e)<br />{<br />logPrintln(" Byte(/"/") = new Byte(null) throw a NumberFormatException!");<br />}<br />try<br />{<br />logPrintln(" Byte.valueOf(null) = " + Byte.valueOf(null));<br />} catch (NumberFormatException e)<br />{<br />logPrintln(" Byte.valueOf(null) = new Byte(null) throw a NumberFormatException!");<br />}<br />logPrintln("Byte.parseByte(/"7b/", 16) = " + Byte.parseByte("7b", 16));<br />logPrintln("Byte.parseByte(/"-7b/", 16) = " + Byte.parseByte("-7b", 16));<br />logPrintln(" Byte.valueOf(/"7b/", 16) = " + Byte.valueOf("7b", 16));<br />logPrintln(" Byte.valueOf(/"-7b/", 16) = " + Byte.valueOf("-7b", 16));<br />logPrintln(" Byte.TYPE = " + Byte.TYPE);<br />logPrintln(" Byte.SIZE = " + Byte.SIZE);<br />logPrintln(" Byte.MAX_VALUE = " + Byte.MAX_VALUE);<br />logPrintln(" Byte.MAX_VALUE+1 = " + new Byte((byte) (Byte.MAX_VALUE + (byte) 1)));<br />logPrintln(" Byte.MIN_VALUE = " + Byte.MIN_VALUE);<br />logPrintln(" Byte.MIN_VALUE-1 = " + new Byte((byte) (Byte.MIN_VALUE - (byte) 1)));<br />}</p><p>public static void showClassInfo() throws ClassNotFoundException<br />{<br />BasicClassTest.showClassInfo(className);<br />}<br />}<br />
結果:
=================== java.lang.Byte's information ===================
package name : java.lang
class name : java.lang.Byte
super class name : java.lang.Number
include method :
hashCode( )
compareTo( java.lang.Byte )
compareTo( java.lang.Object )
equals( java.lang.Object )
toString( byte )
toString( )
decode( java.lang.String )
valueOf( byte )
valueOf( java.lang.String int )
valueOf( java.lang.String )
byteValue( )
doubleValue( )
floatValue( )
intValue( )
longValue( )
shortValue( )
parseByte( java.lang.String )
parseByte( java.lang.String int )
wait( )
wait( long int )
wait( long )
getClass( )
notify( )
notifyAll( )
include field :
MIN_VALUE
MAX_VALUE
TYPE
SIZE
include Constructor :
java.lang.Byte( byte )
java.lang.Byte( java.lang.String )
Test Show:
Byte("123") = 123
Byte("-123") = -123
Byte("abc") = new Byte("abc") throw a NumberFormatException!
Byte((byte) (123)) = 123
Byte(null) = new Byte(null) throw a NumberFormatException!
Byte("") = new Byte(null) throw a NumberFormatException!
Byte.valueOf(null) = new Byte(null) throw a NumberFormatException!
Byte.parseByte("7b", 16) = 123
Byte.parseByte("-7b", 16) = -123
Byte.valueOf("7b", 16) = 123
Byte.valueOf("-7b", 16) = -123
Byte.TYPE = byte
Byte.SIZE = 8
Byte.MAX_VALUE = 127
Byte.MAX_VALUE+1 = -128
Byte.MIN_VALUE = -128
Byte.MIN_VALUE-1 = 127