File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array-constructor 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 "array-constructor" Watch "array-constructor" New topic
Author

array-constructor

joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
String[] x = new String[10];
At this line which constructor gets called.
Since we have used new keyword some constructor must be called. which is that? I am not able to figure out, but i know for sure that String constructor is not called.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
What happens when new is used like this is that a new array is created. An array is an object in Java, but does not have a class definition like other objects. Space is allocated in memory for the elements of the array. In this case, 10 contiguous spaces are set aside in memory each holding a reference to a String. Before you assign the individual elements of the array to point to actual Strings, each reference is null.
Balaji Sampath
Ranch Hand

Joined: Sep 30, 2005
Posts: 63
Let me try to rephrase the question in this way:



From the above code the first constructor to be called would be the default constructor StringCreation() .

I beleive thats the explanation u require?

Anywayz please to brief me if u need any further detail regarding the above code.

Thanks
Balaji.S
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Found this thread.
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
Thanks balaji,
can you put some more light on StringCreation() constructor.
I mean is this StringCreation a logical class.
Badri Sarma
Ranch Hand

Joined: Apr 01, 2003
Posts: 72
Keith Lynn explanation was correct. when you use the following piece of code
String str[] = new String[10];

continious memory allocation would be created. No constructor will be called, at that point.


Thanks<br />Badri
Lukas Stephienn
Ranch Hand

Joined: Dec 23, 2005
Posts: 60
Keith Lynn explanation was correct. when you use the following piece of code
String str[] = new String[10];

continious memory allocation would be created. No constructor will be called, at that point.


Don't agree with that. Becaue Arrays extends Object, the Object constructor will be called.


SCJP 5.0 (91%), SCBCD 1.3 (94%), SCWCD 1.4 (88%)
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
Thats the point, if object constructor gets called then who calls it.
I believe since array extends object and if object constructor gets called, there must be some constructor calling it.
Or may be no constructor gets called at all.
I am still to find the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array-constructor