• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Wrapper

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer i1= 1000;
Integer i2= 1000;
if(i1!=i2){System.out.println("different objects");
if(i1.equals(i2))
{
System.out.println("Same objects");
}

produces the O/P

different objects
Same objects


Can anyone Please Explain.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi thiru,

Thats because, Integer.equals() methods, test the primitive value of the integers, if the both Integer object's primitive values are same, then method returns true.

For '=' comparision, you are directly comparing object's references, java fundamentals, two different objects are not same, though its values are same, they are different objects in memory.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to the above explanation please refer to the following article. The first couple of paragraphs give you the answer but if you bear few more minutes then it will help you to clear this point.


http://www.ibm.com/developerworks/java/library/j-jtp05273.html



Note: I personally suggest reading this article fully because it will help you in collections API.
[ October 12, 2007: Message edited by: Mayur Ramgir ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,

following wrapper objects will always be == when their primitive values are the same:
1. Boolean
2. Byte
3. Character from \u0000 to \u007f (7f is 127 in decimal)
4. Short and Integer from -128 to 127
 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thirumalai Muthu:
Integer i1= 1000;
Integer i2= 1000;
if(i1!=i2){System.out.println("different objects");
if(i1.equals(i2))
{
System.out.println("Same objects");
}

produces the O/P

different objects
Same objects


Can anyone Please Explain.



Can you assign a literal like that? ie. Integer i1 = 1000?
Shouldnt it be something like Integer i1 = new Integer(1000)?
[ October 12, 2007: Message edited by: O. Ziggy ]
 
Thirumalai Muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,it is perfectly legal in Java 5 to do the following

Integer i1=1000;

Unlike Java 1.4 where it has to be Integer i1=new Integer(1000);

In Java 5 the literal is automatically wrapped into an object without the use of the "new" operator.

Other Members Please correct me if I am Wrong.Thanks
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic