Java summary of the underlying type and the constant pool

Source: Internet
Author: User
Tags float double

1, the underlying type has a byte short int long char Boolean float double eight.

Where the wrapper type of byte short int long char is stored in Chang (used to maintain an ordered set of constants used by the type), and the floating-point type does not implement a constant pool.

Constant pools hold data in >=-128 and <=127, and are not maintained in a constant pool when the data is not within that range

  

Integer a=new integer,integer b=new integer (128);

A==b? False because A, b exceeds the 127 limit and does not exist with a constant pool, so the references are different.

 Public static integer valueOf (int  t) {      finalint offset=128;       if (t>=-128&&t<=127) {         return integercache.cache[i+offset];    }        return New Integer (i)  }

The ValueOf method shows that the integer type is taken from a constant pool in the -128~127 range.

2, String a=new string ("BB");

You may have created an object, or you may have created two objects.

During class compilation, a constant pool is first searched for a "BB" object, if any, a copy is copied in the stack, and if not, a copy is created in the constant pool and then copied to the heap. The reference is then handed to s in the stack.

 String A = "A1" ; String b  = "a" + 1; System.out.println ((a  = = b)); // result = True  String a = "atrue" ; String b  = "a" + "true" ; System.out.println ((a  = = b)); // result = True  String a = "a3.4" ; String b  = "a" + 3.4; System.out.println ((a  = = b)); // result = True   analysis: The JVM joins the  "+" sign of a string constant, and the JVM optimizes the "+" connection of the constant string to the concatenated value, with "a" + 1 for the program compile time. After the compiler is optimized, it is already A1 in class. The value of its string constants is determined at compile time, so the final result of the above program is true. 
String a = "AB"= "B"= "a" +//result = False Parse: JVM for string reference, because in string "+" In the connection, there is a string reference exists, and the value of the reference in the program compilation period is not determined, that "a" + BB can not be optimized by the compiler, only during the program run time to dynamically allocate and the new address after the connection to B. So the result of the above program is also false.
String a = "ab"final String bb = "B"= "a" +//result = True  Analysis:The only difference between [3] is that the BB string has a final decoration, and for a final modified variable, it is parsed at compile time to a local copy of the constant value stored in its own constant pool or embedded in its byte stream. So at this point the "a" + BB and "a" + "B" effect is the same. Therefore, the result of the above program is true.

It can be seen that when a string + constant, the value of the link can be optimized at compile time. When the string + reference, the compilation period cannot be determined, can not be optimized

  

Java summary of the underlying type and the constant pool

Related Article

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.