For Programmers: Free Programming Magazines  


Home > Archive > Software Engineering > October 2004 > how do I identify different object instances in Java program?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how do I identify different object instances in Java program?
Ji Wu

2004-10-13, 3:56 am

Suppose there is Java code fragement:

void foo( Collection v, int N ) {
for( int i=0;i<N;i++)
{
Stack st = new Stack(); //st is object name
v.add(st); //v is a container
}
...
}

In C++, we can identify each object instance by its identical object
address, but for Java program, there is no object address concept. So how
should I identify those different 'st' at runtime?

Thx.


Wu Ji


Cristiano Sadun

2004-10-13, 3:56 am

"Ji Wu" <w_wuji_j@hotmail.com> wrote in news:cki573$2210$1@mail.cn99.com:

>
> In C++, we can identify each object instance by its identical object
> address, but for Java program, there is no object address concept. So how
> should I identify those different 'st' at runtime?


That's a question for a java group and you find the answers in every basic
java course/manual.

However: one, there is object address concept; only, the address is opaque
- a reference. You just can't do much aritmetics on it. But you can compare
references to see if it is the same instance.

Two, for semantic equality, you've got equals() and hashCode().
Arnaud Bailly

2004-10-13, 3:56 am

Ji Wu wrote:
> Suppose there is Java code fragement:
>
> void foo( Collection v, int N ) {
> for( int i=0;i<N;i++)
> {
> Stack st = new Stack(); //st is object name
> v.add(st); //v is a container
> }
> ...
> }
>
> In C++, we can identify each object instance by its identical object
> address, but for Java program, there is no object address concept. So how
> should I identify those different 'st' at runtime?
>
> Thx.
>
>
> Wu Ji
>
>

In Java, object identity can be checked with the operator == .
Object equality is checked with 'boolean equals(Object)' method and used
extensively in collections. Note that the implementation of equals in
Object class has same semantics as == .

That said, your question should receive much more attention in some java
dedicated newgroup.

Arnaud
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com