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

A detailed introduction to the Java virtual machine (part ④)-- 8 basic types of wrapper classes and constant pools

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Most of the wrapper classes of the basic type of Java implement constant pool technology, that is, the five wrapper classes of Byte,Short,Integer,Long,Character,Boolean; create corresponding types of cached data with a value of [- 128127] by default, but new objects will still be created beyond this range. The two floating-point wrapper classes Float and Double do not implement constant pooling technology.

The implementation of the valueOf () method is relatively simple, which is to first determine whether the value is in the cache pool, and if so, directly return the contents of the cache pool.

Part of the source code of Integer:

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

Example 1:

At compile time, Integer i1 destroy 40 pancake Java directly encapsulates the code into Integer i1=Integer.valueOf (40); thus using objects in the constant pool. Integer i2 = new Integer (40); / / create a new object. System.out.println (i1==i2); / / output false

Example 2:Integer has automatic unpacking function.

Integer i1 = 40 i1=i2+i3 Integer i2 = 40 i2+i3 Integer i3 = 0 X Integer i4 = new Integer (40); Integer i5 = new Integer (40); Integer i6 = new Integer (0); System.out.println ("i1=i2" + (i1 = = i2)); / / output i1=i2 trueSystem.out.println ("i1=i2+i3" + (i1 = = i2+i3)); / / output i1=i2+i3 true//i2+i3 gets 40, compared with the numerical System.out.println ("i1=i4" + (i1 = = i4)) / / output i1=i4 falseSystem.out.println ("i4=i5" + (i4 = = i5)); / / output i4=i5 false//i5+i6 to get 40, comparing the numeric System.out.println ("i4=i5+i6" + (i4 = = i5+i6)); / / output i4=i5+i6 trueSystem.out.println ("40=i5+i6" + (40 = = i5+i6)); / / output 40=i5+i6 true

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

Internet Technology

Wechat

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

12
Report