Home > Java > javaTutorial > body text

Detailed analysis of the source code of Byte class in Java--step details

php是最好的语言
Release: 2018-08-04 11:36:58
Original
1500 people have browsed it

Today, let’s analyze the source code of the java.lang.Byte class and go straight to the topic

First of all

public final class Byte extends Number implements Comparable<Byte> {
public static final byte   MIN_VALUE = -128;
public static final byte   MAX_VALUE = 127;
public static final int SIZE = 8;
public static final int BYTES = SIZE / Byte.SIZE;
    @SuppressWarnings("unchecked")
    public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
Copy after login

The first sentence of the Byte class is modified by final and cannot be inherited. Inherit The Number class can be used for a series of conversions of numeric types. It implements the Comparable interface, which can be used for comparison.
The second and third sentences define the minimum and maximum values.
The fourth sentence defines the size of Byte. , is 8 bits, that is, one byte
The fifth sentence gives the byte, that is, SIZE/Byte.SIZE = 1; occupies one byte
The sentence that is annotated to remain silent for warnings is Get the two constructors of the original class

    public Byte(byte value) {
        this.value = value;
    }
    public Byte(String s) throws NumberFormatException {
        this.value = parseByte(s, 10);
    }
Copy after login

Byte class. There are restrictions here. The value passed in must be a byte type value, and the string s must be a string that can be converted to a number. Otherwise, An error will be reported

    public static String toString(byte b) {
        return Integer.toString((int)b, 10);
    }
    public String toString() {
        return Integer.toString((int)value);
    }
    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));
        }
    }
Copy after login
  • Next is the toString method, which converts the byte type into a string, which uses the method in the Integer class

The following The ByteCache method defines a Byte cache value and writes -128~127 into a cache array. When it is within this range, the JVM will directly use the cache value, but when it exceeds this range, overflow will occur. As mentioned in the previous article, 128 will become -128, and the loop calculation will continue from the minimum value

parseByte parses the string type into byte type, radix is ​​the base, radix is ​​the number, and s is A decimal number. The result after parsing is a decimal number.

    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);
    }
Copy after login

Convert the value of the incoming parameter to the Byte type. Here it is taken directly from the cache. The method of hanging the parameter radix is ​​to first parse the string into decimal. Then perform valueOf

    public static Byte decode(String nm) throws NumberFormatException {
        int i = Integer.decode(nm);
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                    "Value " + i + " out of range from input " + nm);
        return valueOf((byte)i);
    }
Copy after login

This is a decoding and transcoding method. The previous method was not written like this. Now the decode method of the Integer class is directly called, and then it is judged whether it is less than the minimum value or greater than the maximum value. Then convert it to the byte type and return it. Now it really answers the sentence "There is no byte type in Java"

    public byte byteValue() {
        return value;
    }
    public short shortValue() {
        return (short)value;
    }
    public int intValue() {
        return (int)value;
    }
    public long longValue() {
        return (long)value;
    }
    public float floatValue() {
        return (float)value;
    }
    public double doubleValue() {
        return (double)value;
    }
Copy after login

These are some methods of forced type conversion, which are very simple, but still write

    @Override
    public int hashCode() {
        return Byte.hashCode(value);
    }
    public static int hashCode(byte value) {
        return (int)value;
    }
Copy after login

The first hashCode is an override of the hasnCode method of Object, which is used to compare two values. The hashCode method is often distinguished from the equals method, especially during interviews. This will not be explained in detail now. The hashCode method is mostly used in the

    public boolean equals(Object obj) {
        if (obj instanceof Byte) {
            return value == ((Byte)obj).byteValue();
        }
        return false;
    }
Copy after login

equals method of the collection. Now it is worth comparing.

    public int compareTo(Byte anotherByte) {
        return compare(this.value, anotherByte.value);
    }
    public static int compare(byte x, byte y) {
        return x - y;
    }
Copy after login

Comparison method, if x > y, returns a positive number, if x = y, returns 0. If x < y, return a negative number

    public static int toUnsignedInt(byte x) {
        return ((int) x) & 0xff;
    }
    public static long toUnsignedLong(byte x) {
        return ((long) x) & 0xffL;
    }
Copy after login

Convert byte type to unsigned int type and long type

private static final long serialVersionUID = -7183698231559129828L;
Copy after login
  • Used during serialization, now Without further explanation, I don’t quite understand the serialization process. . . .

Related articles:

java int to byte and long to byte

Analysis of Buffer source code in java

The above is the detailed content of Detailed analysis of the source code of Byte class in Java--step details. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!