Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays

 
Tanuja Vaid
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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")};
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic