This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
String x = new String; 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.
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.
Keith Lynn explanation was correct. when you use the following piece of code String str = new String;
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%)
Joined: Jun 05, 2006
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.