• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in Array construction

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Will this work?
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry there was a typo error I have edded by original post please take a look at it.

Thanks
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Runtime Evaluation of Array Creation Expressions

Hope this helps.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[] 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".
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

and with a small extension you know why s() is not called.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you so much for the reply let me work further if i find any thing I will reply.

Thanks once again
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic