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");
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); }
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)); } }
- 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); }
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); }
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; }
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; }
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; }
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; }
Comparison method, if x > y, returns a positive number, if x = y, returns 0. If x
public static int toUnsignedInt(byte x) { return ((int) x) & 0xff; } public static long toUnsignedLong(byte x) { return ((long) x) & 0xffL; }
Convert byte type to unsigned int type and long type
private static final long serialVersionUID = -7183698231559129828L;
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!

This article analyzes the top four JavaScript frameworks (React, Angular, Vue, Svelte) in 2025, comparing their performance, scalability, and future prospects. While all remain dominant due to strong communities and ecosystems, their relative popul

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

Node.js 20 significantly enhances performance via V8 engine improvements, notably faster garbage collection and I/O. New features include better WebAssembly support and refined debugging tools, boosting developer productivity and application speed.

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

This article addresses the CVE-2022-1471 vulnerability in SnakeYAML, a critical flaw allowing remote code execution. It details how upgrading Spring Boot applications to SnakeYAML 1.33 or later mitigates this risk, emphasizing that dependency updat

Iceberg, an open table format for large analytical datasets, improves data lake performance and scalability. It addresses limitations of Parquet/ORC through internal metadata management, enabling efficient schema evolution, time travel, concurrent w

This article explores methods for sharing data between Cucumber steps, comparing scenario context, global variables, argument passing, and data structures. It emphasizes best practices for maintainability, including concise context use, descriptive

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
