JAVA Integer value range, javainteger Value

Source: Internet
Author: User

JAVA Integer value range, javainteger Value

Source: http://hi.baidu.com/eduask%C9%BD%C8%AA/blog/item/227bf4d81c71ebf538012f53.html

Package com. test; public class Test {public static void main (String [] args) {Integer a = 100; // if new is used, the = value must be false Integer B = 100; system. out. println (a = B); // true Integer c = 150; Integer d = 150; System. out. println (c = d); // false }}

 

Why?
1. During java compilation, Integer a = 100; is translated as-> Integer a = Integer. valueOf (100 );
2. The comparison is still the comparison of objects.
3. In the jdk source code

Public static Integer valueOf (int I) {final int offset = 128; if (I >=- 128 & I <= 127) {// must cache return IntegerCache. cache [I + offset]; // when the value range is met, enter the static IntergerCache that is also created, the value of I + offset indicates to retrieve the value of the lower mark in the cache array} return new Integer (I); // when the value range is-128 and 127 is not met. Remember to use: new, open up new memory space, not in the IntergerCache management area}

While

Private static class IntegerCache {private IntegerCache () {} static final Integer cache [] = new Integer [-(-128) + 127 + 1]; // open-128 to 127 of the memory zone. There are 0 locations. Oh, static {for (int I = 0; I <cache. length; I ++) cache [I] = new Integer (I-128); // assign values to each object in the memory array }}

To improve efficiency, java initializes an integer object between-128--127, so the value assignment is within the same range.

  

Add one more sentence
Integer a = 100;
A ++;
// Here, a ++ creates a new object, not a previous object.

Public static void main (String [] args) {Integer a = 100; Integer B = a; // at this time, the B Pointer Points to the heap address with a value of 100, that is, the heap address of, a = B sets up a ++; // at this time, the value pointed to by a changes to 101, and the pointer points to the heap address of 101. B then points to 100 System. out. println (a = B); // false}

 

 


 

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.