[JAVA] I read the source of Byte

I decided to read the JDK source somehow. That said, I don't have time to read each line carefully, so I read it briefly and found this code.

Byte class

The Byte class is a wrapper class for the primitive type byte. It seems that I haven't found anything by reading the source, which is surprising.

First, the fields and the constructor. Well, it's a source that everyone can imagine.

Byte.java


    private final byte value;

    public Byte(byte value) {
        this.value = value;
    }

ByteCache class

Actually, there is a ByteCache class that cannot be seen in javadoc.

Byte.java


    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));
        }
    }

What we're doing is caching instances from -128 to 127. Oh, it means that all byte values are in the cache.

It is a cache of ByteCache, but it is referenced by valueOf.

Byte.java


    public static Byte valueOf(byte b) {
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }

Compare with the comparison operator ==

If the instance is in the cache, you can compare it with the == operator. Try out.

Main.java


	public static void main(String[] args) {
		byte b0 = 1;
		Byte b1 = new Byte(b0);
		Byte b2 = Byte.valueOf(b0);
		Byte b3 = b0;
		System.out.println(b1 == b2);
		System.out.println(b1 == b3);
		System.out.println(b2 == b3);
	}

Result is···

false
false
true

That's too bad. It is natural that new Byte () will create another instance, but the reference value of the instance will be different. However, the fact that autoboxing (variable b3) is the same as Byte.valueOf () means that autoboxing calls Byte.valueOf () instead of new Byte ().

Besides ...

Well, not really. Speaking of force, the compiler gets angry that the int type cannot be converted to the byte type when writing new Byte (1) or Byte.valueOf (1), but you can see that the constant does not exceed the range of bytes! I feel like. The compiler seems to be smart and quite stubborn, so I want it to be a little more flexible.

Recommended Posts

I read the source of Byte
I read the source of ArrayList I read
I read the source of Integer
I read the source of Short
I read the source of String
I read the Kotlin startbook
Is drainTo of LinkedBlockingQueue safe? I followed the source
05. I tried to stub the source of Spring Boot
I investigated the internal processing of Retrofit
[day: 5] I summarized the basics of Java
Read the Perelman treatise of Poincare conjecture
I want to output the day of the week
I understood the very basics of character input
I compared the characteristics of Java and .NET
I want to var_dump the contents of the intent
I touched on the new features of Java 15
I tried using the profiler of IntelliJ IDEA
I checked the number of taxis with Ruby
Try the free version of Progate [Java I]
[Java] How to get the URL of the transition source
I tried using the Server Push function of Servlet 4.0
I read the readable code, so make a note
I was addicted to the record of the associated model
I tried to summarize the state transition of docker
I saw the list view of Android development collectively
I tried to reduce the capacity of Spring Boot
Judgment of the calendar
I tried the new feature profiler of IntelliJ IDEA 2019.2.
The world of clara-rules (4)
I want to know the answer of the rock-paper-scissors app
Image processing: The basic structure of the image read by the program
I want to display the name of the poster of the comment
I summarized the display format of the JSON response of Rails
The world of clara-rules (1)
The world of clara-rules (3)
Read Java HashMap source
I read the "Object-Oriented Practical Guide", so a memorandum
Source of cellular objects
Read the Rails Guide (Overview of Action Controller) again
I wrote a sequence diagram of the j.u.c.Flow sample
The world of clara-rules (5)
I summarized the types and basics of Java exceptions
The idea of quicksort
[WIP] I tried the configuration of Docker + Streama + NFS
I am keenly aware of the convenience of graphql-code-generator, part 2
I can't get out of the Rails dbconsole screen
I learned about the existence of a gemspec file
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
The idea of jQuery
I made the server side of an online card game ①
I didn't understand the behavior of Java Scanner and .nextLine ().
I took a peek at the contents of Java's HashMap
I tried to summarize the basics of kotlin and java
Now, I understand the coordinate transformation method of UIView (Swift)
[Android] Exit the activity of the transition source at the time of screen transition
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
[Swift] I tried to implement the function of the vending machine
I tried JAX-RS and made a note of the procedure
Specify the character code of the source when building with Maven