• 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

array-constructor

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found this thread.
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks balaji,
can you put some more light on StringCreation() constructor.
I mean is this StringCreation a logical class.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic