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.
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
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 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.
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...