Boxed and unboxing of integer and int types in Java

Source: Internet
Author: User

The most important thing about assigning and comparing integers to int is that the two variables are of different types. An integer is a reference type, and an int is a native data type.
We discuss in four different situations:
1) integer and int type assignment
A. Assign an integer type to the int type.at this point, the value of the integer type variable is automatically disassembled into an int type and then assigned to a variable of type int, where the bottom layer is implemented by calling the Intvalue () method to implement the so-called unboxing.
B. Assign the int type to the integer type.at this point, the value of the INT type variable is automatically boxed into an integer type and then assigned to a reference of the integer type, where the bottom layer is to implement the so-called boxing by calling the ValueOf () method.
2) Comparison of integer and int types
It doesn't matter who compares with WHO, the integer = = int and int = = Integer effect is the same, will be the integer type variable is split into int type, then compare, equal to return True, otherwise return false. Again, the Intvalue () method is called by the unboxing.
3) The comparison between integers
This is relatively simple, directly to the two reference value (that is, the address that stores the target data) to compare the line, no more unpacking, boxing or anything.
4) The comparison between int
This, too, directly compares the values of the two variables.
It is important to note that for an integer object, the JVM automatically caches values in the -128~127 range, so all integer objects that have equal values within this range will share a piece of memory without creating multiple The number of integer objects that exceed the value in this range will open up how many memory. The underlying code is as follows:

[Java]View Plaincopyprint?
  1. <span style="FONT-SIZE:24PX;" > public static Integer valueOf (int i) {
  2. assert Integercache.high >= 127;
  3. if (i >= integercache.low && i <= integercache.high)
  4. return integercache.cache[i + (-integercache.low)];
  5. return new Integer (i);
  6. }
  7. </span>


The test code is as follows:

[Java]View Plaincopyprint?
    1. <span style="FONT-SIZE:24PX;" >public class Integerandinttest
    2. {
    3. public static void Main (string[] args) throws Exception
    4. {
    5. Integer A = 127, b = 128;
    6. int c = 128;
    7. //Auto-boxing, this will call the ValueOf method in integer to box C into an integer type
    8. A = C;
    9. //Compare the address of two objects here, which is obviously not equal to
    10. System.out.println (A = = B);
    11. //This will call the integer Intvalue method to get the value stored in the stack address pointed to by the integer object A and then compare the value with C.
    12. //instead of packing C into an integer type for address comparison
    13. System.out.println (A = = c);
    14. ///Ibid. is also the value stored in the stack address of a point
    15. System.out.println (c = = a);
    16. /** 
    17. * Summary: There are two cases of boxing and unpacking only when the value (=) is assigned.
    18. * when comparing (= =) There will only be one case, that is, unpacking, without the case of boxing
    19. */
    20. System.out.println ("----------------------------------");
    21. Integer i1 = 127;
    22. Integer i2 = 127;
    23. System.err.println (I1 = = I2);
    24. I1 = 128;
    25. I2 = 128;
    26. System.err.println (I1 = = I2);
    27. /** 
    28. * Summary: The JVM automatically caches values in the -128~127 range, so all integer objects with equal values within this range will share a piece of memory without opening up multiple;
    29. * How many memory will be opened for an integer object that exceeds the value in this range to save memory consumption.
    30. */
    31. }
    32. }
    33. </span>

Boxed and unboxing of integer and int types 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.