Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the example of the caching mechanism of the Java wrapper class

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article gives you an example of the caching mechanism of the Java wrapper class, which is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

The caching mechanism of the java wrapper class is a feature introduced in Java 5 to help save memory and improve performance, which is effective only when boxing automatically.

Integer wrapper class

Take a chestnut:

Integer a = 127 th Integer b = 127 th System.out.println (a = = b)

The output of this code is true

The underlying implementation of using autoboxing to convert a primitive type to a wrapper class object is to call the wrapper class's valueOf method:

Integer a = 127; equivalent to Integer a = Integer.valueOf

Take a look at Integer's valueOf method:

Public static Integer valueOf (int I) {if (I > = IntegerCache.low & & I = 127;} private IntegerCache () {}}

The default range is between-128and 127. the maximum value of the range can be set through java.lang.Integer.IntegerCache.high, and the data within the range can be instantiated into Integer objects and placed in the cache array through the for loop.

Take a test again:

Integer a = 128 th Integer b = 128 th System.out.println (a = = b)

The output is false, so if no cache maximum is specified, the cache will be used when autoboxing is used between-128and 127,

Byte wrapper class

Give me another chestnut:

Public static void main (String [] args) {Byte a = 127; Byte b = 127; System.out.println (a = = b); / / true}

Since the Byte ranges from-128to 127, the valueOf of Byte is obtained from the ByteCache cache.

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

ByteCache class:

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)); }} 与IntegerCache相比,ByteCache的最大值是不能修改的就是127 Short包装类 public static Short valueOf(short s) { final int offset = 128; int sAsInt = s; if (sAsInt >

=-128 & & sAsInt

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report