File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Auto boxing confusion

 
Aravindh Vin
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Aravindh Vin
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm not sure i like that behavior. it could potentially cause an elusive bug. hence the original post.
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why? Everybody knows you should use equals for comparing objects anyway.
 
Aravindh Vin
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I understood.. Thanks Phil !!

Rob
I am preparing for OCJP in that I was not able understand this code snippet.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignore - I misread Campbell's post
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urgh. Double post.
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic