3

I am a rookie in Java Programming. I am clearing all my concepts before making a step ahead. I was reading an array chapter which says the basic structure to create an array is:

Type[] var_name = new Type[limit];

I was going through some slides of opencourseware. In these slides, they inserted a class name into the type of the array. For example:

public class Baby {
   Baby[] siblings;
}

Can someone explain me what is the difference between the basic array structure and the structure written inside class.

5
  • The difference is that with this design you may decide if clients of the class may or may not have direct manipulation of the array. It will depend on the design you give to the class. Commented May 18, 2015 at 17:51
  • 1
    @azurefrog that's not the question: what is the difference between the basic array structure and the structure written inside class. Commented May 18, 2015 at 17:52
  • If this helps, it basically makes a bunch of babies. It's no different then any other array. Each Baby would be referenced such as siblings[1].getName() Commented May 18, 2015 at 17:53
  • You can actually create an array of each type, no matter if they are primitives (int, long, ...) or classes. One thing: you should not use limit, but size. An array does not "grow". If you create it (by calling new Type[size]), it will allocate memore for size-many Types. Commented May 18, 2015 at 17:54
  • 3
    OP - can you clarify what bothers you about this exactly? The fact that there is no = and what's following it, or the fact that the class name is used? Or something else? Commented May 18, 2015 at 17:56

3 Answers 3

5

I think this may just be confusion about what constitutes a type. For the reference:

Type[] var_name = new Type[limit]

"Type" must be replaced with any primitive type (int, double, etc.) as well as any Class (Baby, in your case), such as:

String [] string_array = new String[10];

If that's not the issue you're having, the other difference between the two statements is that the first actually creates an array of size "limit" and assigns it to the variable var_name... whereas in the Baby declaration, only the member variable "siblings" in the Baby class is declared. That variable can hold a Baby array, but that array has not yet been created. In the Baby constructor, you might see something like:

Baby() {
     siblings = new Baby[100];
}

This would create an array of Baby class object references of size 100, and assign it to the siblings member of the instance of Baby being created.

Sign up to request clarification or add additional context in comments.

2 Comments

It means if I write class name instead of Type, array will take the primitive type of class? Is it only used to specify primitive type of array?
the word "Type" as used in the reference must be replaced with an actual type, just as "limit" must be replaced with a number. Then, yes, the array would only be able to hold references to objects of that type. If you did "int [] int_array = new int[100]" that array could only hold 100 ints, no Strings, etc.
0

Baby[] sibling is just a declaration of array. It indicates compiler that Baby contains a variable which is of type array.
The first line you mentioned is "declaring" an array as well as "initializing" it. By initializing compiler allocates memory as per the size of array. This is when actual values can be inserted in array variable.

Comments

0

An array basically is a block of contiguous memory which can contain either primitive datatypes or objects of type that you create. Java is a statically typed language - you have to specify the type of the parameter at compile time. So you specify the type at the time you declare the array even though when you initialize it, you are only initializing a contiguous block of memory. By specifying the type, the compiler knows that this memory is to hold an object or a primitive of that 'Type'.

Type[] type = new Type[size];

the above line of code creates a block of contiguous memory of size 'size'. It holds elements of type 'Type' (in this case simply a placeholder for the type of elements you want the array to hold). Notice that you have to specify the type here.

When you have the line:

Baby[] siblings;

you are declaring the array. You still have not initialized it. Before using this, you should initialize it as:

siblings = new Baby[size];

It is only at this point that memory is allocated for this array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.