Chang of basic data types and string type constant pool parsing

Source: Internet
Author: User

To throw a sample:

Integer a1 = new integer (123);
Integer a2 = new integer (123);
SYSTEM.OUT.PRINTLN (a1 = = A2); False because whenever a new keyword is encountered, it will definitely reallocate space to the instance object in the heap, so two addresses must be different

A1 = 123;
A2 = 123;
SYSTEM.OUT.PRINTLN (a1 = = A2); True because there is an inner class in integer, Integercache is used to store data between 128 and 127, if the data defined by the integer is inside this, then the same location data is pointed to

a1 = 234;
A2 = 234;
SYSTEM.OUT.PRINTLN (a1 = = A2); False integer inside class Integercache can only save 128 to 127 of data, if the limit is exceeded, then can not get results from the cache, only through new to allocate space, so point to a different address

int a3 = new Integer (123);
int a4 = new Integer (123);
SYSTEM.OUT.PRINTLN (a1 = = A2); False int is the basic data type and also encounters new to reassign space, pointing to a different address

The following int's understanding can also be understood in a constant pool, stored in a constant pool, pointing to the same position in the constant pool
A3 = 123;
a4 = 123;
SYSTEM.OUT.PRINTLN (a3 = = A4); True int, as the basic data type, puts the data confidant on the stack to manipulate it, but compares the two values for equality,

a3 = 234;
a4 = 234;
SYSTEM.OUT.PRINTLN (a3 = = A4); True int as the basic data type, will put the data confidant on the stack to operate, just compare the two values are equal, there is no package class 128 to 127 range problem

Integer A5 = new Integer (0);
SYSTEM.OUT.PRINTLN (A1 = = a2 + A5); True when an addition operation is performed, the wrapper class is first disassembled and then compared on the stack, so only the two values of the comparison are equal

Further extended:

A constant pool is actually a memory space, unlike the heap space where objects created with the New keyword are located.

8 basic types of wrapper classes and object pools

Most of the basic types of wrapper classes in Java implement constant pool techniques, which are Boolean, Byte,Character,Short,integer, Long the other two types of floating-point type wrapper classes are not implemented . Also byte,short,integer,long,character these 5 types of integer wrapper classes can use object pooling only when the corresponding value is less than or equal to 127, which is why the object is not responsible for creating and managing objects greater than 127 of these classes.

Specific jdk1.7 of the source code:

Byte implements the constant pool source code: Cache the data between [-128, 127]

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

Character implementation of the constant pool source: The cache is [0, 127] between the data

private Static Class Charactercache {
Private Charactercache () {}

Static final Character cache[] = new character[127 + 1];

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

Short implementation of the constant pool source: The cache is the data between [-128, 127]

private Static Class Shortcache {
Private Shortcache () {}

Static final short cache[] = new short[-(-128) + 127 + 1];

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

Integer implementation of the constant pool source: The cache is the data between [-128, 127]

private Static Class Integercache {
static final int low =-128;
static final int high;
Static final Integer cache[];

static {
High value is configured by property
int h = 127;
String Integercachehighpropvalue =
Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high");
if (integercachehighpropvalue! = null) {
int i = parseint (Integercachehighpropvalue);
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 () {}
}
A long implementation of the constant pool source: The cache is the data between [-128, 127]

private Static Class Longcache {
Private Longcache () {}

Static final Long cache[] = new long[-(-128) + 127 + 1];

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

Constant pool parsing of string

The constant pool in Java is actually divided into two forms: a static constant pool and a run-time pool .

The so-called static constant pool, the constant pool in the *.class file, is a constant pool in a class file that contains not only string literals, but also classes, methods, and most of the space in the class file.

running A constant pool is a JVM virtual machine that, after completing the class mount operation, loads the constant pool in class file into memory and saves it in the method area , which is what we often call a const pool, which refers to the run-time pool in the method area.

Next we refer to some examples of the popular constant pools on the web, and then we explain them.

1 String s1 = "Hello";2 String s2 = "Hello";3 String s3 = "Hel" + "Lo";4 String s4 = "Hel" +New String ("Lo");5 String s5 =New String ("Hello");6 String s6 =S5.intern ();7 String s7 = "H";8 String s8 = "Ello";9 String S9 = s7 + S8; 10 11 System.out.println (S1 = s2); // True12 System.out.println (S1 = S3); // True13 System.out.println (S1 = S4); // False14 System.out.println (S1 = S9); // False15 System.out.println (s4 = S5); // False16 System.out.println (S1 = S6); //           

First of all, in Java, the direct use of the = = operator, compared to two strings of the reference address, not the comparison content, compare the content please use String.Equals ().

S1 = = S2 This very good understanding, S1, S2 in the assignment, all use the string literal, white point, is to write the string directly to die, during the compilation, this literal will directly into the class file in the constant pool, so as to achieve reuse, loaded into the run-time pool, S1, S2 points to the same memory address, so it is equal.

S1 = = S3 This place has a hole, S3 although the dynamic splicing out of the string, but all the parts involved in the stitching is known literal, during the compilation, this splicing will be optimized, the compiler directly help you spell, so string s3 = "Hel" + "lo"; The class file is optimized to string s3 = "Hello", so S1 = = S3 is established.

S1 = = S4 Of course not equal, S4 although also splicing out, but the new string ("Lo") this part is not known literal, is an unpredictable part, the compiler will not be optimized, you must wait until run time to determine the results, combined with the string invariant theorem, The ghost knows where the S4 is assigned, so the address must be different . With a schematic diagram to clarify the idea:

S1 = = S9 is not equal , the truth is similar, although S7, S8 in the assignment when the string literal used, but stitching into S9, S7, S8 as two variables, are unpredictable, compiler is compiler, it is not possible when the interpreter, so do not optimize, wait until the run, S7, S8 The new string, the address in the heap is not deterministic and cannot be the same as the S1 address in the method area constant pool .

S4 = = S5 have no explanation, absolutely not equal, both are in the heap, but the address is different.

S1 = = S6 These two equals are entirely attributed to the Intern method, S5 in the heap, the content is Hello, the Intern method attempts to add the Hello string to the constant pool, and returns its address in the constant pool, because the constant pool already has a Hello string, So the Intern method returns the address directly, whereas S1 points to the constant pool at compile time , so S1 and S6 point to the same address, equal.

Chang of basic data types and string type constant pool parsing

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.