• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wrapper class

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


output:
true2

!!!

why number1== number2 is false?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by reading this recent thread and this older one.
 
Ranch Hand
Posts: 77
Eclipse IDE Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi abalfazl hossein, when ever you are comparing content of 2 objects, you can use '==' operator and equals() method.When you use '==' it will compare the address of the object rather than the content in it. Here number1 and number2 are refererring to two different objects ,so their addresses are different.


In this case,you need to use equals() method for comparing the contents.


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:why number1== number2 is false?


Because you didn't read our AvoidTheEqualityOperator (←click) page.

Winston
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also what is worth noting is: javadoc for Integer#valueOf says:
If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int)

In 99,9% of situations you would not need a new instance.
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To give you a deeper understanding lets go under the hood of the language...

When you use the new keyword (new Integer(54)) the jvm requests for memory to store your object on the free store/heap... When this happens the address where the object is stored is assigned to the variable say at memory location 0AF64E so number1 has a value of 0AF64E

So every time you create an object using new its created at a different address

Example:



Armed with this information you can now see that the comparison using the == operator yields false...



To test if two object are meaningfully equal (if there states are equal), you must implement or use the equal(Object) method...



This yields true because the underlying code uses deep comparison

 
Shiva Gajjala
Ranch Hand
Posts: 77
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rico Felix,

Could you please tell me how to find the address of an object .What method to use ??

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shiva Gajjala wrote:
Could you please tell me how to find the address of an object .What method to use ?



You can't, but you shouldn't need to. Rico just used the address to explain why it works as it does.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't.
 
Shiva Gajjala
Ranch Hand
Posts: 77
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mathew , when I worked on JDBC I could see the address of the connection object.


here i could see the address where 'con' is referring to.But when I try to print the Integer object it was printing the value in it.

output - 54
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shiva Gajjala wrote:Hi Mathew , when I worked on JDBC I could see the address of the connection object. . . .

No, you didn't.

What you saw was the hashcode, which does not necessarily point to the address at all.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shiva Gajjala wrote:Could you please tell me how to find the address of an object .What method to use ??


You can't. For starters, Java uses references, not addresses, since it has no idea what type of system it's going to be running on at the time it's compiled. It's the job of the JVM to translate those references into memory addresses as and when it needs to - and it certainly won't tell you what they are (indeed they may well change during the lifetime of the object).

The whole point of Java is that you don't need to be concerned about things like memory addresses, because the language deals with it for you; in the same way that it removes objects when they're no longer needed. It's quite a difficult thing to let go of when you've come from a language like C or C++ but, believe me, you'll be a lot happier when you do.

Winston
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shiva Gajjala wrote:Hi Rico Felix,

Could you please tell me how to find the address of an object .What method to use ??



As the elders have stated, this should not be of concern to the programmer using the Java programming language... I only used that explanation as an example to illustrate logically why you were getting such results from your code. The main purpose of the language is enhance productivity drawing you away from having to worry about memory management (addresses in your case), pointers and the like...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic