0

Suppose there is a class MyObject which has a MyClass() constructor and is properly implemented.

When we call that line of code will it create instances of the MyClass object or will something else happen?

Edit: apparently this question was not very well-received. I'm sorry if it's vague or something. It was simply a homework question that asks for T/F.

I meant to ask: if we have MyClass[][] x = new MyClass[n][n]; // where n is a number Will it create n*n instances of MyClass objects or merely n*n null references?

It turns out that

MyClass[][] x = new MyClass[n][n]; // where n is a number
x[0][0] = new MyClass();

is different from

MyClass x = new MyClass();
user3511965
  • 195
  • 1
  • 3
  • 9
  • 2
    Have you tried it? What if you had a constructor that actually takes arguments? Would the behavior change? – Sotirios Delimanolis Apr 09 '14 at 02:31
  • 1
    It creates 100 nulls. – SLaks Apr 09 '14 at 02:32
  • 4
    This question appears to be off-topic because simply trying it would yield the answer. – Brian Roach Apr 09 '14 at 02:32
  • If I did int[][] x = new int[10][10], doing System.out.println(x[0][0]) would give me 0 because primitives have 0 by default. So I guess class objects would have null then? – user3511965 Apr 09 '14 at 02:36
  • Not to mention, you tell us it's homework (thanks) but you don't have any evidence of doing any research. – DeadChex Apr 09 '14 at 02:36
  • 2
    It creates instances of the array classes. One array of `MyClass[]` references, and ten arrays of `MyClass` references. It does not create any instances of `MyClass`. – Hot Licks Apr 09 '14 at 02:37
  • Sorry, I would usually put a homework tag, but since there isn't one here then I assumed it wouldn't matter. I guess I'm wrong. – user3511965 Apr 09 '14 at 02:38
  • This seems hypothetical so I can't really test it out. – user3511965 Apr 09 '14 at 02:38
  • 1
    @BrianRoach - That's a little unfair, as it's hard to tell what's been created in this case, if you're not pretty good at Java already. – Hot Licks Apr 09 '14 at 02:38
  • 1
    No, this question would be received the same way even if it weren't homework. You could find out, by trying it yourself, in far less time than it took you to type the question. – Dawood ibn Kareem Apr 09 '14 at 02:39
  • 1
    The *homework* tag is deprecated -- you do not need to use it. – Hot Licks Apr 09 '14 at 02:39
  • 2
    When you allocate an array of references, the references are inited to `null`. – Hot Licks Apr 09 '14 at 02:40
  • @DavidWallace I assume they would create instances, but I'm only 90% sure. And I think it's more complicated that it looks. I was just expecting a Yes/No answer but I guess this is how it ends up. – user3511965 Apr 09 '14 at 02:41
  • @HotLicks Does that mean it's an array of null or an array of null objects? (Correct me if something doesn't make sense there) – user3511965 Apr 09 '14 at 02:42
  • 2
    No such thing as a null object. – Dawood ibn Kareem Apr 09 '14 at 02:44
  • 2
    No such thing as a null object -- `null` is a *value* that a reference can take. And a *reference* is **not** an *object*, but rather a pointer to an *object*. It's important to understand the difference between a *reference* (pointer in C, et al) and an *object*. – Hot Licks Apr 09 '14 at 02:50
  • 1
    (This is really an area where many get confused, and some are lost forever. It's a good one to spend some time on -- it's been worth it, in spite of the downvotes.) – Hot Licks Apr 09 '14 at 02:57
  • Not about programming? Please. Voting to reopen. – user207421 Apr 09 '14 at 04:03
  • I think it's because of point 4 in the numbered list on the help centre page. – Dawood ibn Kareem Apr 09 '14 at 04:13

1 Answers1

1

Each slot in the Array would be initially null if the Array is of any object (Primitive data types would simply yield their default value). Just like String x; where x would be null, just in this case, it's an array of null values.

The Array is still the same type of objects it was created for, such a String, just all the slots are null and would need to be instantiated. eg bigArray[1] = new String("Hello!");

If you'd like the array to contain some sort of default, you'll need to fill the array.

MyObject array = new MyObject[3]; //New array that can hold three
for(int i = 0; i < array.length; i++){ //Start i at zero, while it's less than the spots in the array, and add one every time
    array[i] = new MyObject(); //Set the spot to a "real" object now.
}
DeadChex
  • 4,379
  • 1
  • 27
  • 34
  • From http://stackoverflow.com/questions/4459623/java-is-null-an-instance-of-object I see that if it's null, then it's not an object, is that right? – user3511965 Apr 09 '14 at 02:46
  • 1
    Yes, a `null` means that the pointer (where the object is in memory), can't point anywhere, because there's nothing to point at. You haven't created the object yet. – DeadChex Apr 09 '14 at 02:46
  • @DeadChex Is there any possible way they will be not null and actual instances? Could changing the constructor do this? – user3511965 Apr 09 '14 at 02:49
  • No. You have to implicitly tell it "Make these objects and put them in" if you want to fill it with using the default use a for loop, which I'll add to the answer. – DeadChex Apr 09 '14 at 02:49
  • What do you mean by "it's an array of null object now" at the end of the first paragraph? That makes no sense to me, so perhaps you could edit it, to try and make it correct. – Dawood ibn Kareem Apr 09 '14 at 02:50
  • @DeadChex Maybe that's why I was confused. For arrays of objects it's like MyClass[][] x = new MyClass[10][10] then we have to do x[0][0] = new MyClass(), but when there are no arrays involved, doing MyClass x = new MyClass() creates an object. – user3511965 Apr 09 '14 at 02:52
  • 3
    It should be noted that when you do `new MyObject[10][10]` the compiler, "under the covers", creates the base `MyObject[][]` array, then creates ten actual `MyObject[]` arrays to initialize the slots in the base array. This is the one case where creating an array actually puts something other than null/zero into the slots. – Hot Licks Apr 09 '14 at 02:55
  • 1
    @HotLicks It's a bit dangerous to talk about the compiler doing things that actually happen at run time. – Dawood ibn Kareem Apr 09 '14 at 03:05
  • @DavidWallace - Well, the compiler creates the logic to do the initialization, basically a `for` loop. – Hot Licks Apr 09 '14 at 10:58