This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

array-constructor  RSS feed

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • 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: 2410
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Found this thread.
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!