File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Array Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Question" Watch "Array Question" New topic
Author

Array Question

Tom Keith
Greenhorn

Joined: Mar 26, 2002
Posts: 24
Hi All,
Consider the following String array declarations.
1)
String[] paramString = new String[4];
paramString[0] = "user1";
paramString[1] = "pwd";
paramString[2] = "1";
paramString[3] = "sa";
2)
String[] paramString = new String[4] {"user1", "pwd", "1", "sa"};
3)
String[] paramString = {"user1", "pwd", "1", "sa"};
My assumption is that all of three are same
Is there is any advantage or disadvantage in doing them or is it just a matter of coding style?
Does it have to do anything with Garbage Collection?
Thanks,
Tom
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
Number 2 will not compile. Did you try it?


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Tom Keith
Greenhorn

Joined: Mar 26, 2002
Posts: 24
Hi Bosun,
Number 2 compiles and works fine . I have been using it for quite sometime. I just wanted to know the difference.
Thanks,
Tom
timothy zimmerman
Ranch Hand

Joined: Jun 26, 2001
Posts: 149
Odd I would not have thought that 2 would have compiled either. I don't think you need to dimension the array if you are setting it's elements.
to be sure I did a quick test and declaring the array as String[] arr1 = new String[4]{ "one", "two", "three", "four" };
gives the following:
Test.java:10: ';' expected
String[] arr1 = new String[4]{ "one", "two", "three", "four" };
timothy zimmerman
Ranch Hand

Joined: Jun 26, 2001
Posts: 149
Sorry Posted before I finished. As far as your question ... I am not whether the way in which an array is declared and populated would matter to the GarbageCollector.
I would think it is probably coding style. You may know how many elements you want to put in an array but you may not know what those elements are at that moment ... or even at the same time ( you may need to store some ... od some work and come back and add the rest later )
matt hooker
Ranch Hand

Joined: Jul 26, 2001
Posts: 46
Tom,
FYI - unless you are running your own special implementation of Java - number 2 cannot compile as it is syntactically rubbish !!!


Its not what you do, its the way you say you've done it.
matt hooker
Ranch Hand

Joined: Jul 26, 2001
Posts: 46
Anyway - that matter aside, the difference between style 1 and 3 (the 2 that work ) is the amount you have to type.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
As I recall, option 3 was not possible in the earliest versions of Java. But eventually they decided to go ahead and give us this bit of syntactic sugar. The difference between 1 and 3 has no effect on garbage collection. If you know all the elements you want at compile time, you might as well use 3 as it's shorter. 1 is longer but more flexible - use it if you don't know all the elements in advance.


"I'm not back." - Bill Harding, Twister
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
Perhaps you mean

String[] paramString = new String[] {"user1", "pwd", "1", "sa"};


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Question
 
Similar Threads
Threads
sorting array
Synchronized Objects
Threading question from Dan's mock
Threads again