The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Must be a String object not an array of String objects...

Posted by Kishori Sharan on December 22, 2000 at 9:27 AM

All arrays' componenets in java are initialized by default to null. You don't need to initialize them explicitly unless you want to initialize all componenets of newly created array to a value other than null ( for reference type array ) . The error you got must be becuase you are not initializing any String object not any string array. For example
String[] s = new String[3] ;

System.out.println ( s[0] ) ; and it will print "null" because all reference array ( here String one ) are initialized to null. It doesn't matter whether is declared as a memeber of a class or local to a method.
However, if you want to initailize the String array componenets to "" at runtime then you can do that as

// The array s has been created before this line of code
// but you don't know its actial length
for ( int i = 0 ; i < s.length ; i++ ) {
s[i] = "" ;



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us