References to integer objects in Java

Source: Internet
Author: User

There is no pointer in Java, but there is a concept of reference. The main point here is whether an integer is the same object.

1, first look at a section of code:

public static void Main (string[] args) {Integer A1 = 100;integer B1 = a1;//Another can also b1=100field field = null;try {field = A1.G Etclass (). Getdeclaredfield ("value"); catch (Nosuchfieldexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (SecurityException e) {//To Do auto-generated catch Blocke.printstacktrace ();}            Field.setaccessible (true);          try {field.set (A1),} catch (IllegalArgumentException e) {//TODO auto-generated catch Blocke.printstacktrace ();} CA TCH (illegalaccessexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}          System.out.println ("b1=" +b1);                Integer C1 = +;        System.out.println ("c1=" +c1);        

Results:

b1=5000
c1=5000

From above, first here to illustrate a few,

1), for the integer,-128-127 between the integral type has been initialized in the Integercache, if it is boxed, it will be taken from the object.

2), B1=A1 is the number assignment or the same object? This can be seen from the actual results, B1 and A1 point to the same object, not the same value

3), c1=100, indicating that the value between 128-127 is the object obtained from the Integercache, 100 corresponding to the integer object is changed, the subsequent packing for 100 is changed. This is because an array index is used instead of a numeric comparison to get the object in the cache.

However, it would be dangerous to modify this cache, not mind. Who knows what jar package or what platform to a 100 of boxing, but the result is not 100, when the crash.


2, through the above description, then if changed to this is what the answer

public static void Main (string[] args) {Integer A1 = 200;integer B1 = A1; Field field = null;try {field = A1.getclass (). Getdeclaredfield ("value");} catch (Nosuchfieldexception e) {//TODO Auto-gen Erated catch Blocke.printstacktrace ();} catch (SecurityException e) {//TODO auto-generated catch Blocke.printstacktrace ();}            Field.setaccessible (true);          try {field.set (A1),} catch (IllegalArgumentException e) {//TODO auto-generated catch Blocke.printstacktrace ();} CA TCH (illegalaccessexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}          System.out.println ("b1=" +b1);                Integer c1 = n;        System.out.println ("c1=" +c1);        

3, then change it again

public static void Main (string[] args) {integer a1 = new integer, Integer b1 = A1; Field field = null;try {field = A1.getclass (). Getdeclaredfield ("value");} catch (Nosuchfieldexception e) {//TODO Auto-gen Erated catch Blocke.printstacktrace ();} catch (SecurityException e) {//TODO auto-generated catch Blocke.printstacktrace ();}            Field.setaccessible (true);          try {field.set (A1),} catch (IllegalArgumentException e) {//TODO auto-generated catch Blocke.printstacktrace ();} CA TCH (illegalaccessexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}          System.out.println ("b1=" +b1);                Integer C1 = +;        System.out.println ("c1=" +c1);        
This is what the answer. For the operation of new, it is not boxed, but the object is generated in the heap.

Understanding the boxing, caching, reference is not difficult to understand. You can try it yourself.


References to integer objects in Java

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.