Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wrapper class

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


output:
true2

!!!

why number1== number2 is false?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by reading this recent thread and this older one.
 
Shiva Gajjala
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.


 
Winston Gutkowski
Bartender
Pie
Posts: 10236
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:why number1== number2 is false?

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

Winston
 
Paweł Baczyński
Bartender
Posts: 1762
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Rico Felix
Ranch Hand
Posts: 407
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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 Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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 ??

 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't.
 
Shiva Gajjala
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 48652
56
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 10236
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 407
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic