API之Byte类型

来源:互联网 发布:win7系统网络共享设置 编辑:程序博客网 时间:2024/06/06 04:07
package com.wonders.week01;/** * 以JDK1.7为基础 * (1)java.lang.Byte继承了java.lang.Number * (2)是final修饰的类 * (3)构造方法:public Byte(byte value) * (4)构造方法:public Byte(String s) throws NumberFormatException 里面调用了parseByte(s, 10)方法 * @author liyongyong * */public class ByteTest {    public static final byte   MIN_VALUE = -128;//byte类型最小值    public static final byte   MAX_VALUE = 127;//byte类型最大值    public static void main(String[] args) {        /*         * public static String toString(byte b)         * (1)static方法,返回值是String类型,参数只有一个是byte类型         * (2)返回值调用了Integer.toString(int a,int b)方法         * toString()会将byte类型参数b强制转为int类型,并且第二个参数设定为10,         * 此时就会执行b==10的条件语句并调用Integer类中的toString(int i)方法         */        String str1 = Byte.toString((byte) 12);        System.out.println(str1);//12        /*         * public static Byte valueOf(byte b)         * (1)返回值类型是Byte,参数是byte类型         * (2)是一个static修饰的public方法         * (3)Byte类的valueOf()方法接收一个byte类型的参数,         * valueOf(byte b)会返回一个ByteCache.cache[(int)b + offset];的值         * offset是一个固定值128,其中cache[0]~cache[255]对应的是-128-127,         * 如果cache的下标超过0-255,则循环值         */        Byte b1 = Byte.valueOf(MAX_VALUE);        System.out.println(b1);//127        Byte b2 = Byte.valueOf((byte)128);        System.out.println(b2);//-128        Byte b3 = Byte.valueOf((byte)-134);        System.out.println(b3);//122        /*         * public static byte parseByte(String s, int radix)         * (1)static修饰的public方法,返回值类型是byte类型         * (2)有两个参数,第一个是String类型字符串,第二个是int类型         * (3)该方法会抛出NumberFormatException         * (4)如果参数字符串为null,会抛出NumberFormatException异常         * (5)如果指定的精度radix小于2或者大于36,也会抛出NumberFormatException异常         * (6)最后当第一个参数的值被转化后,在-128~127之间,则会返回结果,如果不在-128~127之间则继续抛出         * NumberFormatException异常         *///      byte b4 = Byte.parseByte(null, 0);//      System.out.println(b4);//      byte b5 = Byte.parseByte("hello",1);//radix 1 less than Character.MIN_RADIX//      System.out.println(b5);//      byte b6 = Byte.parseByte("hello",40);//radix 40 greater than Character.MAX_RADIX//      System.out.println(b6);//      byte b7 = Byte.parseByte("129", 3);//NumberFormatException//      System.out.println(b7);        byte b8 = Byte.parseByte("12", 3);        System.out.println(b8);//5        /*         * public static byte parseByte(String s, 10)         * (1)static修饰的public方法,返回值类型是byte类型         * (2)有两个参数,第一个是String类型字符串,第二个参数是固定值10         * (3)该方法会抛出NumberFormatException         * (4)如果参数字符串为null,会抛出NumberFormatException异常         * (6)最后当第一个参数的值被转化后,在-128~127之间,则会返回结果,如果不在-128~127之间则继续抛出         * NumberFormatException异常         *///      byte b9 = Byte.parseByte(null, 10);//NumberFormatException//      System.out.println(b9);//      byte b10 = Byte.parseByte("129", 10);//Value out of range. Value:"129" Radix:10//      System.out.println(b10);        byte b11 = Byte.parseByte("12",10);        System.out.println(b11);//12        /*         * public static Byte valueOf(String s, int radix)         * (1)是一个静态方法,返回值为Byte对象,有两个参数,第一个是String类型字符串,第二个是int类型         * (2)该方法会抛出NumberFormationException         * (3)该方法调用了Byte类中的valueOf(byte b)方法,在内部有调用了parseByte(String s,int radix)方法         * (4)最后又调用了parseInt(String s,int radix)    方法            *///      Byte bb1 = Byte.valueOf("", 12);//      System.out.println(bb1);//NumberFormatException//      Byte bb2 = Byte.valueOf(null, 12);//      System.out.println(bb2);//NumberFormatException//      Byte bb3 = Byte.valueOf("TrUe", 12);//      System.out.println(bb3);        Byte bb4 = Byte.valueOf("3", 12);        System.out.println(bb4);//3        /*         * public static Byte valueOf(String s)          * (1)是一个静态方法,返回值类型是Byte对象,只有一个参数String类型         * (2)throws NumberFormatException         */        Byte byte1 = Byte.valueOf("-12");        System.out.println(byte1);//-12        Byte byte2 = Byte.valueOf("124");        System.out.println(byte2);//124        /*         * public static Byte decode(String nm)          * (1)静态方法,返回值类型是Byte,只有一个参数是String类型的字符串         * (2)throws NumberFormatException         *///      Byte byte3 = Byte.decode("");//      System.out.println(byte3);//java.lang.NumberFormatException: Zero length string//      Byte byte4 = Byte.decode("128");//      System.out.println(byte4);// java.lang.NumberFormatException: Value 128 out of range from input 128//      Byte byte5 = Byte.decode("-129");//      System.out.println(byte5);// java.lang.NumberFormatException: Value -129 out of range from input -129        Byte byte6 = Byte.decode("12");        System.out.println(byte6);//12        Byte byte7 = Byte.decode("0011");        System.out.println(byte7);//9        /*         * public byte byteValue()         * (1)非静态方法,返回值类型是基础数据类型byte         * (2)返回一个byte类型的数据         */        Byte bb = new Byte("123");//创建Byte类型对象bb        byte by1 = bb.byteValue();//通过对象.方法名的形式调用byteValue方法        System.out.println(by1);//123        /*         *  public int intValue()         * (1)非静态方法,返回值类型是int类型,无参数         * (2)返回值是一个int类型,将byte类型强制转化为int类型         */        int number1 = bb.intValue();        System.out.println(number1);//123        /*         * public long longValue()         * (1)非静态方法,返回值类型是long类型,无参数         * (2)返回值是一个long类型,将byte类型强制转化为long类型         */        long l1 = bb.longValue();        System.out.println(l1);//123        /*         * public float floatValue()         * (1)非静态方法,返回值类型是float类型,无参数         * (2)返回值是一个float类型,将byte类型强制转化为float类型         */        float f1 = bb.floatValue();        System.out.println(f1);//123.0        /*         * public double doubleValue()         * (1)非静态方法,返回值类型是double类型,无参数         * (2)返回值是一个double类型,将byte类型强制转化为double类型         */        double d1 = bb.doubleValue();        System.out.println(d1);//123.0        /*         * public String toString()         * (1)非静态方法,返回值类型是String类型,无参数         * (2)返回值是一个String类型,先将byte类型值转为int类型,再通过Integer.toString()方法转为String类型         */        String s1 = bb.toString();        System.out.println(s1);//123        /*         * public int hashCode()         * (1)非静态方法,返回值类型是int类型,无参数         * (2)调用hashCode方法时,会将byte类型数据转为int类型然后返回         */        int number2 = bb.hashCode();        System.out.println(number2);//123        /*         * public boolean equals(Object obj)         * (1)非静态方法,返回值类型是boolean,参数是一个Object对象         * (2)调用时会先判断该对象是否是Object类型,如果是则调用byteValue方法与value做比较,相等则返回true,否则返回false         */        boolean boolean1 = bb.equals(bb);        System.out.println(boolean1);//true        boolean boolean2 = bb.equals("hello");        System.out.println(boolean2);//false        /*         * public int compareTo(Byte anotherByte)         * (1)非静态方法,返回值类型int,参数是一个Byte对象         * (2)调用了compare方法,会将两个byte类型的值做减法         */        int number3 = bb.compareTo(bb);        System.out.println(number3);//0        Byte bb1 = new Byte("-123");        int number4 = bb.compareTo(bb1);        System.out.println(number4);//246        /*         * public static int compare(byte x, byte y)         * (1)静态方法,返回值类型int,两个参数都是byte类型         * (2)返回值是x-y的结果         */        int result = Byte.compare((byte)125, (byte)-129);        System.out.println(result);//-2    }}
原创粉丝点击