Research on the use of new integer or integer.valueof

Source: Internet
Author: User
Tags static class

Author: fbysss
Msn:jameslastchina@hotmail.com
Blog:blog.csdn.net/fbysss
Statement: This article by fbysss Original, reprint please indicate the source

Preface:

Recently saw that the use of integer.valueof to replace the new integer more efficient, because the study of the integer source, found that there is a cache available.

I also find out about it. Found that this is in fact related to Java automatic box unboxing, directly using the integer i = numerical method.

It is a more effective way to study the bytecode by byte code. Let's take a look at it:


-------------------
Integer.valueof Source:
-------------------


public static Integer valueof (int i) {
Final int offset = 128;
if (i >= -128 && i <= 127) {//must cache
return integercache.cache[i + offset];
}
return new Integer (i);
}


-------------------
Integercache source Code (JDK1.6.0_12):
-------------------
private Static Class Integercache {
Private Integercache () {}


Static final Integer cache[] = new integer[-(-128) + 127 + 1];//Note this cache is final.


static {
for (int i = 0; i < cache.length; i++)
Cache[i] = new Integer (i-128);
}



Source 1:public class Testint {


/**
* @param args
*/
public static void Main (string[] args) {
int i = 200;
i + +;
}
}
---------------------
javap-c Testint
---------------------
public class Com.sss.newage.test.TestInt extends java.lang.object{
public Com.sss.newage.test.TestInt ();
Code:
0:aload_0
1:invokespecial #8; Method Java/lang/object. " <init> ":() V
4:return


public static void Main (java.lang.string[]);
Code:
0:sipush 200
3:istore_1
4:iinc 1, 1
7:return


}



Source 2:
public class Testint {


/**
* @param args
*/
public static void Main (string[] args) {
Integer i = 200;
i + +;
}
}
---------------------
javap-c Testint
---------------------
public class Com.sss.newage.test.TestInt extends java.lang.object{
public Com.sss.newage.test.TestInt ();
Code:
0:aload_0
1:invokespecial #8; Method Java/lang/object. " <init> ":() V
4:return


public static void Main (java.lang.string[]);
Code:
0:sipush 200
3:invokestatic #16; Method java/lang/integer.valueof: (I) Ljava/lang/integer;
6:astore_1
7:aload_1
8:invokevirtual #22; Method Java/lang/integer.intvalue: () I
11:iconst_1
12:iadd
13:invokestatic #16; Method java/lang/integer.valueof: (I) Ljava/lang/integer;
16:astore_1
17:return


}

We can see:

1. Line 3rd (row marked 3) called integer.valueof, once the number in the cache window can improve efficiency, then we can assume that the cache is not white, indeed better than the new integer.

2.. line 8th (row 8) for the automatic unboxing (intvalue), the purpose is to carry out the number of 1 operations 3. Note that the 13th line, after the completion of the operation, the box (valueof), these operations do not affect the cache itself, so the cache is designed to read only, There is also no thread safety problem.



Source 3:Modify integer i = 200, integer i =integer.valueof (200);
public class Testint {


/**
* @param args
*/
public static void Main (string[] args) {
Integer I =integer.valueof (200);
i + +;
}
}


public class Com.sss.newage.test.TestInt extends java.lang.object{
public Com.sss.newage.test.TestInt ();
Code:
0:aload_0
1:invokespecial #8; Method Java/lang/object. " <init> ":() V
4:return


public static void Main (java.lang.string[]);
Code:
0:sipush 200
3:invokestatic #16; Method java/lang/integer.valueof: (I) Ljava/lang/integer;
6:astore_1
7:aload_1
8:invokevirtual #22; Method Java/lang/integer.intvalue: () I
11:iconst_1
12:iadd
13:invokestatic #16; Method java/lang/integer.valueof: (I) Ljava/lang/integer;
16:astore_1
17:return


}

As you can see, using integer i = 200; with integer i = integer.valueof (200); is equivalent, from the programmer's reading and writing habits, the use of integer i = 200;




------------------------

One thing to note:

------------------------

The latest version of Jdk,integer is rewritten, and Integercache cache Windows can be configured (-xx:autoboxcachemax settings via JVM parameters).

jdk1.6.0_27 related source code

/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on the usage. During VM Initialization the
* Getandremovecacheproperties method is used to get and remove any system
* Properites that configure cache size. At this time, the size of the
* Cache May is controlled by the VM Option-xx:autoboxcachemax=<size>.
*/


Value of Java.lang.Integer.IntegerCache.high property (obtained during VM init)

static void Getandremovecacheproperties () {
if (!sun.misc.vm.isbooted ()) {
Properties props = System.getproperties ();
Integercachehighpropvalue =
(String) Props.remove ("Java.lang.Integer.IntegerCache.high");
if (Integercachehighpropvalue!= null)
System.setproperties (props); Remove from System props
}
}


private Static ClassIntegercache{
static final int high;
Static final Integer cache[];


static {
final int low =-128;


High value may is configured by
int h = 127;
if (integercachehighpropvalue!= null) {
Use Long.decode this to avoid invoking methods
Require Integer ' s autoboxing cache to be initialized
int i = Long.decode (integercachehighpropvalue). Intvalue ();
i = Math.max (i, 127);
Maximum array size is Integer.max_value
h = math.min (i, Integer.max_value--low);
}
High = h;


cache = new Integer[(high-low) + 1];
int j = Low;
for (int k = 0; k < cache.length; k++)
CACHE[K] = new Integer (j + +);
}


Private Integercache () {}
}

This article writing time is hasty, if has the description improper or the argument has the question place, welcome everybody exchanges, correct.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.