學習java筆記(2),java.lang.Byte。

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.