java package data type --Byte

  Byte is the basic type byte encapsulation type. The Integer Similarly, Byte also provides many of the same methods, such as decode, toString, intValue, floatValue the like, and many methods or direct type converted to an int operate (for example:  public static String toString ( byte B) { return Integer .toString (( int ) B, 10 );}  ). So we just focus on a few different methods or properties.

1. the range

  We know that the range of data byte represents the [-128, 127], then the Byte use two static properties represent the upper and lower bounds: MIN_VALUE, MAX_VALUE

    

2. Cache

  And similar Integer, Byte also provides a caching mechanism, the source code is as follows:

private static class ByteCache {
        private ByteCache(){}

        static final Byte cache[] = new Byte[-(-128) + 127 + 1];

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Byte((byte)(i - 128));
        }
    }

  As can be seen, Byte cache is -128 to 127. We already know, byte type value range is [-128, 127], so Byte is All values ​​were cached.

3. The method of construction and method valueOf

  Byte configuration also provides two methods, respectively, and receiving byte string type parameters:  public Byte ( byte value) { the this .Value = value;}   and a   public Byte (String S) throws a NumberFormatException { the this .Value = parseByte (S, 10 ) ;}  . When you create an object using the constructor will directly allocate memory. The valueOf method will get from the cache, it is recommended to use an object instance valueOf get under normal circumstances, in order to save money.

public static Byte valueOf(byte b) {
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }
public static Byte valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseByte(s, radix));
    }
public static Byte valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

Method 4. hashCoe

  Byte hashCode value is the value of the type (transfer int) it represents

@Override
    public int hashCode() {
        return Byte.hashCode(value);
    }

    /**
     * Returns a hash code for a {@code byte} value; compatible with
     * {@code Byte.hashCode()}.
     *
     * @param value the value to hash
     * @return a hash code value for a {@code byte} value.
     * @since 1.8
     */
    public static int hashCode(byte value) {
        return (int)value;
    }

5. compareTo Method

  Byte also achieve a comparable interface, you can compare the size. And the Integer compareTo is somewhat different, respectively Integer return value is less than the current parameter values ​​-1, 0, and return Byte positive, 0, negative (the current value - the parameter value), of course, this also matches of comparable conventional agreed. 

public int compareTo(Byte anotherByte) {
        return compare(this.value, anotherByte.value);
    }

    /**
     * Compares two {@code byte} values numerically.
     * The value returned is identical to what would be returned by:
     * <pre>
     *    Byte.valueOf(x).compareTo(Byte.valueOf(y))
     * </pre>
     *
     * @param  x the first {@code byte} to compare
     * @param  y the second {@code byte} to compare
     * @return the value {@code 0} if {@code x == y};
     *         a value less than {@code 0} if {@code x < y}; and
     *         a value greater than {@code 0} if {@code x > y}
     * @since 1.7
     */
    public static int compare(byte x, byte y) {
        return x - y;
    }

 

Finish!

Guess you like

Origin www.cnblogs.com/coding-one/p/11725145.html