• 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

Empty strings

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which correctly create an array of five empty Strings?
A.String a [] = new String [5];
for (int i = 0; i < 5; a[i++] = "");
B.String a [] = {"", "", "", "", ""};
C.String a [5];
D.String [5] a;
E.String [] a = new String [5];
for (int i = 0; i < 5; a[i++] = null);
The answer should be A) B) but the given answe was only B)
in http://www.sarga.com/
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right Roll, (a) is just as valid as (b). To be nit-picky, there's really only one String in each case, with five references to it (since "" is yet another String literal, each time it appears in code it references the same object). I can't see that that matters much though since Strings are immutable.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel that answer should be (b)
Why because, the exact question is: Which correctly create an array of 5 empty strings.
In the answer (a) : array of 5 string objects is created in the very first statement itself but new constructor returns null references to array of strings. After which, for loop comes: whic h is just to modify the references that array hold. It does not create any thing. ( that for loop ).
But in the anser(b) : it creates empty string objects....
Am I missing any thing here.
If u create bytecode translation ( readable ), then it would be very easy to understand.....
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya want bytecodes, we got bytecodes!

The codes are different, but I believe they accomplish the same end: they create the array, then they load the same constant value into all five elements of the array. Whether they achieve the latter by repeating near-identical code five times, or running a loop, seems irrelevant. Either way, the array is initially created with all nulls, and then each element must be set to reference the same instance of the empty String "". So as near as I can tell, they are identical in effect.

[This message has been edited by Jim Yingst (edited February 01, 2000).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic