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]
# 1
Run it and find out?
Djaunla at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 2
> Run it and find out?And how is that supposed to help? The program doesn't automagically print out how many objects were created.
floundera at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 3
There's no such thing as a "null constructor." That is, we don't use that term in Java.That code creates two objects.
jverda at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 4
> > 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.
Djaunla at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 5

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.

sd4139a at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 6

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

jverda at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 7
Thanks guys..appreciate it.
java80a at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 8
> Thanks guys..appreciate it.But do you understand why your thought that three were created was incorrect?
jverda at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 9
> Thanks guys..appreciate it.Try reading through the "stories" here: http://www.javaranch.com/campfire.jspThey might help explain it.
CaptainMorgan08a at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 10

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

sd4139a at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 11

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

jverda at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 12
More common than I thought then :-) Oh well, sufficient for our purposes anyway, I guess.
sd4139a at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 13
>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!!!!
java80a at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 14
> 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.
jverda at 2007-7-9 5:56:04 > top of Java-index,Java Essentials,New To Java...
# 15

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.

Java@kondala at 2007-7-9 5:56:05 > top of Java-index,Java Essentials,New To Java...