Hi Anthony
I saw many persons already answered and sent very pertinent tips. In
brief, if you create a variable "Person richard = new Person();", the
name of the variable is useful for you, but you cannot assume that the
program will know it after compilation (at runtime). It is not the name
of the variable, but the data you stored in your Person instance the
thing you must use, as stated by the many people having already answered.
To be more explicit, let's assume you create a Person instance, then
call a method that fills it with data from the database. When you write
the code, you cannot know if it will be "Anthony" the name read from the
database or "Richard". So your code will be something like:
...
Person person1 = new Person();
Person person2 = new Person();
SomeLib.load(person1, 23); // Fill person1 with the data from the
database for personID = 23
SomeLib.load(person2, 55); // ...
if ( person1.name.equals("Richard")){ ... }
...
Hope it helps
mihai
Anthony Lam a écrit :
Hi mihai,
Thanks very much for giving such a detail explanation, but I think I
am not making my question clear enough and I think you answer does not
solve my query. Let me clarify a little bit more of what I am asking.
e.g Person anthony = new Person();
Person richard = new Person();
method(anthony); --- anthony object instance is being passed to
method
method(richard); --- richard object instance is being passed
to method
public void method(Person p) {
within the method's code here. How can I tell the Person
object instance argument passed here is the object
instance of Anthony or object instance of Richard.
I might like to perform if P is Anthony instance then print
out "Hello Anthony"! or else if P is richard instance then I
would print out "Hello Richard" or some other kind of logic
depending on the object passing in.
}
I am sorry for not making the question not clear enough and causing
the confusion here.
Regards,
Anthony
------------------------------------------------------------------------
Date: Mon, 12 Apr 2010 09:50:56 +0200
From: [email protected]
To: [email protected]
CC: [email protected]
Subject: Re: [java programming] Object instance identification
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]
<mailto:[email protected]>
To unsubscribe from this group, send email to
[email protected]
<mailto:[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
------------------------------------------------------------------------
Hotmail: Powerful Free email with security by Microsoft. Get it 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.