File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays" Watch "Arrays" New topic


Tanuja Vaid
Ranch Hand

Joined: Mar 07, 2002
Posts: 51
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
String[]SA=new String[1];SA[0]="aaa";
Also, I don't understand how below two options are valid too.
String[]SA={new String("aaa")};
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
One more source of good information...
From the horse's mouth:
Good Luck,
-Dirk Schreckmann
I agree. Here's the link:
subject: Arrays
It's not a secret anymore!