aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in Array construction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in Array construction" Watch "doubt in Array construction" New topic
Author

doubt in Array construction

Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 142
Hi All,

when we are createing an object we will call the constructor of that class like
Class a{
a s=new a(); // in this case default constructor otherwise parametrised constructor that should be availabel
}

but when we want to create a array we directly use
a[] s=new a[10]; but we are not specifying this constructor also I dont think compiler will provided one such how is "new a[int]" working.


Thanks
subu

[ July 06, 2007: Message edited by: Subu Mhathma ]
[ July 06, 2007: Message edited by: Subu Mhathma ]
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531


Will this work?


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 142
sorry there was a typo error I have edded by original post please take a look at it.

Thanks
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
Runtime Evaluation of Array Creation Expressions

Hope this helps.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
Did you go through it? it says, "space is allocated for the new array", but there is no mentioned about constructor (in case array of non-primitives), it doesnt say if it is calls the constructor as that many times or not? if yes, why, if not why not?

Though this is nowhere on the exam, but it would be of great help if somebody could throw light on this.
Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 142
Hi Thanks for the information,

basically my doubt was how who will provide the constructor new a[10] and how it is handled. or is it a kind of instruction to the JVM with [] braces to treat it like a array creation.

Thanks
subu
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
[] sure means array.But there are two things, array declaration, and array creation. While declaration it doesnt matter much with memory part except creating the reference. Question focuses on what happens when arrays are created?

a [] s =new a[10]

Of course this will have one reference "s". But "will a constructor a() be called ten times", is what my doubt is(and if i am not mistaking you yours too). If yes, then why, if not then why not? Memory sure will be allocated for the ten objects of type "a".
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Akhilesh Trivedi:
Memory sure will be allocated for the ten objects of type "a".
No. Memory will be allocated for the ten object references of type "a". By the way, you should write a small program to answer your doubt.
Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 142
Hi,

class s{
public s(){
System.out.println("The default constructor ");
}
public static void main(String... a){
s p[]=new s[10];
}
}

I wrote this programme but no out put so s() was not called.

Thanks
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi,

and with a small extension you know why s() is not called.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
Originally posted by Manfred Klug:
Hi,

and with a small extension you know why s() is not called.



Thanks for correcting manfred. We can still hold on with


and it is not null. who provides the default constructor for the array object?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

printing the base array "p" is NOT NULL why because just now you have created it (by specifying the size of array).

whereas, printing the element of an array "p[0]" will be NULL because the individual array elements are NOT yet initialized(or not created).

But then why the constructor is not being called here?

I think as per the JLS (and the link given by Akhilesh Trivedi for Array creation at runtime), it says,


Then, if a single DimExpr appears, a single-dimensional array is created of the specified length, and each component of the array is initialized to its default value.


That means, it says that just an array of 1D will be created and allocated the room for the size mentioned. The constructor will be called only when initialize the individual members like



Am i right? If its the case, this is an exception to the normal Object creation scenario, as always the Constructor gets invoked when you invoke the new operator, though array is also created in the same way!

HtH.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 142
Hi All,

Thanks for the information.

I know that when we create an array actually array will not hold objects but refrences to objects. my question was not to ask whether a constructor will be called for array elements like p[0]. But it is is much before this that is when we create an array p[] with new s[10] how actually s[10] is taken care because we have s() constructor internally which constructor or what happens when we call new s[10] I know memory of size 10 will be allocated and array will be initalized to its type.

Thanks
subu
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Hi Subu

I have given the reason for the same query in my prev reply. Please read about the JLS part which is in quotes.

It just creates an Object of type specified and allocates the size for the dimension given. Thats all. The JLS does NOT mention anything about the invocation of Constructor in the case of Array Creation.

Only when the individual members are initialized, the constructor of the class would be invoked.

HtH.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
Subu, that is where i stressed on when i said,

"System.out.println(p) is not null."

Muthu,
If the above statement is not null, then the p has some object, if p has some object then it sure would have passed some constructor call.

p[0] does not hold object but p itself does. I repeat,

p[0] does not hold object but p itself does.

The only place where p is initialized is...

s p[]=new s[10];

do we expect a constructor call for p here ? will it be valid if I say, new s[10] calls the constructor of the super class of all objects... "Object()", i believe that is what happens.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Thats fine Akhilesh. I do agree and that might possibly be happening.

But what i was stressing was, the constructor of the corresponding class, whose instances are created is NOT invoked!! That is not mentioned in the JLS as well.

HtH.
Chandra shekar M
Ranch Hand

Joined: Dec 20, 2006
Posts: 142
Thanks you so much for the reply let me work further if i find any thing I will reply.

Thanks once again
Aaron Raja
Ranch Hand

Joined: Jun 07, 2007
Posts: 206
All you need to know about Array is this!
Arrays are dynamically created objects in Java code. An array can hold a number of variables of the same type. The variables can be primitives or object references; an array can even contain other arrays.

Declaring array variables
-------------------------
When we declare an array variable, the code creates a variable that can hold the reference to an array object. It does not create the array object or allocate space for array elements. It is illegal to specify the size of an array during declaration. The square brackets may appear as part of the type at the beginning of the declaration or as part of the array identifier:
int[] i; // array of int
byte b[]; // array of byte
Object[] o, // array of Object
short s[][]; // array of arrays of short

Constructing arrays
-------------------
You can use the new operator to construct an array. The size of the array and type of elements it will hold have to be included. In the case of multidimensional arrays, you may specify the size only for the first dimension:
int [] marks = new int[100];
String[][] s = new String[3][];

Initializing arrays
--------------------
An array initializer is written as a comma-separated list of expressions, enclosed within curly braces:
String s[] = { new String("apple"),new String("mango") };
int i[][] = { {1, 2}, {3,4} };
An array can also be initialized using a loop:
int i[] = new int[5];
for(int j = 0; j < i.length;j++){
i[j] = j;
}

Accessing array element
------------------------
Arrays are indexed beginning with 0 and ending with n-1, where n is the array size. To get the array size, use the array instance variable called length. If you attempt to access an index value outside the range 0 to n-1, an ArrayIndexOutOfBoundsException is thrown.


Thanks, AR
SCWCD 5, SCJP 1.4, OCA (PL/SQL)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in Array construction