ValueOf () and praseint () differences between integers

Source: Internet
Author: User

First look at the implementation of valueof ()

public static Integer ValueOf (String s) throws NumberFormatException {
        return integer.valueof (parseint (s));
    }
public static Integer valueOf (int i) {
assert integercache.high >= 127;
if (i >= integercache.low && i <= integercache.high)
return integercache.cache[i + (-integer        Cache.low)];
return new Integer (i);
}
So it can be seen as:

New Integer (Integer.parseint (s)) 
In other words, the result of parseint is encapsulated once and the return value is integer

You can also take a look at integer and int unpacking

        int i = 128;  Integer i2 = 128;
        Integer i2 = integer.valueof (128);
        Integer i3 = new Integer (128); System.out.println ("i = = I2:" + (i = = I2)),//integer automatically unboxing to int, so true System.out.println ("i = = i3:" + (i = = i3));//i Nteger automatically unboxing to int, so true System.out.println ("i2 = = i3:" + (I2 = = i3));//new integer = = new Integer (128), so Fals E//For the number between 128 to 127, will be cached, integer i5 = 127, 127 will be cached, the next time you write an Integer I6 = 127, will be directly from the cache, will not be new Inte
        GER i5 = 127;//java at compile time, translated into Integer i5 = integer.valueof (127);
        Integer I6 = 127;
        SYSTEM.OUT.PRINTLN ("i5 = = I6:" + (i5 = = I6));
        Integer i52 = 128;
        Integer i62 = 128;
        
        System.out.println ("i52 = = i62:" + (i52 = = i62));
        Integer ii5 = new Integer (127); SYSTEM.OUT.PRINTLN ("i5 = = Ii5:" + (i5 = = ii5));
        Because the objects are different, false integer i7 = new Integer (1);
        Integer i8 = new Integer (1); System.out.println ("i7 = = I8: "+ (i7 = = i8)); False because the object is not the same



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.