Simple question for objects
Hi Friends,
Got a simple questions,how many objects are created by this code:
Assume:
Test is aclass with anull constructor
Test obj1;
Test obj2;
Test obj3 =new Test();
obj1 =new Test();
obj2=obj1;
As per my guess,I think the answer is : 3
Rite or wrong?
[457 byte] By [
java80a] at [2007-11-26 18:22:11]

> Run it and find out?And how is that supposed to help? The program doesn't automagically print out how many objects were created.
There's no such thing as a "null constructor." That is, we don't use that term in Java.That code creates two objects.
> > Run it and find out?> > And how is that supposed to help? The program doesn't> automagically print out how many objects were created.Good point. My apologies for the unhelpful reply.
If you add:
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
To the end of your code, then look at the output you'll see something like:
Test@<A Hex String>
Test@<A Hex String>
Test@<A Hex String>
The hex strings are memory addresses. The number of distinct addresses is the number of objects you have created. Hope that helps.
> The hex strings are memory addresses. The number of
> distinct addresses is the number of objects you have
> created. Hope that helps.
Not necesarily. The default hashCode is generally based on the address, but doesn't necessarily equal the address or even have any relation to it. Two distinct Objects can have the same hashcode.
Thanks guys..appreciate it.
> Thanks guys..appreciate it.But do you understand why your thought that three were created was incorrect?
> Thanks guys..appreciate it.Try reading through the "stories" here: http://www.javaranch.com/campfire.jspThey might help explain it.
> > The hex strings are memory addresses. The number
> of
> > distinct addresses is the number of objects you
> have
> > created. Hope that helps.
>
> Not necesarily. The default hashCode is generally
> based on the address, but doesn't necessarily equal
> the address or even have any relation to it. Two
> distinct Objects can have the same hashcode.
Well yeah, but close enough, and I only just got out of bed :-)
Hash collisions for that kind of thing would be pretty rare, wouldn't they?
> > distinct Objects can have the same
> hashcode.
>
> Well yeah, but close enough, and I only just got out
> of bed :-)
> Hash collisions for that kind of thing would be
> pretty rare, wouldn't they?
I got a collision in less than 2000 objects in my first run, and 5 collisions in about 20,000:
import java.util.*;
public class HC {
public static void main(String[] args) {
Map<Integer, List><Object>> map = new HashMap<Integer, List><Object>>();
for (long ix = 0; true; ix++) {
Object obj = new Object();
int hc = obj.hashCode();
if (map.containsKey(hc)) {
List<Object> list = map.get(hc);
list.add(obj);
System.out.println("Iteration " + ix + ", hc=" + hc + ", count="
+ list.size());
}
else {
List<Object> list = new ArrayList<Object>();
list.add(obj);
map.put(hc, list);
}
}
}
}
Iteration 1785, hc=9578500, count=2
Iteration 2084, hc=14850080, count=2
Iteration 12731, hc=31489342, count=2
Iteration 14971, hc=27940859, count=2
Iteration 20503, hc=14274282, count=2
More common than I thought then :-) Oh well, sufficient for our purposes anyway, I guess.
>But do you understand why your thought that three were created was >incorrect? Yes, I got it Jverd.Actually when I do,obj2 = new Test();obj3 = obj2 ;I created 2 references to the same object.Got it...you are the best!!!!
> obj2 = new Test();> obj3 = obj2 ;> > I created 2 references to the same object.Yup.> Got it...you are the best!!!!Sheoot. 'Twarn't no thang.
only 2 objects r created. sure.
beacuse:
Test obj1; //this is only ref in stack
Test obj2; //this is only ref in stack
Test obj3 = new Test();// 1st object created in heap
obj1 = new Test();// 2 nd object created in heap.
obj2=obj1;//obj1 and obj2 refernces to same memory in heap.
so result=2.