aspose file tools*
The moose likes Java in General and the fly likes Auto boxing confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Auto boxing confusion" Watch "Auto boxing confusion" New topic
Author

Auto boxing confusion

Aravindh Vin
Greenhorn

Joined: Dec 17, 2012
Posts: 3
Someone please help me

Code:
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1 == i2) System.out.println("Same objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");
output:

different objects
meaningfully equa

Code:
Integer i3 = 10;
Integer i4 = 10;
if(i3 != i4) System.out.println("different objects"); //I expect this to be true.
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");

output:

same object
meaningfully equal


could not understand != behavior.. Thanks in Advance!!
Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Java has a pool of Integer objects (-127 to 128). If you request an Integer with one of these values without explicitly telling Java you want a new Integer it will point your reference at the pool Integer with the value requested. Try this:

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to CodeRanch Aravindh Vin
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i'm not sure i like that behavior. it could potentially cause an elusive bug. hence the original post.


SCJP
Visit my download page
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Why? Everybody knows you should use equals for comparing objects anyway.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Aravindh Vin
Greenhorn

Joined: Dec 17, 2012
Posts: 3
Now I understood.. Thanks Phil !!

Rob
I am preparing for OCJP in that I was not able understand this code snippet.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Phil English wrote:Java has a pool of Integer objects (-127 to 128). . . .
That is what everybody things, but it is not quite true. It is not -128…127 (you had the two numbers swapped), but -128…127 and maybe more. See this method, which by the way, you should regard as the preferred way to create an Integer object.
Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Campbell Ritchie wrote:
Phil English wrote:Java has a pool of Integer objects (-127 to 128). . . .
That is what everybody things, but it is not quite true. It is not -128…127 (you had the two numbers swapped), but -128…127 and maybe more. See this method, which by the way, you should regard as the preferred way to create an Integer object.


1. Re: 127/128. Duh. Oops. Will attempt to make myself remember that.
2. Interesting. I was looking in the JLS for the exact specification of the pooling method but I couldn't find anything of much use. Looking around more I found this link which shows this extra functionality. It seems like you can set the AutoBoxCacheMax property to increase the upper limit of the cache but it seems the lower limit is fixed at -128 and that the cache needs to be a continuous sequence, i.e. not [-128:127, 1000:1128].

Thanks for the info.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Phil English wrote: . . . I found this link which shows . . . you can set the AutoBoxCacheMax property to increase the upper limit of the cache . . . .
Well done finding that. There are lots of similar old discussions on the Ranch, but I can’t seem to find them at the moment. I can’t find any evidence of an AutoBoxCacheMin option, as you implied.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Ignore - I misread Campbell's post


Joanne
Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Urgh. Double post.
Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Campbell Ritchie wrote:
Phil English wrote: . . . I found this link which shows . . . you can set the AutoBoxCacheMax property to increase the upper limit of the cache . . . .
Well done finding that. There are lots of similar old discussions on the Ranch, but I can’t seem to find them at the moment. I can’t find any evidence of an AutoBoxCacheMin option, as you implied.


Sometimes I think being able to sum up problem or question in 3-5 words [and google it] is one of the most important skills one can have nowadays but most the time I am ashamed by how much of what I know/do depends on it (see every non-trivial VBA script I ever wrote )

Edit: removed all the stuff based on a misreading of Min and Max probably not a good sign for my future programming prospects...

It does seem odd to only have flexibility one way. It also seems strange (although probably more explicable: complexity of coding etc.) that you have to have a sequential cache. I guess cache size is a tradeoff between instantiation and runtime costs. For example if I knew I was going to use 256, 512 and 1024 a lot then I would have to have all values up to 1024 cached.

Anyhow, I am probably overthinking this and should go back to my Design Patterns.
Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Joanne Neal wrote:Ignore - I misread Campbell's post


Ha. As did I. I didn't even notice until I was kicking myself for getting ninja'd by spending too long riffling through class files and the internet in general.

Now I have to scrap out my double post and my actual post. At this rate I'll be one of those people with a post count of 1000 and an average post length of 4 words.
Aravindh Vin
Greenhorn

Joined: Dec 17, 2012
Posts: 3
Similar problem.. What is the output of the below code and explain why? Thanks !!

package dumps;


class Profile{

private int a;

public Profile(int a){
this.a=a;
}

private Boolean equals(Profile p){
return p.a==this.a;
}

}


public class AA{

public static void main(String args[]){
Profile pf1=new Profile(4);
Profile pf2=new Profile(4);
Object ob=pf1.equals(pf2);

Integer i1=new Integer(222);
Integer i2=new Integer(222);
System.out.println("Profile "+ob);
System.out.println("Integer "+i1.equals(i2));
}

}
Phil English
Ranch Hand

Joined: Jun 18, 2012
Posts: 62

Aravindh Vin wrote:Similar problem.. What is the output of the below code and explain why?


Why don't you have a go first? You must be able to get the output as a minimum
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Auto boxing confusion