+ 9
References and Objects
How many references and objects in this code : Circle[] circle = new Circle[10];
17 Answers
+ 8
Well since we have an array of size 10, so we have 10 references and each reference is pointing to one object so we have 10 objects.
+ 7
Mays Qunaibi In Java you donot need to initialize the array.
Their default value will be zero/null depending on the data type.
+ 7
Mays Qunaibi There was a little confusion
Circle[] circle = new Circle[10];
Above line creates an array of 10 references of type circle which have null value (or you can say they are pointing to null)
To initialize them you can use for loop
for (int i = 0; i < 10; i++)
circle[i] = new Circle();
Now each reference in the array will be pointing to object.
+ 5
I think
1 reference: circle is the reference to the array.
10 objects: The array have 10 objects of type Circle stored/ initialized.
+ 4
I want to know this also !!!
+ 3
Anybody can help us ??
+ 3
but the objects in the array not initialized yet they just have referencesš§š¤š¤
+ 3
nAutAxH AhmAd i mean arrays which stord objects....stord the references of thier objects ....here we creat an array but didnāt creat any objects...just didnāt assign itš¤š¤
+ 3
Yes in this case that we are just create an array without assign any value ..can we say it has 10 objects or it has 10 references ?
+ 3
zemiak so do u think thereās 10 null objectās are already created...??š¤
+ 3
zemiak mmmm...maybe ..but you still didnāt spicified ...if āitās described as reference to nothingā.. so will count it with reference or objects ??thatās what i confused with when iām trying to answer this todayššthanks for discussšø
+ 3
nAutAxH AhmAd so what is the final answer ?what is the count of objects and how much references we have ?
+ 3
nAutAxH AhmAd cool... so do u think the reference of the array count with this 10 referencses ??
+ 1
null is not object, it has not class, rather it is value or type,it is described as "reference" to nothing, but actually there is no address referenced to place in memory.
0
there is 1 reference to object in circle variable, 10x null in array are not references
class Circle {}
Circle[] circle = new Circle[10];
int i=0;
System.out.println(circle);
for(var c : circle){
System.out.println("["+i++ +"] "+c);
}
//output
[LProgram$1Circle;@77468bd9
[0] null
[1] null
[2] null
...
[8] null
[9] null
0
Salut