• 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

Arrays

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which of the following statements will correcly create and initialize an array of Strings to non null elements?(jq+ question)
why is
String[]SA=new String[1]{"aaa"};
not valid
and
String[]SA=new String[1];SA[0]="aaa";
valid
Also, I don't understand how below two options are valid too.
String[]SA={"aaa"};
String[]SA={new String("aaa")};
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Just to point out the obvious...
The first one won't compile - so, it's not the right choice.
The other three will compile and do what the question is looking to do.
Bradley Kjell has a nice on-line Java tutorial that explains many concepts of Java step-by-step. Chapter 46 on Arrays might help you to understand array declaration and initialization better.
Good Luck,
-Dirk Schreckmann
[ March 07, 2002: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
One more source of good information...
From the horse's mouth:
http://java.sun.com/docs/books/tutorial/java/data/arrays.html
Good Luck,
-Dirk Schreckmann
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic