File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes wrapper class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "wrapper class" Watch "wrapper class" New topic
Author

wrapper class

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


output:
true2

!!!

why number1== number2 is false?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
Start by reading this recent thread and this older one.
Shiva Gajjala
Ranch Hand

Joined: May 02, 2014
Posts: 35

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

Joined: Mar 17, 2011
Posts: 7487
    
  18

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

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

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 837
    
  14

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

Joined: Mar 08, 2014
Posts: 216
    
    3

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

Joined: May 02, 2014
Posts: 35

Hi Rico Felix,

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

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4338
    
    7

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

Joined: Oct 13, 2005
Posts: 37890
    
  22
You don't.
Shiva Gajjala
Ranch Hand

Joined: May 02, 2014
Posts: 35

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

Joined: Oct 13, 2005
Posts: 37890
    
  22
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

Joined: Mar 17, 2011
Posts: 7487
    
  18

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

Joined: Mar 08, 2014
Posts: 216
    
    3

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...
 
Consider Paul's rocket mass heater.
 
subject: wrapper class
 
Similar Threads
valueOf() and new
The MathQuiz file doesnt seem to run i dont know why
Having the user input numbers separated by commas