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

Integer interview and source code analysis

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

Share

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

This article shows you the Integer interview and source code analysis, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Scene:

A friend of    went to the interview yesterday. I asked him what questions he asked in the interview, including questions related to Integer. Here are the questions asked by the interviewer, and some of which I have extended.

Q: are the two new Integer 128s equal?

A: no. Because the Integer cache pool defaults to-127128

Q: can I modify the scope of the Integer cache pool? How to modify it?

A: yes. Use-Djava.lang.Integer.IntegerCache.high=300 to set the Integer cache pool size

Q: which design pattern is used in the Integer caching mechanism?

A: Hengyuan model

Q: how does Integer get the cache pool size you set?

Sun.misc.VM.getSavedProperty ("java.lang.Integer.IntegerCache.high")

Q: what's the difference between sun.misc.VM.getSavedProperty and System.getProperty?

Answer: the only difference is that System.getProperty can only obtain non-internal configuration information; for example, java.lang.Integer.IntegerCache.high, sun.zip.disableMemoryMapping, sun.java.launcher.diag, sun.cds.enableSharedLookupCache, etc., can not be obtained, these can only be obtained using sun.misc.VM.getSavedProperty

Integer initialization source code analysis: private static class IntegerCache {static final int low =-128; static final int high; static final Integer cache []; static {/ / high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty ("java.lang.Integer.IntegerCache.high") If (integerCacheHighPropValue! = null) {try {int I = parseInt (integerCacheHighPropValue); I = Math.max (I127); / / Maximum array size is Integer.MAX_VALUE h = Math.min (I, Integer.MAX_VALUE-(- low)-1) } catch (NumberFormatException nfe) {/ / If the property cannot be parsed into an int, ignore it. }} high = h; cache = new Integer [(high-low) + 1]; int j = low; for (int k = 0; k)

< cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >

= 127;} private IntegerCache () {}} VM.class source code analysis: initialization: static {allowArraySyntax = defaultAllowArraySyntax; savedProps = new Properties (); finalRefCount = 0; peakFinalRefCount = 0; initialize ();} getSavedProperty method: public static String getSavedProperty (String var0) {if (savedProps.isEmpty ()) {throw new IllegalStateException ("Should be non-empty if initialized");} else {return savedProps.getProperty (var0) } savedProps.getProperty method: public String getProperty (String key) {Object oval = super.get (key); String sval = (oval instanceof String)? (String) oval: null; return ((sval = = null) & & (defaults! = null)? Defaults.getProperty (key): sval;} System.java source code analysis: / * initialize the system class. Called after the thread is initialized. * / private static void initializeSystemClass () {/ * VM may call JNU_NewStringPlatform () to set those encoding sensitive attributes (user.home,user.name,boot.class.path, etc.) during "props" initialization, * they may need to be accessed through System.getProperty (), and the relevant system coding attributes that have been initialized (put in "props") in the early stage of initialization. * therefore, make sure that you can use "props" during initialization and put all system properties directly into it. * / props = new Properties (); initProperties (props); / / initialized by the VM / * some system configurations can be controlled by the VM option, such as the maximum amount of direct memory and integer cache size used to support automatic boxing of object identification semantics. Typically, the library will get these values * from the properties set by VM. If the attributes are * for internal implementation only, they should be removed from the system properties. * * see java.lang.Integer.IntegerCache and * for example, the sun.misc.VM.saveAndRemoveProperties method. * * keep a private copy of the system property object, which can only be accessed by internal implementations. Remove * some system attributes that are not suitable for public access. * / sun.misc.VM.saveAndRemoveProperties (props); lineSeparator = props.getProperty ("line.separator"); sun.misc.Version.init (); FileInputStream fdIn = new FileInputStream (FileDescriptor.in); FileOutputStream fdOut = new FileOutputStream (FileDescriptor.out); FileOutputStream fdErr = new FileOutputStream (FileDescriptor.err); setIn0 (new BufferedInputStream (fdIn)); setOut0 (newPrintStream (fdOut, props.getProperty ("sun.stdout.encoding") SetErr0 (newPrintStream (fdErr, props.getProperty ("sun.stderr.encoding"); / * load the zip library now to prevent java.util.zip.ZipFile from trying to load it later. * / loadLibrary ("zip"); / / set the Java signal handler for HUP,TERM and INT (if available). Terminator.setup (); / * initialize any incorrect operating system settings that need to be set for the class library. * currently, this is inoperable anywhere except Windows, which has a process-wide error mode set before using the java.io class. * / sun.misc.VM.initializeOSEnvironment (); / * the main thread is not added to its thread group like other threads; we have to do it ourselves here. * / Thread current = Thread.currentThread (); current.getThreadGroup (). Add (current); / / register shared secret setJavaLangAccess (); / * * subsystems called during initialization can call sun.misc.VM.isBooted () to avoid doing things that should wait until the application class loader is set up. * important: make sure this is still the last initialization operation! * / sun.misc.VM.booted ();}

Let's focus on this sentence: sun.misc.VM.saveAndRemoveProperties (props); it removes the configuration used internally in the system. Let's take a look at how the source code works.

Sun.misc.VM.saveAndRemoveProperties method: public static void saveAndRemoveProperties (Properties var0) {if (booted) {throw new IllegalStateException ("System initialization has completed");} else {savedProps.putAll (var0); String var1 = (String) var0.remove ("sun.nio.MaxDirectMemorySize") If (var1! = null) {if (var1.equals ("- 1")) {directMemory = Runtime.getRuntime (). MaxMemory ();} else {long var2 = Long.parseLong (var1); if (var2 >-1L) {directMemory = var2 } var1 = (String) var0.remove ("sun.nio.PageAlignDirectMemory"); if ("true" .equals (var1)) {pageAlignDirectMemory = true;} var1 = var0.getProperty ("sun.lang.ClassLoader.allowArraySyntax"); allowArraySyntax = var1 = = null? DefaultAllowArraySyntax: Boolean.parseBoolean (var1); / / remove configuration for internal use, which should not be seen by var0.remove ("java.lang.Integer.IntegerCache.high"); var0.remove ("sun.zip.disableMemoryMapping"); var0.remove ("sun.java.launcher.diag"); var0.remove ("sun.cds.enableSharedLookupCache") }} the above content is Integer interview and source code analysis. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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