Small traps for basic data type wrapper classes in Java constant pool

Source: Internet
Author: User
Tags first string

Presumably most people who have studied Java should have done this:

1  Public classTest {2      Public Static voidMain (string[] args) {3         //First String4String s1= "Hello";5         6         //a second string7String s2= "Hello";8         9         //Compare if S1 and S2 are the sameTenSystem.out.println (s1==S2); One          A          -         /** - * Modify Variables the          */ -          -s1=NewString ("Hello"); -          +S2=NewString ("Hello"); -          +         //compare S1 and S2 again ASystem.out.println (s1==S2); at     } -};

The final result

Figure A

For this result, I believe no one has any objection! Because the first group relies on the result of a constant pool, where string s1= "Hello" actually creates a string constant "Hello" object in a constant pool in an in-memory stack, when string s2= "Hello", because the constant pool already exists, S2 points directly to the same " Hello "objects, that is, the address values of the S1 and S2 objects are consistent, at this point the string is somewhat similar to the basic data type, so when compared with" = = ", the value is" true ".

And if String s1=new string ("Hello"), S1 in the heap to open up new memory space to hold "Hello" value, similarly, S2 will also re-open in the heap of space "Hello" value, S1 and S2 point to the value of different addresses, with "= =" The comparison is naturally different and the value is "false".

But don't know how many people have done the following question?

Modify the variable type in the previous question to be integer.

1  PackageAction;2 3  Public classTest {4      Public Static voidMain (string[] args) {5         //First Value6Integer s1=127;7         8         //a second value9Integer s2=127;Ten          One         //Compare if S1 and S2 are the same ASystem.out.println (s1==S2); -          -          the         /** - * Modify Variables -          */ -          +s1=128; -          +s2=128; A          at         //compare S1 and S2 again -System.out.println (s1==S2); -     } -};

What would your answer be?

The result diagram is as follows:

Figure II

Is it a little unexpected to you?

In principle, both the integer and string types are reference types, then the final result should be the same as the string type, and even if there is no constant pool of integer type technology then this two times the result should be consistent ah, after all, only a difference of 1!

This is my blog to say the Java constant pool of small traps!

There are 6 types of wrapper classes for basic data types in Java that implement the constant pool technology, namely:Byte, short,Integer,Long ,characher , Boolean. Double and Float are not implemented, as evidenced by:

Might

Directly define the constant will be error ( This corroboration is actually a bit unreliable, if anyone knows, trouble to tell a voice, thank you!) )

In the above mentioned 6 kinds of packaging with constant pool technology, in addition to the Boolean, the other value can not exceed 127 (less than or equal to) , more than 127 constant pool will not automatically create objects, the wrapper class and the normal reference type is not different.

A detailed introduction to the constant pool can be borrowed from this blog (and I get the reason for it):

Http://www.cnblogs.com/qinqinmeiren/archive/2011/07/19/2151683.html

Small traps for basic data type wrapper classes in Java constant pool

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.