Hi Anthony

The 2 object instances may be different because they contain different data (they are logically different) or, even if their contents is identical, they may be different because they were created as different objects, so they are located at different addresses in memory (they are physically different, although they can be logically equivalent).

Once upon a time, C language used "pointer" variables: variables that contained the physical address of some structured data. Nowadays programming languages use "handlers": variables that contain an integer value uniquely identifying the address of the object. (In fact, the physical address is no longer reliable on moder computers, as the data can be swapped to the disk and reloaded at another physical address, so the handle is a sort of subscript in an array of physical addresses/pointers that are updated by the system.)

If you write:
   Person person1 = new Person();
then, at runtime, the JVM creates in memory enough space to handle a Person object data and stores the handle in the variable "person1".

If you write:
   Person person1 = new Person();
   Person person2 = new Person();
then two memory areas are rent out from the system and the two handles are stored in the two variables. Then: boolean equ = ( person1 == person2 ); // stores "false", as the two handles are different, as the two pointed objects are at different addresses in the memory.

It is more difficult to tel that two different Person instances are equal because they contain the same data. If it is the case, the test ( person1 == person2 ) will return "false", because even if they contain the same data, their addresses are different.

In order to manage "logical" equivalence instead of "physical" equivalence, the java.lang.Object class (the one inherited by all Java classes) defines the method "public boolean equals( Object );". One looking for the "logical" equivalence, should not test "person1 == person2", but "person1.equals( person2 );".

If you try, you'll find no difference between the two tests. It is just because the generic "equals(...)" methods doesn't know when you wish the two person to be logically equivalent: their names must coincide?, their names and birth dates?, all the stored data?. It is up to you to override the method for your own class to perform the proper tests.

For example, java.lang.String overrides "equals" (and defines also "equalsIgnoreCase" for case insensitive equivalence). Two Strings can be physically different but logically equal. For e.g. try:

   String s1 = new String("abc");
   String s2 = new String("abc");

   System.out.println(s1==s2);
   System.out.println(s1.equals(s2));

You can also try:

   String s1 = "abc";
   String s2 = "abc";

   System.out.println(s1==s2);
   System.out.println(s1.equals(s2));

Surprisingly, there will be "true" for the two tests. In fact, the compiler is smart enough to understand that the two constants "abc" are identical and it stores them as a single instance in memory. "String s1 = "abc";" doesn't duplicate the object, just stores in s1 the handle for the already stored "abc" constant. Same does for s2, so the two variables store the same handle value.

I suggest to take a look to the Java doc for java.lang.Object and java.lang.String. Take a look to the "equals(...)" methods and also to the "hashCode()" ones.

Hope it helps
mihai



Anthony Lam a écrit :
Hi,
Say if I have 2 object instances of a class Person named Person1 and Person2. If these object instance is passed as an argument to a calling method e.g public void method(Person P). Is there anyway inside the method that can identify which is the current Person object Person1 or Person2 that is being passed to the method? Thanks. Regards,
Anthony

------------------------------------------------------------------------
Hotmail: Trusted email with powerful SPAM protection. Sign up now. <https://signup.live.com/signup.aspx?id=60969> -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en

--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to